1
\$\begingroup\$

I am not sure if this is a right place to put this question but if anyone is familiar with XBee and can help me with this problem, I highly appreciate your help.

I have two XBees, one configured as coordinator API and another configured as router API.

I have attached those two XBees with two arduinos just to test communication between them. The code for each one is very simple.

Code in arduino hooked to coordinator XBee

Description: This will simply check button press. Upon press it will send ZigBee transmit request frame.

int BUTTON1 = 3;

void setup()
{
    pinMode(BUTTON1, INPUT);
    Serial.begin(9600);
}

void loop()
{
    if (digitalRead(BUTTON1) == HIGH)
    {
        sendData();
    }
}

void sendData()
{
    Serial.write(0x7E);

    Serial.write(0x00);
    Serial.write(0x16);

    Serial.write(0x10);

    Serial.write(0x01);


    Serial.write(0x00);
    Serial.write(0x13);
    Serial.write(0xA2);
    Serial.write(0x00);
    Serial.write(0x40);
    Serial.write(0x8C);
    Serial.write(0xC6);
    Serial.write(0xD4);

    Serial.write(0x20);
    Serial.write(0x01);

    Serial.write(0x00);

    Serial.write(0x00);

    //data start  
    Serial.write(0x54);
    Serial.write(0x78);
    Serial.write(0x44);
    Serial.write(0x61);
    Serial.write(0x74);
    Serial.write(0x61);
    Serial.write(0x30);
    Serial.write(0x41);

    Serial.write(0x13);

    delay(10);

}

Next is the code for arduino hooked to router XBee.

Description: This simply scans "0x54" byte and if it is there then lights up a LED. As you can see, coordinator sends this byte 0x54 along with other bytes.

int BELL = 5;

void setup()
{
    pinMode(BELL, OUTPUT);
    Serial.begin(9600);
}

void loop()
{

    if(Serial.available() >= 25)
    {
        if(Serial.read() == 0x7E)
        {
            for(int i=0;i<24;i++)
            {
                if(Serial.read() == 0x54)
                {
                    digitalWrite(BELL, HIGH); 
                }
            }
        }
    }
}

I would like to add that, when I turn on serial monitor, and then press the button, the serial monitor displays correctly what data was passed. Also tx lights up. So my assumption is that the problem is in the router not getting the data rather than coordinator not sending the data. I have been trying to hunt this problem for couple of hours now with no success. Any help is highly appreciated!

\$\endgroup\$
6
  • \$\begingroup\$ Can you plug in the router to your computer via a UartBee or similar? If so check with the computer whether it is receiving the messages \$\endgroup\$ Commented Jun 11, 2013 at 4:12
  • \$\begingroup\$ How large is the buffer serial.available can hold? Maybe you could try just sending a single byte and see if this arrives. \$\endgroup\$
    – Tom L.
    Commented Jun 11, 2013 at 4:41
  • \$\begingroup\$ I cleaned up your code a lot. You really don't need that many linebreaks. \$\endgroup\$ Commented Jun 11, 2013 at 8:57
  • \$\begingroup\$ In API mode you have to construct packets. Good idea though to do this in AT mode. \$\endgroup\$ Commented Jun 11, 2013 at 11:33
  • \$\begingroup\$ I have no idea what API or AT mode is (I'm not an Arduino guy) but it looks like he is triggering on specific bytes, not the whole packet itself. Of course, the idea is just meant for debugging, but it's easier to debug smaller pieces :) \$\endgroup\$
    – Tom L.
    Commented Jun 11, 2013 at 19:23

1 Answer 1

1
\$\begingroup\$

I don't think your checksum byte is correct.

Adding up all the bytes from after the length bytes to before the checksum byte gives me 0x604. 0xff - 0x04 = 0xfb

You should write a method to implement sending (and receiving too), e.g.

struct xBeePacket {
   unsigned int dest_address;
   unsigned char api_id;
   unsigned char frame_id;
   unsigned char options;
   unsigned char data[100];
   unsigned int length;
}

void send_message(xBeePacket* p)
{
   unsigned char checksum = 0;

   Serial.write(0x7E);
   Serial.write((byte)(length>>8));
   Serial.write((byte)(length & 0x00ff));

   Serial.write(p->api_id);  checksum += p->api_id;
   Serial.write(p->frame_id);  checksum += p->frame_id;

   Serial.write((byte)(p->dest_address >> 8));  
   checksum += (byte)(p->dest_address >> 8);
   Serial.write((byte)(p->dest_address & 0x00ff));  
   checksum += (byte)(p->dest_address & 0x00ff);

   Serial.write(p->options);  checksum += p->options;

   for (int i = 0; i < p->length; i++)
   {
       Serial.write(p->data[i]);  checksum += p->frame_id;
   }

   Serial.write(0xff - checksum);
}

Hoep this gets you started.

\$\endgroup\$
2
  • \$\begingroup\$ @user734861 I was looking at your new answer and code, but it has disappeared. For adding up the checksum use unsigned char instead of long. It will rollover but that is ok, it is what is meant to happen. Also on the coordinator side, since you have options byte set to 0x00 it will return an ack if successful, so you could check if you are receiving that back from the XBee. \$\endgroup\$ Commented Jun 11, 2013 at 22:10
  • \$\begingroup\$ @user734861 And if all fails, try a library like code.google.com/p/xbee-arduino :) \$\endgroup\$ Commented Jun 11, 2013 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.