XBee router address as an argument

Greetings,

I am trying to build a small XBee network with one coordinator and five routers. I would like to be able to have the function that builds the API string have the router address as an argument. Something like this:

uint16_t router_address_1[16]={0x00, 0x13, 0xA2, 0x00, 0x40, 0x0A, 0x59, 0xF1}; .... void setup() { }

void loop() { }

void setRemoteState(uint16_t SHSL, char value1, char value2, char value3) { ... Serial.write((uint16_t)SHSL, sizeof((uint16_t)SHSL));... }

Is it possible ? Thank you.

// Arduino pins 2,3,4,5 driving High/Low pins DIO0,DIO1,DIO2,DIO3 on XBee
// 
int red_light = 2;
int green_light = 3;
int yellow_light = 4;
int blue_light = 5;
int white_light = 6;

// Routers addresses
uint16_t router_1[16]={0x00, 0x13, 0xA2, 0x00, 0x40, 0x0A, 0x59, 0xF1};

void setup() {
  pinMode(red_light, INPUT);
  pinMode(green_light, INPUT);
  pinMode(yellow_light, INPUT);
  pinMode(blue_light, INPUT);
  pinMode(white_light, INPUT);
  
  digitalWrite(red_light, LOW);
  digitalWrite(green_light, LOW);
  digitalWrite(yellow_light, LOW);
  digitalWrite(blue_light, LOW);
  digitalWrite(white_light, LOW);
  
  Serial.begin(9600);
}

void loop() {
  // Red lights **************************
  if (digitalRead(red_light) == HIGH)
  { setRemoteState((uint16_t)router_1, 0x44, 0x34, 0x5); }
  else 
  { setRemoteState((uint16_t)router_1, 0x44, 0x34, 0x4); }
  
void setRemoteState(uint16_t SHSL, char value1, char value2, char value3)
{   
// Remote XBee: AT, Base XBee: API
Serial.write(0x7E);         // Sync up the start byte
Serial.write((byte)0x0);    // Length MSB (always 0)
Serial.write(0x10);         // Length LSB
Serial.write(0x17);         // 0x17 is the frame ID for sending an AT command
Serial.write((byte)0x0);    // Frame ID (no reply needed)
// Send the 64 bit router destination address
Serial.write(SHSL, sizeof((uint16_t)SHSL));

Serial.write(0xFF);         // Destination Network (Set to 0xFFFE if unknown)
Serial.write(0xFE);
Serial.write(0x02);         // Set to 0x02 to apply these changes
Serial.write(value1);       // AT Command: D0
Serial.write(value2);
Serial.write(value3);       // Set D1 to be 5 (Digital Out HIGH)
long chexsum = 0x17 + 0xFF + 0xFF +0xFF + 0xFE + 0x02 + value1 + value2 + value3;
Serial.write(0xFF - (chexsum & 0xFF));    // Checksum
}

Is it possible ?

Yes.

Are you having a problem?

If you are using API mode, you may want to be aware of this library: http://code.google.com/p/xbee-arduino/

It defines a data type for XBee addresses, XBeeAddress64, that would make the function you propose perhaps a little cleaner. The library contains a huge amount of functionality, that is just a small part.

Thank you very much gentlemen. I have seen that library, but I was not sure I undertsand which section of the 19 bytes required by the AT command frame type (0x17) the payload covers. The command I am trying to send is:

Command to set DIO4 high:

7E 00 10 17 05 00 13 A2 00 40 0A 59 F1 FF FE 02 44 34 05 1E

all the way to F1 is the address of destination, FF FE is a generic network address.

My payload should be: 44 34 (which is hex for ASCII DIO4) and 05 (which the status = High).

How can I do this ? An example would be highly appreciated.

cheers, MaLi

Here is an example of how to send an AT command to the XBee using the library. This code requests the XBee’s Node ID. Processing the response is left as an exercise for the reader :wink: See also the examples that come with the library

#include <XBee.h>                //http://code.google.com/p/xbee-arduino/

XBee xbee = XBee();              //instantiate the XBee object

void setup(void)
{
    xbee.begin(115200);          //also does Serial.begin()
    getXBeeNI();
}

void loop(void)
{
}

//Request the XBee's Node ID (AT NI command).                           *
void getXBeeNI(void)
{
    union {byte B; char C;} atCmd[3];
    AtCommandRequest atCmdReq;

    strcpy(&atCmd[0].C, "NI");
    atCmdReq = AtCommandRequest(&atCmd[0].B);
    xbee.send(atCmdReq);
}

Jack,

Thank you very much. I see how to send AT commands. I would like though to learn how to build the payload as described above. The library seems to be very useful, but I could not find much documentation. Can you help me understand how to build the payload ?

Many thanks, MaLi

MaLi: Jack,

Thank you very much. I see how to send AT commands. I would like though to learn how to build the payload as described above. The library seems to be very useful, but I could not find much documentation. Can you help me understand how to build the payload ?

Many thanks, MaLi

Were the example sketches that come with the library any help?

Thank you Jack. Not much. They show how to build the payload with two elements, but there is nothing localizing the payload content on the overall serial message to be sent.

cheers, MaLi

MaLi: Thank you Jack. Not much. They show how to build the payload with two elements, but there is nothing localizing the payload content on the overall serial message to be sent.

cheers, MaLi

If I understand your concern, that may be something you don't need to worry about since the library takes care of building the packets. Depending on which XBees you have, see the Serial1_TX or Serial2_TX examples. Basically the TX request object (Tx16Request for S1, ZBTxRequest for S2) just needs a pointer to the payload and its length, and the address of the destination XBee.