Sending commands from one XBee to another XBee mounted on Arduino Uno

So here’s the setup.

The goal of this project is to send a serial command from the XBee on the Dongle Board connected to a computer to the XBee on the Arduino Uno and have it turn the relays on and off.

First, the XBee setup.  XBee’s should work out of the box and, for the most part, they do.  I did have some trouble getting it setup to talk to each other.  The key turned out to be the setup of the destination address and the source address on the XBees, which interestingly enough, makes a little bit of sense.  The sender must have a source address set to the same value as the destination address on the receiver.  Go figure.

Configuring the XBee is actually quite easy.  Using XCTU old-gen (not the Next Gen version.  It didn’t work AT ALL) to connect to and configure the boards, I configured one at a time on the Dongle first.  We will configure them with the following settings:

Setting Value Notes
XBee A: The Receiver on the Arduino Uno PAN ID 3332 This is the default, but you can make it whatever you want,as long as they match on both XBee’s. This is the name of the Personal Area Network they are connecting to.
DL 1 This is the destination address setting on the XBee. The receiver has to have the destination address set that the sender will send comands to. This can be another number other than 1 if you’d like, as long as it matches the source on the sender XBee.
XBee B: The Sender on the Dongle PAN ID 3332 Again, this is the Personal Area Network that both XBee’s will connect to, so this must be the same.
MY 1 This is the source address and must match the destination address setting of the receiving XBee.

After you open XCTU, connect the first XBee to the Dongle and plug it in.  You will need to determine which COM port it is using via the Windows Device Manager.  Mine used COM6.  First, you have to add the COM port on the PC Settings Tab.

Add Port
Add COM port 6 to connect to the XBee.

Next, test the connection.

Test Connection
You should get back the XBee’s information when you test

Now, we need to configure the settings by first reading the settings, changing the ones you need to, then writing the changes to the XBee.

Configure XBee
Read, Update, then Write to save the changes to the XBee config.

After the XBee’s are both configured, connect one of the XBee’s to the relay shield mounted on the Arduino Uno and upload the sketch below.  I’ll describe what the sketch does first.  The relay shield has two relay modules.  I connected the relay on pin 5 to the top plug of a standard wall outlet plug from Home Depot, and the relay on pin 4 to the bottom plug.  More on the plug setup in the next post, but for the purposes of this code, you can hook anything you want up to the relay to connect power when the relay is on.  This post only covers sending a command to set a pin HIGH or LOW, which you can now do.  Well, after you upload the sketch.

And now, the sketch…

#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3); //RX, TX
const int relayOne = 4; // the pin for the first relay
const int relayTwo = 5; // the pin for the second relay
char incomingByte; // a variable to read incoming serial data into
void setup(){
  pinMode(relayOne, OUTPUT);
  pinMode(relayTwo, OUTPUT);
  xbee.begin(9600);
}
void loop(){
  while (xbee.available()) {
    incomingByte = xbee.read();
    // if it's a capital F (ASCII 72), turn on the first relay:
    if (incomingByte == 'F') {
      digitalWrite(relayOne, HIGH);
    }
    // if it's a lowercase F (ASCII 76) turn off the first relay:
    if (incomingByte == 'f') {
      digitalWrite(relayOne, LOW);
    }
    // if it's a capital S (ASCII 72), turn on the second relay:
    if (incomingByte == 'S') {
      digitalWrite(relayTwo, HIGH);
    }
    // if it's a lowercase s (ASCII 76) turn off the second relay:
    if (incomingByte == 's') {
      digitalWrite(relayTwo, LOW);
    }
  }
}

One thought on “Sending commands from one XBee to another XBee mounted on Arduino Uno”

Leave a Reply