Search code examples
javaapiembeddedxbeezigbee

Xbee communication using Digi xbee-java api


I have configured one xbee pro as coordinator (API mode) and other as router (API mode). I trying to send data from coordinator to router using xbee java api, but in the router code i keep getting null, am I doing something wrong. Below is the code for Sending data (coordinator):

public class MainApp {
private static final String PORT = "/dev/ttyUSB0";
private static final int BAUDRATE = 9600;

public static void main(String[] args)
{
    String data = "Helloww";

    XBeeDevice mycord = new XBeeDevice(PORT, BAUDRATE);     

    try {
        mycord.open();
        System.out.println("Port is opened\n");
        System.out.println("remote device connection\n");
        //mac of my router
        RemoteXBeeDevice router = new RemoteXBeeDevice(mycord,
                new XBee64BitAddress("0013A20040DD9BDD"));
        System.out.println("Sending data\n");
        mycord.sendData(router, data.getBytes());

    } catch (XBeeException e) {
        e.printStackTrace();
        mycord.close();
        System.exit(1);
    }
}

}

code on router side

public class RecvApp {
private static final String PORT = "/dev/ttyUSB1";
private static final int BAUDRATE = 9600;

public static void main(String[] args)
{
    XBeeDevice myrouter = new XBeeDevice(PORT, BAUDRATE);

        try {
            myrouter.open();
            System.out.println("router port opened\n");
            //mac of coordinator
            RemoteXBeeDevice remotecord = new RemoteXBeeDevice(myrouter, new XBee64BitAddress("0013A20040D96FE5"));
            XBeeMessage msg = myrouter.readDataFrom(remotecord);
            System.out.print(msg);

        } catch (XBeeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            myrouter.close();
            System.exit(1);
        }
      }
   }

Solution

  • Found the issue, I was not converting the message received in the correct format. Added the below lines

    String content = HexUtils.prettyHexString(HexUtils.byteArrayToHexString(xbeeMessage.getData()));
        System.out.println("Hex data" + "" + content + "\n");
        String value = new String(xbeeMessage.getData());
        System.out.print("Actual msg" + " " + value + "\n");
    

    Works now :)