I'm working on a project that using XBee S1 as a transmitter / receiver. These are the hardware I am using:
The purpose of the project is simple, that is to turn on / off the LED. And here is the code (controller):
public static void main(String args[]) throws XBeeException,
InterruptedException {
XBee xbee = new XBee();
boolean running = true;
try {
// OPEN SERIAL PORT
xbee.open("COM10", 9600); // String arg is
// Unique to YOUR
// MACHINE!
Scanner input = new Scanner(System.in);
XBeeAddress16 address = new XBeeAddress16(0x22, 0x22);
int[] payload;
payload = new int[1];
System.out.println("Enter a new command for LED: 0 to turn the LED OFF, 1 to turn ON");
payload[0] = (int) input.nextByte();
TxRequest64 request = new TxRequest64(address, payload);
System.out.println("\nXBee request is: " + request.getXBeePacket());
while (running) {
try {
TxStatusResponse response = (TxStatusResponse) xbee
.sendSynchronous(request, 100);
request.setFrameId(xbee.getNextFrameId());
System.out.println("\nXBee request is: " + request.getXBeePacket());
System.out.println("Response received" + response);
if (response.getApiId() != ApiId.TX_STATUS_RESPONSE)
{
System.out.println("Response error");
}
else
{
System.out.println("Response success");
}
}
catch (XBeeTimeoutException e) {
System.out.println("Me ! Unable to send");
}
System.out.println("Enter a new command for LED: 0 to turn the LED OFF, 1 to turn ON");
payload[0] = (int) input.nextByte();
request.setPayload(payload);
if(payload[0] == 2)
running = false;
}
}
finally {
xbee.close();
}
}
And this is the code for Arduino (end device):
#include <LiquidCrystal.h>
int LED = 12; //Turn this LED on or off, depending on packet rx'd
int debugLED = 13; //Flash light to indicate rx
int packet; //Packet will be two bytes in length
int analogValue;
int debugValue = 0;
int discard;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
pinMode(LED, OUTPUT);
pinMode(debugLED, OUTPUT);
Serial.begin(9600);
packet = 0x01;
lcd.begin(16,2);
}
void loop() {
lcd.setCursor(0, 1);
if(Serial.available() >= 16){ //<--Changing the value from 21 to 16 fixed the problem!
debugValue = 0;
debugValue = Serial.read();
if(debugValue == 0x7e){ //Look for starting byte
lcd.clear();
lcd.print(debugValue);
delay(500);
digitalWrite(debugLED, HIGH); //Flash Arduino LED (not the one on digital pin 11)
delay(1000); //to indicate rx'ing data
digitalWrite(debugLED, LOW);
//lcd.print(debugValue);
for(int i = 2; i != 10; i++){ //Discard unused data (see XBee API protocol for more info)
discard = Serial.read();
if (i != 8)
packet = discard;
// print the number of seconds since reset:
if(i == 5)
lcd.setCursor(0,1);
lcd.print(packet);
delay(500);
}
lcd.print(discard);
}
}
if(packet == 0x7e)
digitalWrite(LED, HIGH);
else
digitalWrite(LED, LOW);
}
The problem is the output in the LCD display always shows same output. How could that be?
These are the 'strange' numbers I got from LCD:
126 0 6 129 125 49 125 49 125 125 29 29
Those values remains the same, tough I've changed the input value from 0 to 1!!
This is the sample output from my Java terminal (console):
1
XBee request is: 0x7e,0x00,0x06,0x01,0x02,0x22,0x22,0x00,0x01,0xb7
Response receivedapiId=TX_STATUS_RESPONSE (0x89),length=3,checksum=0x75,error=false,frameId=0x01,status=SUCCESS
Response success
Enter a new command for LED: 0 to turn the LED OFF, 1 to turn on
0
XBee request is: 0x7e,0x00,0x06,0x01,0x03,0x22,0x22,0x00,0x00,0xb7
Response receivedapiId=TX_STATUS_RESPONSE (0x89),length=3,checksum=0x74,error=false,frameId=0x02,status=SUCCESS
Response success
Enter a new command for LED: 0 to turn the LED OFF, 1 to turn on
I'm sure this is the payload value (marked with ^^^^):
0x7e,0x00,0x06,0x01,0x02,0x22,0x22,0x00,0x01,0xb7
^^^^
But how to get that value with Arduino?
Anyway, I referenced to the tutorial XBee API Project One.
Well, after doing some search, the best way to get XBee's payload value through Arduino is using XBee API for Arduino, as I read this site: wirelessly transmit analog signals using the Arduino XBee API. Here is the code to get the 'payload' value:
#include<Xbee.h>
int DEBUG = 13;
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();
void setup() {
pinMode(DEBUG, OUTPUT);
Serial.begin(9600);
DEBUG = 0;
}
void loop() {
xbee.readPacket();
if(xbee.getResponse().isAvailable()){
lcd.print("Data received");
if(xbee.getResponse().getApiId() == RX_16_RESPONSE){
xbee.getResponse().getRx16Response(rx16);
uint8_t payload = rx16.getData(0);//this is the payload value, easy to get it :D
}
}
}
That is!! :D