Les 2 cavaliers USB/XBee permettent de switcher le Serial entre le port USB pour téléchargement et XBee pour l'execution du programme.
Les carte XBee ne sont pas compatibles avec les breadboard (Pas 2,54), il faut donc utiliser une Shield.
/*
XBEE #2 SENDER
==============
Button-Arduino-Xbee#2 > Xbee#1-Arduino-led
Most code from : XBee Analog Duplex sender
http://www.faludi.com/classes/sociableobjects/code/XBee_Analog_Duplex_Sender.pde
*/
const int button = 2;
const int led = 13; // just to see when zigbee is ok
int buttonState;
int lastButtonState = LOW;
void setup() {
Serial.begin(9600); // liaison série de la console (débugage/xbee according to jumper on xbee shield)
pinMode (led, OUTPUT);
pinMode (button, INPUT);
setDestination();
blink(led, 3);
}
void loop() {
buttonState = digitalRead(button);
if (buttonState != lastButtonState) {
// Send serial data to xbee #1
Serial.println(buttonState, DEC);
lastButtonState = buttonState;
}
delay(50);
}
// Xbee configuration
void setDestination() {
Serial.print("+++");
char thisByte = 0;
while (thisByte != '\r') { // wait for xbee response
if (Serial.available() > 0) {
thisByte = Serial.read();
}
}
Serial.print("ATRE\r");
Serial.print("ATDL0\r"); // sent to xbee 0
Serial.print("ATMY1\r"); // this xbee is 1
Serial.print("ATID1111\r");
Serial.print("ATCN\r");
}
// Blink
void blink(int thisPin, int howManyTimes) {
for (int blinks=0; blinks< howManyTimes; blinks++) {
digitalWrite(thisPin, HIGH);
delay(200);
digitalWrite(thisPin, LOW);
delay(200);
}
}
/*
XBEE #1 RECEIVER
================
Button-Arduino-Xbee#2 > Xbee#1-Arduino-led
Most code from : XBee Analog Duplex sender
http://www.faludi.com/classes/sociableobjects/code/XBee_Analog_Duplex_Sender.pde
*/
const int led = 13;
String inputString = "";
int receiveValue;
void setup() {
Serial.begin(9600); // liaison série de la console (débugage/xbee according to jumper on xbee shield)
pinMode (led, OUTPUT);
setDestination();
blink(led, 3);
}
void loop() {
// Receive serial data from xbee #2
if (Serial.available() > 0) {
handleSerial();
}
}
// Xbee configuration
void setDestination() {
Serial.print("+++");
char thisByte = 0;
while (thisByte != '\r') {
if (Serial.available() > 0) {
thisByte = Serial.read();
}
}
Serial.print("ATRE\r");
Serial.print("ATDL1\r"); // sent to xbee 1
Serial.print("ATMY0\r");
Serial.print("ATID1111\r");
Serial.print("ATCN\r");
}
void handleSerial() {
char inByte = Serial.read();
// save only ASCII numeric characters (ASCII 0 - 9):
if (isDigit(inByte)){
inputString = inputString + inByte;
}
// if you get an ASCII newline:
if (inByte == '\n') {
// convert the string to a number:
receiveValue = inputString.toInt();
// set the analog output LED:
digitalWrite(led, receiveValue);
// Serial.println(receiveValue, DEC);
// clear the input string for the
// next value:
inputString = "";
}
}
// Blink
void blink(int thisPin, int howManyTimes) {
for (int blinks=0; blinks< howManyTimes; blinks++) {
digitalWrite(thisPin, HIGH);
delay(200);
digitalWrite(thisPin, LOW);
delay(200);
}
}