xbee received data not correct

XBee projects like the adapter, xBee tutorials, tweetawatt/wattcher, etc. from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Piggy
 
Posts: 13
Joined: Sun Nov 18, 2012 1:21 am

xbee received data not correct

Post by Piggy »

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


#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

#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);
    
   }

  
 }
 

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: xbee received data not correct

Post by adafruit_support_bill »

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.

Piggy
 
Posts: 13
Joined: Sun Nov 18, 2012 1:21 am

Re: xbee received data not correct

Post by Piggy »

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

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: 13
Joined: Sun Nov 18, 2012 1:21 am

Re: xbee received data not correct

Post by Piggy »

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

#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


#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: 13
Joined: Sun Nov 18, 2012 1:21 am

Re: xbee received data not correct

Post by Piggy »

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!!

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: xbee received data not correct

Post by adafruit_support_bill »

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.

Locked
Please be positive and constructive with your questions and comments.

Return to “XBee products (discontinued)”