Simple communication between two arduino with xbee S2 (help with code)

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. :slight_smile:
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

So since the first post, I have not come yet with a working code. After lot of attempts of different codes (a mix of internet examples and my codes) I decided to make my sketch easier.

The transmitter code :

#include <XBee.h>

uint8_t text[] = {'H', 'e', 'l', 'l', 'o'};

XBee xbee = XBee();
XBeeAddress64 remoteAddress = XBeeAddress64(0x0013a200, 0x407a3903);
ZBTxRequest zbTx = ZBTxRequest(remoteAddress, text, sizeof(text));

void setup () {
  delay(1000);
  xbee.begin(9600);
}

void loop () {
  xbee.send(zbTx);
  delay(3000);
}

The receiver code :

#include <XBee.h>

XBee xbee = XBee();
ZBRxResponse zbRx = ZBRxResponse();

void setup () {
  delay(1000);
  xbee.begin(9600);
  Serial.println("starting up yo!");
}

void loop () {

  // 1. This will read any data that is available:
  xbee.readPacket();

  // 2. Now, to check if a packet was received: 
  if (xbee.getResponse().isAvailable()) {
    if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
      xbee.getResponse().getZBRxResponse(zbRx);
    
     for (int i = 0; i < zbRx.getDataLength(); i++) {
         Serial.print("payload [");
         Serial.print(i, DEC);
         Serial.print("] is ");
         Serial.print(zbRx.getData(i));
     }
    }
  }
}

I think that the problem is my code because when I connect the Xbee coordinator directly to the computer (with Xbee shield) and run X-CTU, I receive the message sent by the transmitter (see attachment).

Whearas, when I try to get the data with the serial monitor of the arduino, I don't get anything.

