Wireless communication using an XBee radio

The XBee family of products contain a wide variety of hardware versions of the radio, as well as a list of available firmwares to modify how the XBee radio communicates with other XBees. We used a Series 1 XBee with the XBEE 802.15.4 firmware to create collective network of XBees. The Series 1 was the original version of the XBee radio, and the easiest to set up.

Generally, the firmware that these radios use enable them to use peer-to-peer communication, meaning that each specific radio communicates with another radio, in a one-on-one manner.

Series 1 XBee communication example

The way this communication is set is that the XBee settings are modified using the XCTU utility, a program from Digi, the company that makes XBee. Using this, the destination address (where any messages sent using the radio will go) can be modified, as well as the source address, which represents the address of the radio being programmed. For example, if the source address of the radio is set to 0, and the destination address is set to 1, the radio will receive any messages sent to 0 and send any outgoing messages to 1. Since each address is established as a 16 bit number, locations can range from 0-65535.

To test this communication, I set a Series 1 XBee radio with the following settings:

Sample XBee settings

I left the channel setting alone, since changing from the default really doesn’t help for much, but I did change the PAN ID to avoid interference from other possible XBee networks. The PAN ID sets which radios are on the same network, so it’s a good idea to change from the default in case other XBee enthusiasts are close by and haven’t changed their settings. To do this, chose any 4 hexadecimal characters (0-9, A-F) to set your PAN ID. I chose (0xCAFE), because who doesn’t like a cafe? The Destination Address High sets the value of the first 8 bits of the Destination Address, and the Destination Address Low sets the value of the last 8 bits of the Destination Address.

The other XBee was given identical settings, expect I swapped the Destination Address Low and Source Address.

At this point, the radios are fully configured to talk to each other. To test this, we can use the XCTU Console by clicking on the monitor undefined icon. This will bring up a page that looks like the following:

By pressing the undefined button, XCTU opens a serial connection with the radio module, allowing characters to be sent by typing them into the Console log. This converts the input to a hexadecimal value based on the ASCII value of the character, shown in the right window, and sends it to the XBee radio to be broadcast. Text entered by the user appears blue in the console, and text received by the XBee appears red.

To make this configuration more mobile, the XBees can be used with an Arduino to eliminate the need for a wired connection to the microcontroller for input. The wiring diagram is shown below, but only four connections are given: ground to ground, 5V to 5V, DOUT to RX, and DIN to TX.

XBee connected to Nano

To communicate from the Nano to the XBee, all that is needed is to print out messages normally through the serial port. The code I used is shown below, along with a brief explanation of what it does.

#define LED 13  //Replaces any instance of LED with 13 (for lighting up an LED)
//#define BLINK

int incomingByte = 0; //Defines a variable to use to store each incoming byte

void setup() {
  Serial.begin(9600); //Opens a serial port at 9600 bits per second
  pinMode(LED, OUTPUT); //Defines the LED pin as an output
  digitalWrite(LED, LOW); //Initializes the LED to off
  delay(100);
}

void loop() {
//  Serial.print('0');
  short_blink();  //Each cycle, the LED will blink for easier debugging
  while (Serial.available()) {  //If there is data waiting in the serial buffer, do some stuff
    incomingByte=Serial.read(); //Store the first (or only) byte in incomingByte
    Serial.print("Recieved: ");
    Serial.println(incomingByte, HEX); //Sends the hex value of the incoming byte back
#ifdef BLINK  //If BLINK is defined, the LED will blink 3 times for each incoming byte. This will delay the program though, so beware
    for(int i=0; i<3; i++){
      short_blink();
    }
#endif
  
  delay(1000);  //Delays each cycle significantly in order to see what is happening
}

void short_blink(){
  //This program blinks once
  digitalWrite(LED, HIGH);
  delay(250);
  digitalWrite(LED, LOW);
  delay(250);
}

This program basically just parrots back whatever is sent to if. For example, if the character ‘t’ is sent using the XBee connected to the computer running XCTU, the nano will send back “Recieved: 74”, because 74 is the hexadecimal equivalent of 116, which is the ascii value for t.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s