0

I am new to this and I am trying to make a simple program that will send a command to the arduino and then send a response back to java via xbee. I am able to send command to arduino but I am unable to read a response from it. I am using XBee S2C with API-2 configuration. How can I read the response from the arduino in my java?

Here is my code for java:

public class Transmitdataxbee {
    private static final String PORT = "COM8";
    private static final int BAUD_RATE = 9600;
    private static RemoteXBeeDevice myremote;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE);
        Scanner input_rpi = new Scanner(System.in);
        String data;

        try {
            myDevice.open();
            XBeeNetwork myXBeeNetwork = myDevice.getNetwork();
            myXBeeNetwork.setDiscoveryTimeout(10000);
            myXBeeNetwork.startDiscoveryProcess();

            while (myXBeeNetwork.isDiscoveryRunning()) {
                System.out.println("Discovering devices...");

            }
            myremote = myXBeeNetwork.getDevice(new XBee64BitAddress("0013A20041768E48"));
            String nodeIdentifier = myremote.getNodeID();
            System.out.print("Node ID: ");
            System.out.println(nodeIdentifier);

            System.out.println("Enter Command");
            data = input_rpi.next();

            myDevice.sendData(myremote, data.getBytes());

            System.out.println("Current timeout: " + myDevice.getReceiveTimeout() + "milliseconds");

            //read from arduino
            XBeeMessage edMessage = myDevice.readDataFrom(myremote);
            String data_ed = edMessage.getDataString();
            System.out.println(data_ed);



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

and this is my code for arudino

#include <XBee.h>

XBee xbee = XBee();
ZBRxResponse rx = ZBRxResponse();
XBeeAddress64 test = XBeeAddress64(0x0013A200, 0x41768E6E);

ModemStatusResponse msr = ModemStatusResponse();

uint8_t data;
char cmd1[9];
String cmd;
char d_ata;
int j = 0;
int icount = 0;
int count = 32;

void setup() {
     Serial.begin(9600);
     Serial2.begin(9600);
     xbee.setSerial(Serial2);
     xbee.setAPImode(2);
     Serial.println("Connecting....");
}

void loop() {
    xbee.readPacket(50);
    if (xbee.getResponse().isAvailable()){
        Serial.println("Connected");
        Serial.println("Getting Message...");

        if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
            xbee.getResponse().getZBRxResponse(rx);
            Serial.println("Packet received!");

            if(rx.getOption() == ZB_PACKET_ACKNOWLEDGED){
                Serial.println("Packet acknowledged");
            }
            cmd = "";
            Serial.println("Received Data: ");
            for (int i = 0; i < rx.getDataLength(); i++) {
                //print8Bits(rx.getData()[i]);
                cmd1[i] = (char) rx.getData()[i];
                cmd += cmd1[i];
                Serial.println(cmd);
                Serial.println();
            }
            if (cmd == "a") {
                data = "l";
                ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
                xbee.send(zbtx);
            }
            else if (cmd == "w"){
                data = "u";
                ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
                xbee.send(zbtx);
            }
            else if (cmd == "s"){
               data = "d";
               ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
               xbee.send(zbtx); 
            }
            else if (cmd == "d"){
                data = "r";
                ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
                xbee.send(zbtx);
            }
            else{
                data="e";
                ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
                xbee.send(zbtx);
            }  
        }
    } else if (xbee.getResponse().isError()) {
       // some kind of error happened, I put the stars in so
       // it could easily be found
       Serial.print("************************************* error code:");
       Serial.println(xbee.getResponse().getErrorCode(),DEC);
    }
}
Maël Pedretti
  • 718
  • 7
  • 22
  • I am not sure at all but the problem might come from the part when you read datas from the arduino. You try to read right after sending which might not work. Is there a way to wait on `myDevice` to have available datas? – Maël Pedretti Sep 20 '18 at 07:49

1 Answers1

0

After a few reasearches I found a piece of documentation from Digi that might help you.

As I thought in my first comment, the problem appears to come from the part where you try to read datas from the device.

According to this documentation : https://www.digi.com/resources/documentation/digidocs/90001438/reference/r_xb_java_lib_data_reception_callback.htm

You have to create a datalistener an register it to your XBeeDevice.

They even give you a few hints about XBeeMessage information.

I don't know your Java level but all the code is provided in the link above.

If you want to read more about listeners, check this : What is the purpose of a listener in Java?

Maël Pedretti
  • 718
  • 7
  • 22