It may seem easy for someone, but I'm a newbie with arduino and Xbee, so if someone could answer me or correct my receive code it would be nice. :slight_smile:

  if (xbee.getResponse().isAvailable()) {

Add an else clause, and print a message...

    if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {

Ditto.

Where is it failing?

Thanks for the reply PaulS.

As you said, I put an else clause to the first if :

Serial.println("No packet available");

and to the second if :

Serial.print("No data getting out of the packet");

When I connect the Uno to the computer, the Din Led of the xbee shield lights and I get on the serial monitor "No packet available", even when I turn on the transmitter Fio (The two RSSI Led light then but no change of the message print).

I don't understand why it is failing here, any ideas?

I've just tried something else that I found weird. When I connect the Xbee router API (which is normally connected to the fio) directly to the computer with the xbee shield and open the Terminal of X-CTU, I get the message "No packet available" of the Receiver (Xbee->Arduino Uno->USB). The Uno also has it Tx led blinking.

Is that normal that the Receiver sends this message to the transmitter even if I don't code it like the transmetter's code with a "xbee.send(zbTx);"?

Is that normal that the Receiver sends this message to the transmitter even if I don't code it like the transmetter's code with a "xbee.send(zbTx);"?

Yes. The XBee is connected to the serial port, so anything sent to the serial port will be broadcast. The API mode and library just make formatting, sending, receiving, and parsing of the packet transparent to the user.

Thanks for the answers PaulS.

After racking my brains trying to found why my code didn't work comparing of the different code or examples I saw, I decided to look again on the Xbee configuration's side and tried different settings. I finally decided to reset them and reconfigure them.

There are my working settings if they can help somebody :
Receiver : COORDINATOR API, Version 2170 instead of the latest 218C (it works so I will keep this version for now), DH=0, DL=0, AP=2
Transmetter : ROUTER API, Version 2370 instead of 238C, DH=0 instead of the DH's receiver, DL=0 instead of the DL's receiver, AP=2

It works :slight_smile: and I can now receive the Hello message...in hexadecimal!!

PS : I also figure out that when I turn off the Uno (coordinator's side) for example to change his sketche and I don't turn off/on the Fio (router), when I turn back the Uno, the two Xbee don't reconnect (no RSSI signal and no receiving), have to turn off/on the fio to make a "Reset" for the network. It may help, I don't know.

Hello

great efforts :slight_smile:
I have tried your codes with all options but no thing work with me
it fall at the first if statement ?!
any help I will appreciate it

Regards

?

I Have tried this thing
I have one XBee in API as coordinator, and one router XBee in API

I connected the arduino to the router and installed the following code on arduino

#include <XBee.h>

uint8_t text[] = {'H', 'e', 'l', 'l', 'o'};

XBee xbee = XBee();
XBeeAddress64 remoteAddress = XBeeAddress64(0x0013a200, 0x407a3903);
ZBTxRequest zbTx = ZBTxRequest(remoteAddress, text, sizeof(text));

void setup () {
delay(1000);
xbee.begin(9600);
}

void loop () {
xbee.send(zbTx);
delay(3000);
}

the serial output of the arduino is:

7E 00 0F 10 01 00 7D 33 A2 00 40 7D 5E 70 39 FF FE

Which is has no response on coordinator !!

So I tried the following, I tried to communicate using direct I/O on the two XBees and I has a API frame communication from the router to the coordinator which is :

7E 00 12 29 00 13 A2 00 40 7C 48 1E 4C 46 01 01 00 01 00 00 01 00

and communicate well :slight_smile:
I Think the problem is in the code on arduino it should be edited to send a frame such as the second one to establish communication

I’m I right ??
I think the following command should modified

ZBTxRequest zbTx = ZBTxRequest(remoteAddress, text, sizeof(text));

If yes, How to make that ??

P.S: the XBees configuration as follow
Receiver : COORDINATOR API, Version 21A7 , DH=0, DL=FFFF
Transmetter : ROUTER API, Version 23A7 , DH=0 , DL=0
AND THE SAME PAN ID
regards

Try to reset your two xbees and then just change these parameters :
API mode Coordinator and Router, AP=2, DH=0, DL=0, BD at 9600 for the two.

Then connect the Coordinator directly to the PC with the usb shield and connect the Router to the Arduino (as you done) and upload the Router code.

Then open the XCTU to see what you receive on the Coordinator's side.

I did exactly what you said but there is no communication :~

mismas_nc:
Try to reset your two xbees and then just change these parameters :
API mode Coordinator and Router, AP=2, DH=0, DL=0, BD at 9600 for the two.

Then connect the Coordinator directly to the PC with the usb shield and connect the Router to the Arduino (as you done) and upload the Router code.

Then open the XCTU to see what you receive on the Coordinator's side.

Mismas_nc is your working code on this page? I'm also trying to use API on xbee S2's.

Definitely going to try and go over xbee's configs tonight.

This thread was such a HUGE help!
mismas_nc , THANKS!!!!

I am currently using a Coordinator in API mode (robot) and a Router in AT mode (controller). The controller is using direct IO so their is no Arduino board on this side. The receiving coordinator side has the arduino. I would like to be able to pull data from the robot and send it back to the controller (ex. backup sensors for if it about to back up into something. The robot is a 140lb electric wheelchair which has been converted into a snow plow robot.

[code]#include <XBee.h>

uint8_t text[] = {'H', 'e', 'l', 'l', 'o',' ','T','H','I','S',' ','I','S',' ', 'A','R','D','U','I','N','O'};
XBee xbee = XBee();
XBeeAddress64 remoteAddress = XBeeAddress64(0x00000000, 0x0000FFFF);//64 address of remote xbee:
ZBTxRequest zbTx = ZBTxRequest(remoteAddress, text, sizeof(text));// api 2 Tx request frame
ZBRxResponse zbRx = ZBRxResponse();// reading remote xbee response:
void setup()
    {
      Serial.begin(9600);
      xbee.setSerial(Serial);
      Serial.println("starting up yo!");
    }
void loop ()
    {       
       xbee.readPacket();// This will read any data that is available:
       if (xbee.getResponse().isAvailable())//Now, to check if a packet was received: 
        {   
          if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) 
            {
              xbee.send(zbTx);//sending data to remote xbee:
            }
        }
    }


[/code]

Transmetter: COORDINATOR API 2, latest Version , DH=0, DL=FFFF,
Receiver: ROUTER API 2 latest Version , DH=0, DL=0,