Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data over serial Arduino and XBee

Tags:

arduino

I am having the following:

Two Arduinos and two XBees. I want to send data from the one to another. The XBees communicate, because I have the proposes test (connect one XBee with the Arduino and the other to the PC, write from the one, and watch the other in the other terminal).

Now I want to send data from the one to another:

These are my two scripts:

For sending (which is tested in the former test that sends all the letters):

#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3); // RX, TX
char c = 'A';
int  pingPong = 1;

void setup()
{
    Serial.begin(9600);

    Serial.println( "Arduino started sending bytes via XBee" );

    //Set the data rate for the SoftwareSerial port
    xbee.begin(9600);
}

void loop() {
    // Send character via XBee to other XBee connected to Mac
    // via USB cable.
    xbee.write( c );

    //--- Display the character just sent on console. ---
    Serial.println( c );

    //--- Get the next letter in the alphabet, and reset to ---
    //--- 'A' once we have reached 'Z'.
    c = c + 1;
    if ( c>'Z' )
         c = 'A';

    //--- Switch LED on Arduino board for every character sent---
    if ( pingPong == 0 )
        digitalWrite(13, LOW);
    else
        digitalWrite(13, HIGH);
    pingPong = 1 - pingPong;
    delay( 1000 );
}

The problem is when I connected an Arduino to receive data from the other XBee.

Here is my code:

#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3); // RX, TX

void setup()
{
    Serial.begin(9600);

    Serial.println( "Arduino started receiving bytes via XBee" );

    // Set the data rate for the SoftwareSerial port.
    xbee.begin(9600);
}

void loop()  {
    int temp = xbee.read();

    Serial.print("Character received:");
    Serial.println(temp);
    delay(1000);
}

Output is always:

Character received: -1.

If I change the temp from int to byte I see Character received: (a non-[ASCII][3] symbol).

I am using XBee series 1.

They are configured through X-CTU, based on the one tutorial found on ladyada.net.

Then I connected the XBee to an Arduino (TX to pin 3, RX to 2, Vcc and GND respectively) and the other XBee to the PC through an FTDI cable. I was able to send characters from the Arduino and see them in the serial monitor of the X-CTU. Does this mean that they are configured correctly?

Then I wanted to connect an Arduino to my receiver. You can see the code above. I am always getting no available data.

Returned -1 means that there is no data in the serial.

like image 212
ghostrider Avatar asked Jan 07 '13 15:01

ghostrider


People also ask

How does Arduino communicate with XBee?

Connect VCC (pin 1) of XBee module to 3.3V of Arduino Nano and GND (pin 10) of XBee to GND of Arduino Nano. These two connections make up for powering the transmitting side XBee module. Connect Dout (pin 2) to D2 of Arduino Nano and Din (pin 3) to D3 of Arduino Nano.

Can Arduino read serial?

You can use the Arduino environment's built-in serial monitor to communicate with an Arduino board. Click the serial monitor button in the toolbar and select the same baud rate used in the call to begin() . Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board).

What is XBee Arduino?

The Xbee shield allows an Arduino board to communicate wirelessly using Zigbee. It is based on the Xbee module from MaxStream. The module can communicate up to 100 feet indoors or 300 feet outdoors (with line-of-sight).

How do I use XBee modules?

Download the XCTU software from here. Run the program and connect the XBee Explorer USB board with your computer. Click on the "Discover devices" icon to add your xBee in the XCTU software. Now click on it (first image above) and set the CH field to e.g. "C" and the ID field to e.g. "1001".


1 Answers

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!

like image 92
ZnArK Avatar answered Sep 30 '22 17:09

ZnArK