Hello everybody,
I plan to make multiple arduino communicate with each other in a mesh network using xbees.
So I'm trying first to establish a simple communication (Tx->Rx) between two arduino with xbee series 2, using the xbee-arduino library (in Arduino 1.0 IDE) and its examples but I've got some trouble to get what I want.
Here's my configuration :
- (Base station) PC->Arduino uno->Xbee (S2, coordinator api, AP=2, 9600BD), Example_Series2_Rx_Nss (a little bit modified)
#include <XBee.h>
#include <SoftwareSerial.h>
// create the XBee object
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
int statusLed = 13;
int errorLed = 12;
int dataLed = 11;
void flashLed(int pin, int times, int wait)
{
for (int i = 0; i < times; i++)
{
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times)
{
delay(wait);
}
}
}
void setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
pinMode(dataLed, OUTPUT);
// start serial
xbee.begin(9600);
Serial.println("starting up yo!");
}
// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
//Attempt to read an API packet from the ZBee Module
xbee.readPacket();
// got something
if (xbee.getResponse().isAvailable()) {
// got a zb rx packet
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);
Serial.println("Got an rx packet!");
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
flashLed(statusLed, 10, 100);
Serial.println("packet acknowledged");
} else {
// we got it (obviously) but sender didn't get an ACK
flashLed(errorLed, 2, 100);
Serial.println("packet not acknowledged");
}
//set dataLed PWM to value of the first byte in the data
//analogWrite(dataLed, rx.getData(0));
} else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
xbee.getResponse().getModemStatusResponse(msr);
// the local XBee sends this response on certain events, like association/dissociation
if (msr.getStatus() == ASSOCIATED) {
// yay this is great. flash led
flashLed(statusLed, 10, 10);
} else if (msr.getStatus() == DISASSOCIATED) {
// this is awful.. flash led to show our discontent
flashLed(errorLed, 10, 10);
} else {
// another status
flashLed(statusLed, 5, 10);
}
} else {
// not something we were expecting
flashLed(errorLed, 1, 25);
}
} else if (xbee.getResponse().isError()) {
Serial.print("oh no!!! error code:");
Serial.println(xbee.getResponse().getErrorCode());
}
}
- (Remote station) Arduino Fio->Xbee (Series 2, routeur API, AP=2, 9600BD)->Photocell sensor, Example_Series2_Tx
With these two codes, I've got on the side of the arduino Fio, the status led blinking once, telling me that the packet has been sent, then blinking more times to confirm that the transmission was a success and also, the error led blinking when I switch off the power of the Arduino Uno because the base station didn't receive the packet.
But, on the side of the arduino Uno, I haven't got any response from the base station (errorLed or statusLed
or errorLed or messages on the serial monitor except the "starting up yo!"). :~
Can somebody tell me why this happens or correct my code to see the communication Rx status?
How can I see the the sensor values on the Arduino serial monitor with the receive packet?
Thanks