Xbee Pro S2B to Xbee Pro S2B(Adruino) communication problem

Hallo,

I am currently struggling with a comm’s problem. I have just started with Arduino and xbee. I have configured my xbee’s accordingly (Co ordinator AT and Router AT). Their PAN ID’s is the same and I can do a simple chat between the two xbee’s. But when I connect the co ordinator to my Arduino mega, and try to run my little program it does not work. I need the Arduino to turn on and off an LED when I send a “1” and “2” form my laptop. Both the Arduino and the xbee is connected to the same Laptop.

The program on my Arduino is:

#include <SoftwareSerial.h>

SoftwareSerial xbee (2,3);

int myData = 0;

const int ledPin = 13;

void setup()

{

Serial.begin(9600);

xbee.begin(9600);

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, HIGH);

delay (2000);

digitalWrite(ledPin, LOW);

delay (2000);

Serial.println(“Ready\n”);

}

void loop()

{

xbee.listen();

while(xbee.available()>0)

{

int myData = xbee.read()-‘0’;

if(myData == ‘1’)

{

digitalWrite(ledPin,HIGH);

Serial.write(myData);

}

if(myData == ‘2’)

{

digitalWrite(ledPin,LOW);

Serial.write(myData);

}

}

}

What am I doing wrong?

Please assist.

Regards

Jaco

Why are you using the software serial library when you have an Arduino Mega with 4 hardware serial ports?

Read up which UART pins are connected to what: http://arduino.cc/en/Main/arduinoBoardMega

p.s.

while(xbee.available()>0)
 {
 int myData = xbee.read()-'0';

What do you think the result of myData becomes when you sent the character ‘0’?

 if(myData == '1')
 {
 digitalWrite(ledPin,HIGH);
 Serial.write(myData);
 }
 if(myData == '2')

And what integer value do you think that ‘1’ and ‘2’ means to the Arduino compiler?

P.S. 2

Use a serial monitor application to show the characters as hexadecimal values, at the same time as their normal representation. Standard Arduino serial monitor doesn’t allow this. Plenty of serial terminal software on the web to download. But XCTU is something you already should have and can do this too.

Hi Valen,

Thank you for your reply.

I did read the link you have send. I can see it has more TX/RX ports. How do I link them to the xbee on the shield? I have two shields a sparkfun shield and a xbee pro shield.

Regard to the other questions “What do you think the result of myData becomes when you sent the character ‘0’?” I tried to make the variable an integer by subtracting ‘0’ because, I want the output to be written as an integer. “And what integer value do you think that ‘1’ and ‘2’ means to the Arduino compiler?” The compiler should see it as an integer(a number) being send over the xbee to turn the light on and off.

I have been working with XCTU. But what ever i send over the xbee connected to the laptop, does not get to the arduino to be displayed. I can do a simple chat between the xbee’s but i can not seem to get information to be send to the arduino and to be displayed by the arduino. So i think the problem could be my programming.

Jaco Pretorius:
Hi Valen,

Thank you for your reply.

I did read the link you have send. I can see it has more TX/RX ports. How do I link them to the xbee on the shield? I have two shields a sparkfun shield and a xbee pro shield.

I don’t know the details of the schematic of those shields, and don’t have the time to investigate right now. Sorry, maybe later.

Regard to the other questions “What do you think the result of myData becomes when you sent the character ‘0’?” I tried to make the variable an integer by subtracting ‘0’ because, I want the output to be written as an integer. “And what integer value do you think that ‘1’ and ‘2’ means to the Arduino compiler?” The compiler should see it as an integer(a number) being send over the xbee to turn the light on and off.

I have been working with XCTU. But what ever i send over the xbee connected to the laptop, does not get to the arduino to be displayed. I can do a simple chat between the xbee’s but i can not seem to get information to be send to the arduino and to be displayed by the arduino. So i think the problem could be my programming.

It is an integer indeed. But what values do they represent? And how does that influence the decisions made in the if-statements.

If you send a character ‘1’, and subtract ‘0’ from it, can it then still be considered to have the same value as ‘1’? You may be thinking by the logic of 1-0=1, but you are wrong here.