1

So I have a couple of XBee S2Cs, and I have configured them properly--they can in fact communicate with each other. I am using them with Arduino Unos and this shield: https://store.arduino.cc/usa/shield-xbee-without-rf. I am attempting to interface with the XBees with SoftwareSerial, but for whatever reason, they are instead using just regular Serial. Anything I put on the SoftwareSerial appears to be ignored entirely, while anything I put on Serial appears in my serial monitor and also is transmitted to the other XBee. I have this code for transmitting:

#include <SoftwareSerial.h>
SoftwareSerial xbee (2, 3);
void setup() {
    Serial.begin(9600);
    xbee.begin(9600);
}

void loop() {
    xbee.println("hello");
    Serial.println("world");
    delay(5000);
}

And this code for receiving:

#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3);

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

void loop() {
    if (xbee.available() > 0) {
        Serial.println("xbee: " + xbee.readString());
    }
    else {
        Serial.println("no xbee data");
    }

    if (Serial.available() > 0) {
        Serial.println("serial: " + Serial.readString());
    }
    else {
        Serial.println("no serial data");
    }
    delay(5000);
}

Every 5 seconds, I get this output through the serial monitor on the transmitting side:

world

and this on the receiving side:

no xbee data
serial: world

I also get the string world when I connect a third XBee to my XCTU console.

So I have a couple of questions:

First off--how is this happening? I'm no Arduino expert, but as far as I can tell from what I've read, if the XBee is capturing my serial output, then I shouldn't also be able to see it in the serial monitor.

Second off--how can I get the SoftwareSerial to work? I could theoretically just use Serial forever, but that would make debugging way more trouble, and would probably create a code-maintenance headache later. If I had to guess, I might need to use different pins for my shield, but I have no idea how to find out what pins are the right ones to use.

Update: Based on what I've seen and read, it looks like the particular combination of the Uno with this shield leaves me with no options but to use the hardware serial. A different shield or Arduino board would grant me options to separate my serial output from my xbee output, but that does not appear to be an option here.

  • 1
    If that shield has the xbee wired to the hardware serial pins (0 and 1) then no amount of creating software serial instances is going to rewire it. There isn't much information on that shield in the link, is there a way to change what pins it is using? – Delta_G Jul 2 at 20:46
  • That could be the case. I have no idea how I would change that--I haven't found much info on the shield either, and I'm not enough of an electrical guy to delve into it without. – jordan767 Jul 2 at 21:14
  • The big lesson may be, "Always check for documentation BEFORE plunking down your money." No matter how cool some device might seem, if it doesn't come with some sort of instructions then it is pretty much useless. – Delta_G Jul 2 at 21:25
  • What position are the jumpers in? Read this: arduino.cc/en/Main/ArduinoXbeeShield – Majenko Jul 2 at 21:54
1

To debug the sketch with SoftwareSerial, remove the jumpers from JP2 and JP3 and wire (with jumper wires) the middle pin of JP2 and JP3 to SoftwareSerial pins of your choice.

Middle pin of JP2 is TX of the Xbee so wire it to RX of SoftwareSerial. Middle pin of JP3 is RX of XBee.

On that particular board, JP2 is the jumper closer to the middle of the board, while JP3 is closer to the edge.

shield's schematics

1

You will need an xbee breakout board Xbee breakout board or this one from Sparkfun to do that...

Make sure you check if the board comes with headers as they are not always included. Note also that the xbee socket is not a standard size - which is why you need the breakout board - it converts the XBee pin to a standard size. Here are the 2mm headers required for xbee and regular 0.1" headers required for connection to a breadboard or jumper wires etc.

Actually, I used leonardo for the very reason that you asked about in your question. Leonardo has two serial ports, one is for the usb connection you are familiar with on Uno, but it is not connected to pins 0 and 1.

The other is a completely independent serial port that is wired to pins 0 and 1 on leonardo.

So on Leonardo, you can do Serial.println("Debug msgs"); and Serial1.println("xbee messages"); (note the digit 1 in Serial1). Of course other platforms such as Mega also can be used in the same way.

I am assuming you already have some sort of an XBee shield that allows you to plug the XBee directly into your Uno. The beauty of the Leonardo is that you can continue to use that sheild. The XBee will "automatically" be connected to Serial1.

FWIW, i had all sorts of trouble with s/w serial. I couldn't get it to work with my XBees at all. I could never work out why it did not seem to work.

As soon as i moved to leonardo, it worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.