3

I have 2 XBee S1 modules configured in API mode 1. XBee module 1 is a transmitter of a signal and XBee module 2 is a receiver of a signal. And the issue is that receiver does not receives anything from transmitter (or so it seems).

Transmitter configuration is this:

AP 1
MY 1
ID 1984
DL 2
CH C

Everything else is left by default. Transmitter is attached to XBee Explorer. XBee Explorer is connected to PC via USB.

Following piece of code sends signal each 1 second:

public class Main {

    private Main() {
        XBee xbee = new XBee();
        try {
            xbee.open("/dev/ttyUSB0", 9600);
            final XBeeRequest request = new TxRequest16(new XBeeAddress16(0, 2), new int[] { 1 });

            //noinspection InfiniteLoopStatement
            while (true) {
                xbee.sendSynchronous(request);
                Thread.sleep(1000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            xbee.close();
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}

Java XBee API library was used: https://code.google.com/p/xbee-api/ And I see TX LED flashing every second.

Receiver configuration is this:

AP 1
MY 2
ID 1984
DL 1
CH C

Everything else is left by default. Following wiring for receiver is used: enter image description here

Receiver is attached to the breadboard via XBee Explorer. Continuity test shows that current flows fine between XBee RX and mbed P27, as well as between XBee TX and mbed P28.

And following code on an mbed (LPC1768) runs to receive packets:

Serial terminal(USBTX, USBRX);

while(1) {
    terminal.puts("Reading packet...\r\n");
    xbee.readPacketUntilAvailable();
    terminal.puts("Packet available\r\n");

    XBeeResponse response = xbee.getResponse();
    if (response.isAvailable()) {
        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: ");
            terminal.puts(l);
            terminal.puts("\r\n");
        }
    }
    wait(1);
}

Popular Arduino/mbed library for an XBee API mode was used. Sources are located here: http://mbed.org/users/okini3939/code/XBee/

And the output of a console is: Reading packet... for all times. And RX LED is not flashing on receiver.

oddy
  • 1,890
  • 2
  • 16
  • 26
  • can you connect both to the PC? – Keith Nicholas Jan 01 '14 at 06:00
  • the other think to make sure of is they are both on the same channel – Keith Nicholas Jan 01 '14 at 06:02
  • @KeithNicholas Receiver does not have USB connector. Only transmitter does. As for the same channel, triplet consisting of MY, ID and DL are the only options that need to be set. They are set correctly (as per documentation). One other detail is that if embedded solution will start sending packets and PC will start listening (i.e. if I swap transmitter and receiver), then everything goes well. – oddy Jan 01 '14 at 07:10
  • both are receivers and transmitters, and the xbee can operate on multiple channels, I use them for talking to various things and tend to set them on different channels to avoid interference – Keith Nicholas Jan 01 '14 at 07:13
  • @KeithNicholas I see. Thank you. I'll check CH option. – oddy Jan 01 '14 at 07:22
  • @KeithNicholas I have updated my question with channel option value. They share the same channel, C. – oddy Jan 01 '14 at 07:27

3 Answers3

1

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);
    }
}
oddy
  • 1,890
  • 2
  • 16
  • 26
  • Looks like you have a duplicated call `XBeeResponse response = xbee.getResponse();` both outside and inside of your `if (response.isAvailable())`. – tomlogic Jan 06 '14 at 17:51
  • @tomlogic Indeed, I missed that bit :) Fortunately, getResponse is a plain getter that returns private variable stored by successfull readPacket execution. And thus does not cause any harm. – oddy Jan 07 '14 at 01:37
0

Note that because you're running in API mode, ATDL isn't used so you shouldn't need to set it.

The Java library you linked to is documented as only working with ATAP=2. Try changing that setting on the transmitting node.

You should be dumping any packets that come in on the transmitter. The XBee module should generate a "Transmit Status" (0x89) frame that can aid in debugging.

tomlogic
  • 11,489
  • 3
  • 33
  • 59
  • It didn't work. Although, it was a great tip and I appreciate it. What I have now after I did all your suggested fixes is this (raw response packet): [0x0, 0x3, 0x89, 0x1, 0x1, 0x74] I.e. I changed API mode to 2 and dumped response as you suggested. – oddy Jan 02 '14 at 09:34
  • I am not sure if I read this packet properly, but it looks like after frame ID I have status 1, which is "An expected MAC acknowledgement never occurred."? – oddy Jan 02 '14 at 11:23
  • @oddy, is it possible to dump the outbound request before sending it? I don't have a huge amount of experience with the S1, but you can use the XBee documentation to see what you're actually asking in the outbound request. Note that the Tx Status of 0x01 lets you know that the packet hasn't arrived on the remote XBee, so you can focus on debugging the wireless communications instead of investigating possible host serial communication failures on the receiver end. – tomlogic Jan 02 '14 at 17:45
  • Glad you were able to debug and isolate the issue, and then figure out the cause. – tomlogic Jan 06 '14 at 17:51
0

I think the easiest way to read data from the Xbee is just to open the serial port in a separate thread and constantly loop for 0x7E (the start delimiter). That is what I'm doing with my code and it works fine. I used the RxTxSerial library and this tutorial to get it started: http://embeddedfreak.wordpress.com/java-serial-port-trail/ I don't have my code resetting the xbee or having to wait to be able to receive the data properly. If you would like to have a look at my code then I will gladly share and explain it.

jigglypuff
  • 507
  • 1
  • 7
  • 20