Home/Support/Support Forum/How to properly send a message from Arduino to Java using Xbee-S2

How to properly send a message from Arduino to Java using Xbee-S2

0 votes
Hello everyone. I am currently testing out the Digi Xbee Java library and working through the examples. I've been able to send data from the Router xbee (located on pc) to the Coordinator xbee (located on arduino) and succesfully turned on/off an led, this was done in Java. Now I am trying to send a message from the arduino side to the pc side and display the message in my java app. However, I have not had any luck. I'm pretty sure the problem is within my arduino code and if someone could let me know what I am doing wrong I would be grateful.


The following is my Arduino code, It's purpose is just to send out messages.
Code:
#include <SoftwareSerial.h> SoftwareSerial XBee(2, 3); // Arduino RX, TX (XBee Dout, Din) void setup(){ XBee.begin(9600); pinMode(13, OUTPUT); } void loop(){ delay(1000); digitalWrite(13, HIGH); XBee.print("Hello, can you hear me"); delay(1000); digitalWrite(13, LOW); }

The java files are the examples located in the communication section of the api and the links to them are as follows:

Main:
http://pastebin.com/AmcUzVHr
Listener class:
http://pastebin.com/qdhPTcBS
asked Jun 18, 2015 in RF Solutions and XBee by Fito New to the Community (0 points)

Please log in or register to answer this question.

1 Answer

0 votes
Hi,
Your code seems to be fine. Please change arduino RX,TX pins to (0,1) and try as these are default pins.

Please provide some more information about the hardware you are using for establishing communication between Arduino and XBee module like Arduino XBee sheild.
answered Jun 19, 2015 by Avinash Babu New to the Community (4 points)
Thanks for taking a look at my code. I am using a Sunfounder uno board with the Sparkfun Xbee shield, the coordinator xbee is located on this shield. My uno board is powered by a 9v power supply. On the PC side I have the sparkfun xbee explorer usb connected with my router xbee.

I would like my Java application to receive a string sent by the coordinator xbee on the uno board. The application is able to send broadcast messages so I can send out data.

My XBee's are also in API mode.
Hi,

Please paste the following code in main method of java class and check what you are getting in the console.
try {
myDevice.open();
myXBeeNetwork = myDevice.getNetwork();
myXBeeNetwork.setDiscoveryTimeout(15000);
myXBeeNetwork.addDiscoveryListener(new DiscoveryProcess());
myXBeeNetwork.startDiscoveryProcess();
myDevice.addDataListener(new MyDataReceiveListener());
System.out.println("\n>> Discovering remote XBee devices...");
devicesList = myXBeeNetwork.getDevices();
for(int i = 0 ; i < devicesList.size(); i++){
RemoteXBeeDevice xbee = new RemoteXBeeDevice(myDevice,devicesList.get(i).get64BitAddress());
System.out.println(devicesList.get(i).get64BitAddress().toString());
System.out.println(devicesList.get(i).getPANID().toString().getBytes());
System.out.println(devicesList.get(i).getXBeeProtocol());
myDevice.sendData(xbee, data.getBytes());
}
} catch (XBeeException e) {
e.printStackTrace();
myDevice.close();
System.exit(1);
}

Please let me know wether you are getting coordinator address on console or not.
I entered the code you provided and this was the result:

WARNING:  RXTX Version mismatch
    Jar version = RXTX-2.2pre1
    native lib Version = RXTX-2.2pre2

>> Discovering remote XBee devices...
>> Device discovered: 0013A20040CAE669 -  
>> Discovery process finished successfully.

The address I obtained from your code is my coordinator address. I've also been trying to implement the XBee arduino library since I am trying to communicate in API mode.
Hi ,
The code which you are using for Arduino is AT mode. To establish communication in API mode using XBee-arduino library, refer the following link : https://code.google.com/p/xbee-arduino/
Sorry for the late response and thank you very much for the help you have provided me. I have been looking over that library but I am still having trouble sending a response to the Java app. The following is my arduino code:

#include <XBee.h>

XBee xbee = XBee();

uint8_t payload[] = {'H','I'};
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x40CAA93E);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));

void setup() {
  Serial.begin(9600);
  xbee.setSerial(Serial);

}

void loop() {

  xbee.send(zbTx);

  if(xbee.readPacket(500)){
    pinMode(13, HIGH);
  }
  
  delay(1000);
  pinMode(13, LOW);
}

I am using the same listener code that I originally posted.
Please I have the same problem how I must configure my xbee beceause in  the xbee arduino library the xbees must be configured in AP=2 and for the xbee java library the xbees must be configured in AP=1 please any help ?
...