Raspberry Pi as an Xbee Wireless Sensor Network Gateway

Notes about Raspberry Pi, an ARM based pc about the size of a deck of cards which costs $35. I might use it as a replacement for the Asus WL520 GU wifi modems I’ve been using as an Xbee WSN Gateway.


General References


I started out with the 4G SD Card from Newark that was pre-loaded with wheezy-debian. Now I’m using the distro from Adafruit called Occidentalis v0.2.  It includes ssh and other features that makes it easier to confugure.

Copy the image to a 4G SD Card using Win32DiskImager.  Basically you download an OS image to a windows machine and copy it to an SD Card.  When using WinDiskImager, pay close attention to your read/write actions because it’s possible to overwite the wrong drive.

Before selecting an SD Card, look at the list in Verified Peripherals.  Not all SD Cards work the same, and I spent a lot of time trying to launch the OS even though the image copy was successful.  If you get to the point where you’ve successfully copied the image but it won’t boot up, cut your losses and try another SD Card.

  • login as: pi, password: raspberry
  • I set up a root account and did most of the installation as root

Expanding the partition on the sccard

Once installed, expand the SD Card partition to fill the 4G memory space. The image on the 4G sdcard is 1.8G. This describes how to expand the partition to fill the entire 4G.


Set up SSH so you can access the Raspberry Pi from a terminal program of via Putty and/or WinSCP.

ssh-keygen -t rsa -C "your_email@youremail.com"

XBee connection

I used the CISECO daughter board kit which costs about $6 which has a GPIO connector and a place to hook up an xbee to the serial port on the GPIO.  It derives the 3.3V from the GPIO and it includes an array of through holes for misc prototyping.

  • XBee Radio PCB daughter board– by CISECO – basically a breakout board for the GPIO connections with access to the serial port and 3.3V.
  • Getting started with GPIO and PythonThis is the first of two articles showing basic GPIO on the Raspberry-Pi using the prototype area of the Slice of Pi. This covers basic details on the GPIO pins, setting up a Python library to allow access to the GPIO. There is an example circuit to build on the Slice and some code to get the outputs working. This was originally a blog post on Matts blog at http://lwk.mjhosting.co.uk


Raspberry Pi with XBee daughterboard, Slice of Pi

Raspberry Pi with XBee daughterboard, Slice of Pi

On the Adafruit distro, Occidentalis v0.2, I was having some issues with reading the serial port in my python code.  I kept getting erros related to the serial port being in use.  Some research got me to this article on the RaspberryPi.org site and this article from Clayton’s Domain.  I edited a file, /boot/cmdline.txt, and this is what it came down to (all on one line):

dwc_otg.lpm_enable=0 rpitestmode=1 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait

Install Apache on Raspberry Pi

Eventually I will want to set up a web interface to allow users to edit the gateway config items, like the network protocol and to manage the various sensors, python scripts, etc.  For now, I’m just going to manage it it by ssh command line.

  • http://tinkernut.com/wiki/page/Episode_320
  • Webmin– “Webmin is a web-based interface for system administration for Unix. Using any modern web browser, you can setup user accounts, Apache, DNS, file sharing and much more. Webmin removes the need to manually edit Unix configuration files like /etc/passwd, and lets you manage a system from the console or remotely.”
  • Raspcontrol– PHP based Dashboard application. More about status then configuring.

Installing Python components

Rasberry Pi, default linux install, has python already installed.

I needed these modules to allow me to access various web services like Cosm (pachube), Thing Speak, Open.Sen.se, and my own SOAP based service that I wrote in C#.


We need some extra libs to run the sensor programs.

Check if python is installed and what version is installed

python -V

Change directory:

cd /home/tinaja/downloads/

WGet the file then unpack it:

wget http://sourceforge.net/projects/pyserial/files/pyserial/2.5/pyserial-2.5.tar.gz/download
tar -zxvf pyserial-2.5.tar.gz

