Reading data over serial Arduino and XBee

Changing int to byte is really changing int to char. The non-ASCII symbols are a result of trying to render the character (0b11111111). Negative one (-1) in decimal is all ones in binary because int's are signed by default. Check out Bin/Dec/Hex Converter to verify.

All of that is to say that xbee.read() returns a byte/char. I was not able to find anything in the documentation, but I would assume that the -1 is due to an error (based on the hardware Serial documentation). This is because there is nothing to read.

You could try the following:

  • Ensure the RX/TX lines are correct. Trust me, it happens.
  • Check to see if the XBee has data available prior to reading. (You'll get a lot less lines printed since it will wait until a byte is ready to be read.)
if (xbee.available()) {
    byte temp= xbee.read();
    Serial.print(temp);
}
  • Use the built in (hardware). SoftwareSerial should work, but in my experience, hardware serial is much more reliable.
    • Depending on your model(s) of Arduino, you may have to (Disable Auto Reset on Serial Connection). This appears to be only needed if you're trying to send data through the FTDI chip from somewhere other than the IDE's Serial Monitor (generally speaking).
  • This thread Arduino to Arduino XBee Serial Communication has a very similar setup that appears to be working. Simplify your effort as much as possible and then slowly add functionality.
  • Directly connect the XBee RX & TX lines to a USB-to-FTDI connector, such as this cable or this breakout board.

Until you have a working proof of concept, you should make it as simple as possible. Once it is working then add features one at a time. This seems like what you're doing already, but this could probably be simplified further (cut the Arduinos out of the equation by using FTDI only, use hardware serial, etc.).

It sounds like a pretty cool project. Good luck!

Tags:

Arduino