The issue was complex (consisted with a few smaller issues). Dumping request and response frames from both ends as @tomlogic suggested was quite helpful.
To resolve these issues I first ensured that hardware part works.
XBee explorer has 4 LEDs. One is power and should always glow red when powered. And 3 others, depending on what explorer you have, may be called rx, tx, rssi
or din, dout, rssi
.
When transmitting code runs, we first need to make sure that rx
and tx
are blinking simultaneously every second. I.e. signal is sent each second.
When receiving code runs and calls readPacketUntilAvailable
, LEDs on receiving XBee explorer should glow as follows: rssi
glows constantly, tx
or dout
should blink.
Now that when we know hardware is fine, we can debug software part.
My issue was with receiving code. When we are setting up receiving XBee, we need to make sure we do two things. First, we call xbee.begin(baudRate)
. And second, we should reset module.
reset = 0;
wait_ms(100);
reset = 1;
wait_ms(100);
And last, but not least thing, readPacketUntilAvailable
method will not reset response. We call this function in a loop. Whatever first response we get will be repeated, no matter what other data is sent by transmitter.
First response that receiver got in my case was MODEM_STATUS_RESPONSE
(0x8A in HEX). And subsequent packets were not read. readPacket
must be called instead, since that method will reset previous response.
My receiving code now looks like this:
XBee xbee(p28, p27);
DigitalOut reset(p11);
DigitalOut mbed_led1(LED1);
int main() {
Serial terminal(USBTX, USBRX);
while(!terminal.readable()) {
wait_ms(10);
}
terminal.getc();
mbed_led1 = 1;
xbee.begin(9600);
reset = 0;
wait_ms(100);
reset = 1;
wait_ms(100);
while(1) {
terminal.puts("Reading packet...\r\n");
xbee.readPacket(500);
XBeeResponse response = xbee.getResponse();
if (response.isAvailable()) {
terminal.puts("Packet available\r\n");
XBeeResponse response = xbee.getResponse();
char tmp[20];
sprintf(tmp, "0x%02X", response.getApiId());
terminal.puts("Response available at API: ");
terminal.puts(tmp);
terminal.puts("\r\n");
uint8_t api = response.getApiId();
if (api == RX_16_RESPONSE) {
Rx16Response rx16 = Rx16Response();
response.getRx16Response(rx16);
uint8_t len = rx16.getDataLength();
char l[20];
sprintf(l, "%d", len);
terminal.puts("We have data of length ");
terminal.puts(l);
terminal.puts("\r\n");
}
}
wait(1);
}
}