cd pyserial-2.5
python setup.py install

simplejson 2.6.1

simplejson 2.6.1 is compatible with python 2.5.

Change directory:

cd /home/tinaja/downloads/

WGet the file then unpack it:

wget http://pypi.python.org/packages/source/s/simplejson/simplejson-2.6.1.tar.gz
tar -zxvf simplejson-2.6.1.tar.gz

cd simplejson-2.6.1
python setup.py install


The suds libs provide services for SOAP calls.

Need to install python’s setup tools:

apt-get install python-setuptools

Change directory:

cd /home/tinaja/downloads/

wget https://fedorahosted.org/releases/s/u/suds/python-suds-0.4.tar.gz
tar -zxvf python-suds-0.4.tar.gz

cd python-suds-0.4/
python setup.py install


Used by Cosm (pachube)

? git clone git://github.com/petervizi/python-eeml.git

# cd /home/tinaja/downloads/
# wget http://pypi.python.org/packages/source/p/python-eeml/python-eeml-1.2.0.tar.gz
# older version - wget http://pypi.python.org/packages/any/p/python-eeml/python-eeml-1.1.0.linux-i686.tar.gz
# tar python-eeml-1.2.0.tar.gz
# python setup.py install


I’ve loaded the python code I’m using on GitHub:


The Raspberry Pi can be wifi enabled with a USB based wifi device. The main concern would be with power consumption; the raspberry pi runs off of a 5V (currently 700mA) supply (like a cell phone charger) and a wifi device might strain the load.

For now (8.26.2012) the wired ethernet configuration works great.  It can just plug into a IP provider modem.

To change the boot-up text

Did this to brand the OS as one I set up.  Only for show…

edit /etc/moto.tail

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

Type 'startx' to launch a graphical session

This version, from Adafruit's Occidentalis v0.2, set up by:
 _____ _              _         _          _         
