User Tools

Site Tools


xbee:remote_blink

XBee Remote Blink Tutorial

The Adobe Flash Plugin is needed to display this content.

Summary

Control a LED wirelessly from a computer. This tutorial uses XBee series 2 radios, a Fio or Funnel IO board (wireless oriented Arduino board with XBee socket and battery connector) but any other Arduino with XBee shield/breakout would do, and a XBee Explorer USB board connected to a laptop. An application on the laptop presents a GUI where a mouseOver event will send serial data (0 or 1) over wireless to the Fio board. The Fio listens to serial data, and turns a LED accordingly.

ZigBee or XBee? series 1 or 2, 2.5 ???

It can be confusing, and they are often used interchangeably. Here is a little clarification:

  • ZigBee is a specification/standard for a suite of high level communication protocols using small, low-power digital radios. Think of Bluetooth but for low-power devices. wikipedia
  • XBee is the brand name from Digi International for various radio modules.
  • XBee Series 1 or XBee 802.15.4, the original XBee radio, quite simple to use, does point to point and point-to-multipoint.
  • XBee ZNet 2.5, is actually a Series 2 module with older firmware. Series 2 and 2.5 are compatible via firmware updates.
  • XBee Series 2 or XBee ZB, uses the ZigBee mesh networking protocol. The latest version available from Digi.
  • Pro versions, for all series, have just increased output power capabilities, thus longer range.

Check this XBee comparison matrix. XBee Series 1 and Series 2/2.5 are not compatible. Check carefully which version your are using/buying.

In this tutorial, we are using XBee Series 2 (non Pro) modules.

Configuring the XBee radios

We want our radio modules to talk to each other, so we have to configure them accordingly. They need to be on the same network (PAN ID 55 in our case). We then set the Coordinator to talk to the Router, and the Router to the Coordinator, via the Destination Address settings. Addresses on XBees are in 64 bits format, thus come in two parts, low (first 32 bits) and high (last 32 bits). The high value is common to all XBees manufactured by Digi, at 0013A200. Each XBee radio module has its own unique Radio Serial Address. You can read this value using the X-CTU software (see screenshots below). This is necessary to know when we are configuring the Destination Address, to which radio we want to talk to. Using the X-CTU software on a PC and the XBee Explorer USB board, configure the radios as the following:

Coordinator

  • firmware Zigbee Coordinator AT
  • PAN ID 55
  • DH 0013A200
  • DL 404A61E1 (or Serial Low of your Router)

Router

  • firmware Zigbee Router AT
  • PAN ID 55
  • DH 0013A200
  • DL 403E2112 (or Serial Low of your Coordinator)

Setup the remote Router node, using the Fio board

We first starts by programming the Fio board with code to blink a LED on pin 13 upon receiving data over serial (xBee radios replace the usual USB cable now). Note that you need an external USB-to-Serial board, like the FTDI Basic 3.3V from Sparkfun, to program the Fio. The mini-USB port is only for power + charging the battery. You have to remove the XBee Radio from the backside socket when programming the Fio.

Arduino code for the Fio

int incomingByte = 0;	// for incoming serial data
 
void setup() {
  Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
  pinMode(13, OUTPUT);
 
  // blink twice at startup
  digitalWrite(13, LOW);
  delay(1000);
 
  digitalWrite(13, HIGH); // first blink
  delay(50);
  digitalWrite(13, LOW);
  delay(200);
  digitalWrite(13, HIGH); // second blink
  delay(50);
  digitalWrite(13, LOW);
}
 
void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
 
    if(incomingByte == '0'){
      digitalWrite(13, LOW);
    }else if(incomingByte == '1'){
      digitalWrite(13, HIGH);
    }    
    // say what you got:
    Serial.print("Fio received: ");
    Serial.write(incomingByte);  // Arduino 1.0 compatibility
    Serial.write(10);    // send a line feed/new line, ascii 10
  }
}

When done, disconnect the FTDI Basic board, insert the Router radio in the back socket and plug your battery.

Sending data from the computer via Coordinator

At this point, it is best to first check that the radio link between the XBees modules works properly. To test, we will use a terminal application and type manually 0 and 1. X-CTU has a convenient Terminal tab, so we'll use it. Insert your Coordinator radio into the XBee Explorer USB board, plug it to the computer and launch X-CTU. Choose the Terminal tab, toggle the Open/Close Serial Port button to open the port, and start typing 0 and 1. Check that LED on the Fio board turns on and off accordingly. You should see the typed text and the response from the Fio board as in the screenshot below. If not, check all your connections, and power cycle the Fio board.

One you know that data is going across and is properly turning on and off the LED, you can quit X-CTU and launch Processing. Only one application can access a serial port, so make sure you close all instances before opening it again in another application. You can run this simple Processing sketch and check that the LED turns on and off as the mouse moves over the square.

Processing code for the GUI interface

import processing.serial.*;
 
Serial myPort;  // Create object from Serial class
int val = 0;        // Data to send
 
void setup(){
  size(200, 200);
  println(Serial.list());
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}
 
void draw(){
  background(255);
  if (mouseOverRect() == true) {  // If mouse is over square,
    fill(255, 0, 0);               
    val = '1';
  } 
  else {    
    fill(127);               
    val = '0';
  }
  // send val to serial port  
  myPort.write(val);
 
  rect(50, 50, 100, 100);
}
 
boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
 
// manual trigger with mouse press
void mousePressed() {
  myPort.write('1');
}

You might notice that the response speed is not that great. Why is that so? The Processing application sends 0 or 1 (1 byte) very rapidly (~60 fps or more), and for each transmission, the Fio responds with some text (~20 chars/bytes) acknowledging the data received. This return data stream consumes bandwidth (20 bytes * 60 = 1200 bytes or 9600 bits per second) on the radio link. We now actually overfills the data channel we set at 9600bps in our radios. As the return text is not really useful once we know that the radio link works, we might as well avoid it. Go back to the Arduino sketch for the Fio and comment the few lines of code for the response, or just delete them.

    // say what you got:
    // Serial.print("Fio received: ");
    // Serial.write(incomingByte);  // Arduino 1.0 compatibility
    // Serial.write(10);    // send a line feed/new line, ascii 10

Plug the FTDI Basic into the Fio, take out the XBee and upload that new sketch to the board. Once done, reconnect your Fio for radio operation, and try again with the Processing sketch. It should be pretty responsive now.

Questions, problems and comments, email me camille.moussette@dh.umu.se

Camille Moussette
September 2011

xbee/remote_blink.txt · Last modified: 2013/05/24 16:18 by camille