Setup:
A - XBee (series 1, ID=1234, MY=1111, AP=2, CE=1) connected via a USB explorer to a computer (Windows)
B - XBee (series 1, ID=1234, MY=2222, AP=2, CE=0) connected to an Arduino Pro Mini (3.3V, 8MHz) via the hardware TX/RX pins. There is an h-bridge coupled to the ProMini that drives a motor forward or reverse based on the signals received through the XBee.
When I had these in AT mode (AP=0), I had full functionality. I could send a command from either X-CTU or a Visual Basic GUI I wrote and the motor attached to the ProMini would behave properly. But I want to have several of these units, and to be able to address them individually instead of as a whole, so I started down the API route. I have communication working, in that I can send a frame through X-CTU to the target XBee and a proper reply appears in the X-CTU console.
I tried writing a sketch that as a start point would merely echo a received payload back to the computer (after confirming all looked good here I will go to work on processing the payload to get the h-bridge to operate accordingly). However, it echos back different values than what I sent.
sent (via X-CTU):
7E 00 0A 01 01 22 22 00 3C 30 30 30 3E AF (essentially the payload is the character string '<000>')
received (in X-CTU) (I broke up the two response frames for readability):
7E 00 03 89 01 00 75 (success message)
7E 00 0A 81 22 22 36 00 53 49 20 52 56 A0 (payload received from unit 2222 is 53 49 20 52 56, or the character string 'SI RV' - a space in there of course)
I'm probably missing something obvious here. But I naturally need to be able to process the correct data to get my motor to behave properly down the line. The sketch:
#include <XBee.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();
const int motorAPin1 = 12; // H-bridge motor control output FIN (pin 3)
const int motorAPin2 = 11; // H-bridge motor control output RIN (pin 5)
uint8_t data[5];
void setup()
{
Serial.begin(38400); // initialize xbee serial communications
xbee.setSerial(Serial);
}
void loop()
{
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
// got an RX16 response
for (int i = 0; i<5; i++)
{
data[i] = rx16.getData(i);
}
Tx16Request tx = Tx16Request(0x1111, data, sizeof(data));
xbee.send(tx);
}
}
}
If instead of sending the data[] array from the rx16.getData call I use
for (int i = 0;i<5;i++)
{
data[i] = 31;
}
I get 1F in my payload response in X-CTU which is decimal 31, so that makes some sense I guess. But before I wasn't even getting the same value for the middle three bytes of the payload which suggests this is more than a matter of me messing up hex vs decimal and all that.
My motor control code ultimately does a switch-case based on a number 0-107, so I will probably change my payload structure sent from the computer to be a single byte (versus 5, especially since with the API mode I can verify I am getting a full packet and not just random crap) that on the Arduino I will convert hex -> decimal and then use accordingly, as opposed to the tokenized 5-character string structure I was using (<000> - <107> so I made sure I wasn't getting garbage, legacy from when I was controlling the motor through direct serial), but first I need to sort out what is going on here.