|_   _(_)_ __   __ _ (_) __ _  | |    __ _| |__  ___ 
  | | | | '_ \ / _` || |/ _` | | |   / _` | '_ \/ __|
  | | | | | | | (_| || | (_| | | |__| (_| | |_) \__ \
  |_| |_|_| |_|\__,_|/ |\__,_| |_____\__,_|_.__/|___/
                   |__/ TinajaLabs.com, Summer 2012

To set a program to start at boot-up

To automatically run the main python script, allsensors.py, when the computer starts…

Edit /etc/rc.local and add the python line shown below:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"

# add this for Tinaja Labs sensor tracking
python /home/tinaja/allsensors.py &

exit 0

To set up Tomcat Server

At some point I might want to set up a Tomcat server and use it with some Java apps.  Later.


To watch the message log file

This allows you to see the system messages generated by the script as it sends various sensor readings to the web services.

cd /var/log/
tail -f messages

To kill the Tinaja python script

Look for the process id of the python script with this command:

ps aux # for a full list
ps aux | grep python  # to see only python processes

Take note of the process id number and issue this command:

kill 9999 # where 9999 is the process id number

That’s all I have for now, hope you found something useful here.


18 thoughts on “Raspberry Pi as an Xbee Wireless Sensor Network Gateway

  1. Pingback: Raspberry Pi as an Xbee Wireless Sensor Network Gateway #piday #raspberrypi @Raspberry_Pi « adafruit industries blog

  2. I am very interested with this tutorial. I am not good in programming from scratch. Do you have any idea if I using mutiple arduino+ xbee, how can a send a data to cosm/pachube? Any script I can refer to?

  3. Hi I’m hoping you can help me out, i want to build an automation network and your setup looks ideal for the ideas i want to try. However not being a Programmer (willing to learn) I’m looking for the simplest way to build the system. I would like to control various parts to the system With my android phone eg(Alarm on phone triggers sunrise lamp, start morning routine etc.) i thought about trying to use tasker with http commands, but im just looking for the simplest place to start. Like the minimal services I need to run on my PI server. Not looking for a complete system design but just some advice. Thanks very much

    • Adam,
      One of the things to consider is what is known as the internet of things (aka, #IOT). This concept refers to the connectivity we get by using the internet protocols as the means of communicating between devices.

      You mentioned using the alarm on your android. If there is a way to trigger a web url call from your alarm app, this would be excellent. Perhaps Tasker is the app which can provide this functionality.

      The next thing to hook up is a web server on the raspi, like apache. You could then write a service using php (here’s the coding) to respond to the call from your android alarm trigger. This php code could trigger an output on the raspi GPIO pins to flip a relay, which in turn, turns on a lamp, or starts the coffee pot.

      Here’s an interesting article that might help with the web service approach:
      Other search results: http://goo.gl/xkEdO

      You can find variations on a theme at the Adafruit Learning system site here:

      The Adafruit Cobler kit will probably help get you started.

      I used a GPIO board that was ideally suited to accessing the serial output from an xbee radio. The Cobbler connection is a general purpose device that I think will be useful for your project.

  4. Thanks so much for the info, indeed a great place to start, eventually i would like to communicate with xbee/arduino modules,( assuming that ill need to use python scripts to communicate to the arduinos) will control be possible with php? . I am planning to use arduinos as temp/touch sensors and also to control relays. The reason I want to use tasker is that its simple yet powerful way to create your own apps. I’ve seen an example on instructables.com where they use apache, php5 and mysql they build a webpage that can control the gpio, so I want to use tasker for some control as it has an option to get or push to http and can use the data as variables. I could just be over thinking it and perhaps just accessing the control from the phones browser for when i want control makes more sense. That way the pi can run the automation and the android will just be for updating settings, timers etc.

    Not sure If that makes sense, to clarify 1. Can my server communicate both ways with my wireless xbee cards/arduinos?

    2. Is the best way to send commands through apache/php?

    3. Should tasker work to fill a field with a variable in my website that my server can use?

    I guess that your probably not familiar with tasker I’m just wondering if that sounds like it would work. Sorry for taking up so much of your time, thanks very much.


    • I think tasker should be a great way to trigger events on the raspi server (I call it a gateway device; first iterations used a wifi router with OpenWRT, python). Tasker should be able to send data to a web service.

      All the code in my projects lives in the gateway and at the moment, all python. The python script reads the serial port, polling for data from the various xbee radios, bundles up the data and sends it out to web services like cosm, thingspeak, open.sen.se.

      My sensor boards do not have an arduino or microcontroller. They have a voltage regulator, an xbee radio, and up to 4 sensors each. At some point I might need a microcontroller, but I’m trying to keep the code centrally located in the gateway device. It’s clearly not the only way to go, but it’s my way for now. Keep as much of the logic in one central location/device within the house.

      I struggled with communicating both ways with the xbee. But I did finally make it work. Look up the link to github on this site. There are some python samples for controlling a servo.

      Alternative to apache/php: I recall seeing a python script that acted as a web service but can’t remember where. Perhaps a google search will find it.

      • Thanks I definitely will keep looking. Thanks for taking the time to help out.

  5. Hello,
    I just wanted to add an “Amen” to the idea of using Arduino/XBees as the Coordinators/End Devices and RPi as the Gateway/Router! The ideal would then be to configure a web-based Dashboard using Javascript to view sensor data and control functions. With the variety of web services out there like ThingSpeak, COSMetc… and mobile “Context Aware” apps like Tasker, its going to be very important to have one single dash to connect to all of them, rather than using everything separately.
    One idea I had was to use the Processing IDE to configure Sketches that could output to Arduino (which is based on Processing), Android (to the ADK), Javascript (Web app) and Java (Desktop PC). All thats missing is an extension for the RPi through Python. This is just an idea, so I have no clue if it will work or not…

    Another question I had is more specific to the RaspiGateway project. I’m using Occidentalis v.2 on Adafruit’s fantastic WebIDE which allows you to clone directly from Git Repo’s. I was able to clone the RaspiGateway Repo but have not had any success in installing “pySerial” or any of the other library extensions.
    When I send “python -V” it returns “Python 2.7.3rc2″ but when I type ” cd /h
    ome/tinaja/downloads/” this is what it returns-

    “webide@raspberrypi /usr/share/adafruit/webide/repositories/gateway_raspi $ cd /h
    bash: cd: /home/tinaja/downloads/: No such file or directory”

    Is this because I’m trying to access from within the WebIDE or am I running a newer version of Python? Or is it something else?

    Thank you, and let me just say I appreciate what you are doing here greatly!

  6. Pingback: Raspberry Pi as an Xbee Wireless Sensor Network Gateway | drmegaramp

  7. Pingback: XBee กับ Raspberry Pi | Raspberry Pi Thailand

  8. Have you done any work defining the gateway using the zigbee stack so that the gateway will properly define itself and what it does to other controllers that implement the stack? Basically one sided so that it will be interoperable with a variety of controllers just right out of the box using the Zigbee Application Profiles and Application Framework?

  9. How to configure Zigbee API mode on raspberry pi . I am trying to add the com port of my zigbee on Apache Tomcat server file but it gives an error …… plz help,,,,,,,,thanks in advance

  10. Very well presented but needs urgent updating to allow for the Jessie Pixel version of RPi Debian.

    I would love to see another article on RPi and PC Wireless communication in a wireless (not WLAN)

  11. Hello Buddy,

    I’ve just come across your project and I have some questions if you dont mind answering!

    I’m undertaking a similar project but unsure whether the hardware I have is correct or whether the method that I’m using is!

    I have :
    1 x XBee V3
    1 x Rapsberry Pi 3
    1 x Temp Sensor (https://uk-m.banggood.com/New-Arrival-Original-Xiaomi-Mini-Smart-Home-Temperature-and-Humidity-Sensor-White-p-1046061.html?gmcCountry=GB&currency=GBP&cur_warehouse=CN&createTmp=1&utm_source=googleshopping&utm_medium=cpc_bgcs&utm_content=garman&utm_campaign=pla-gbg-brand-xiaomi-mb)

    My plan was to setup the XBee as a co-ordinator whilst the sensor acts as an end point device. After searching the web, I’ve only seen examples where they utilize 2 Xbees. The sensor I have (temp) operates using the ZigBee protocol. Do I need another XBee (as a router)? According to the ZigBee manual, this is not required as end points can connect to co-ordinator. How do I connect the sensor to the XBee? Just a basic idea on how to connect.

    When would your code come into play?

    Sorry for the login winded request

    Kind regards

    Waqas Tabassam

    • Apologies for a delayed response. As you can see this is from about seven years ago. ;)

      My setup has evolved. I still use the xbee series 1 devices. I have them distributed around my home and they are collecting temperature and light levels and sending to an xbee on the raspberry pi. (working on an update that uses the wemos D1 mini; uses wifi)

      The xbee devices are configured to point to the gateway xbee which is attached to a raspberry pi computer which is running the latest version of raspbian lite. On that gateway raspi I have a python program running that converts the data into an MQTT publish protocol. The endpoint on that is another raspi that’s running node-red, a coordination piece. It’s a really useful component and I recommend it for centralizing all the coordination all your home automation components.

      I see they provide a wifi + xbee gateway device that I assume translates from the xbee sensor to wifi platform. Beware a lot of these systems only use a mobile app and there are often a lot of machinations to make a custom connection. Many also only allow configuration by utilizing a cloud application which makes me nervous about what kind of data they’re sneaking out of my home.

      Finally, if you build on this platform, look at which xbee device they are using in their gateway. It could be one of a few varieties, like series 1 or series 2 and might be using a particular version of the associated firmware.

      Have fun.

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 )

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