xbee received data not correct

by Piggy on Sun Nov 18, 2012 1:24 am

I am using one xbee with the Arduino Pro Mini 5 V ATmega328, and the other xbee with the Arduino Pro Mini 3.3 V ATmega328. I'm using the Adafruit xbee adapters. I'm trying to simply send data from one to the other. The value that the arduino serial monitor prints from the receiving xbee is not the value that is being transmitted.
I'm using the SoftwareSerial library with a 9600 baud rate. On the receiving xbee, the red RSSI LED turns on once the transmitter is turned on. I configured the xbees using X-CTU. For each xbee, I set the Destination Address H/L to the Serial Number H/L of the other xbee. I set the baud rate to 9600 and the Pan IDs are the same. I'm just transmitting a constant number "4", and it displays on the receiving side as "134".
Here is my transmitter code:
Code: Select all | TOGGLE FULL SIZE

#include <SoftwareSerial.h>
 
SoftwareSerial xbee= SoftwareSerial(3,8); // RX connected to Xbee TX, TX connected to Xbee RX
 

void setup()
{
  pinMode(3,OUTPUT);
 
  Serial.begin(9600);
  xbee.begin(9600);
}


void loop()
 {
     byte data = 4;
     xbee.print(data);
   
     delay(50);
 }


Here is the receiver code:

Code: Select all | TOGGLE FULL SIZE
#include <SoftwareSerial.h>

SoftwareSerial xbee = SoftwareSerial(10,11); //RX connected to Xbee TX, TX connected to Xbee RX

void setup()
{
  pinMode(11,INPUT);
 
  Serial.begin(9600);
  xbee.begin(9600);
}


void loop()
 {
   
   if (xbee.available())
   {
     byte c = xbee.read();
     Serial.println("val:");
     Serial.println(c,DEC);
   
   }

 
 }
 
Piggy
 
Posts: 4
Joined: Sun Nov 18, 2012 12:21 am

Re: xbee received data not correct

by adafruit_support_bill on Sun Nov 18, 2012 9:00 am

What happens if you send a 5?

The 'print' function will send an ASCII '4', not a binary 4. On the receive side, you are formatting the received value as a decimal number. The decimal value of an ASCII '4' is 34. I'm not sure where you are getting the '1' from.
User avatar
adafruit_support_bill
 
Posts: 28224
Joined: Sat Feb 07, 2009 9:11 am

Re: xbee received data not correct

by Piggy on Sun Nov 18, 2012 2:52 pm

oh oops...I'm formatting it as a character now, but now I get a weird symbol printed. This is how I changed the if statement in the receive loop:
Code: Select all | TOGGLE FULL SIZE
if (xbee.available())
{
     char c = xbee.read();
     Serial.println("val:");
     Serial.println(c);
}


The character printed is a zero with a slash through it...

Thank you!
Piggy
 
Posts: 4
Joined: Sun Nov 18, 2012 12:21 am

Re: xbee received data not correct

by Piggy on Sun Nov 18, 2012 5:55 pm

I changed the code slightly and I'm getting a different result. The value is printed as "-1". On the SoftwareSerial documentation, it says the read will return -1 if no character is available. I don't understand why this would happen if the code is within the "xbee.available" statement. The red receiving LED lights up.

Receiving code:
Code: Select all | TOGGLE FULL SIZE
#include <SoftwareSerial.h>

SoftwareSerial xbee = SoftwareSerial(10,11); //RX,TX

void setup()
{
  pinMode(11,INPUT);
  Serial.begin(9600);
  xbee.begin(9600);
}


void loop()
 {
   
   if (xbee.available())
   {

     Serial.println("val:");
     
     Serial.println(xbee.read());
   
   }

 
 }
 


Transmitting code:
Code: Select all | TOGGLE FULL SIZE

#include <SoftwareSerial.h>
 
SoftwareSerial xbee= SoftwareSerial(3,8); // RX, TX
 

void setup()
{
  pinMode(3,OUTPUT);
 
  Serial.begin(9600);
  xbee.begin(9600);
}


void loop()
 {
     char data = 5;
     xbee.print(data);
     delay(50);
 }
Piggy
 
Posts: 4
Joined: Sun Nov 18, 2012 12:21 am

Re: xbee received data not correct

by Piggy on Sun Nov 18, 2012 11:59 pm

Nevermind, I got it working! I didn't notice that I had two xbee.read statements (I deleted the first xbee.read when I posted the code without realizing its significance) and therefore the second one returned a -1. Thanks for pointing out the issue!!
Piggy
 
Posts: 4
Joined: Sun Nov 18, 2012 12:21 am

Re: xbee received data not correct

by adafruit_support_bill on Mon Nov 19, 2012 7:04 am

You should debug this one step at a time:
    Direct the transmitting side to print to the serial monitor so you know EXACTLY what is being transmitted.
    Then connect the XBees and direct the output of the receiving XBee (no arduino just yet) to the serial monitor to verify that you are receiving EXACTLY what was sent.
    Then connect the receiving Arduino and debug that sketch.
User avatar
adafruit_support_bill
 
Posts: 28224
Joined: Sat Feb 07, 2009 9:11 am