arduinoserial-portxbee

How to send multiple commands to xbee router in one data packet transmission using arduino uno


I was wondering if I could control multiple IO pins on the xbee remotely with one command from a coordinator.

I can easily send one IO pin high or low as below

xB.write((byte)0x7E);
xB.write((byte)0x0);
xB.write((byte)0x10);
xB.write((byte)0x17);
xB.write((byte)0x0);  // Frame ID
xB.write((byte)0x00); // first byte
xB.write((byte)0x13); // 2
xB.write((byte)0xA2); // 3
xB.write((byte)0x00); // 4
xB.write(addy5); // 5
xB.write(addy6); // 6
xB.write(addy7); // 7
xB.write(addy8);// 8
xB.write((byte)0xFF);
xB.write((byte)0xFE);
xB.write((byte)0x02);
xB.write('D');
xB.write('0');
xB.write(val);
long sum = 0x17 + 0x13 + 0xA2 + addy5 + addy6 + addy7 + addy8 + 0xFF + 0xFE + 0x02 + 'D' + '0' + val;
xB.write((byte)0xFF - (sum & 0xFF));

But I can't seem to send more than one digital pin change at once like this.

xB.write((byte)0x7E);
xB.write((byte)0x0);
xB.write((byte)0x1F);
xB.write((byte)0x17);
xB.write((byte)0x0);  // Frame ID
xB.write((byte)0x00); // first byte
xB.write((byte)0x13); // 2
xB.write((byte)0xA2); // 3
xB.write((byte)0x00); // 4
xB.write(addy5); // 5
xB.write(addy6); // 6
xB.write(addy7); // 7
xB.write(addy8);// 8
xB.write((byte)0xFF); // "10"67
xB.write((byte)0xFE);// 10"66"
xB.write((byte)0x02);

xB.write('D');
xB.write('1');
xB.write(bin[0]);

xB.write('D');
xB.write('0');
xB.write(bin[1]);

xB.write('D');
xB.write('2');
xB.write(bin[2]);

xB.write('D');
xB.write('3');
xB.write(bin[3]);

xB.write('D');
xB.write('5');
xB.write(bin[4]);

xB.write('D');
xB.write('4');
xB.write(bin[5]);

long sum = 0x17 + 0x13 + 0xA2 + addy5 + addy6 + addy7 + addy8 + 0xFF + 0xFE + 0x02 + 'D' + '1' + bin[0] + 'D' + '0' + bin[1] + 'D' + '2' + bin[2] + 'D' + '3' + bin[3] + 'D' + '5' + bin[4] + 'D' + '4' + bin[5];
xB.write((byte)0xFF - (sum & 0xFF));

What might I be doing wrong? Thanks!


Solution

  • As you can see from the API reference for the Remote AT Command Frame, the bytes after the command (D0) are the value for that command. The XBee module doesn't have a frame type for multiple AT commands, but you can send a series of queued commands, and have them all apply at once when you set the "Apply Changes" bit of the options field.

    So, where you're sending 0x02 right before the AT command, you would send 0x00 for the commands you want to queue up, and only send the 0x02 on the last command to have it apply all changes at once.

    Also, consider writing some routines to simplify your frame sending. For example, a single function that takes a pointer to a buffer with a length could automatically send the start of frame (0x7E 0x00 <length>) and a calculated checksum byte at the end of the frame.