Today I was messing around with my Raspberry Pi and wondered how difficult it would be to have my Raspberry Pi talk to my computer (or other devices including other Raspberry Pi’s) using an XBee. If you aren’t familiar with what the XBee is, it is basically a small chip, a little bigger than a quarter, that allows you to communicate wirelessly between devices. It is a chip that can be embedded into pretty much any device and it comes in all kinds of variations. For example, some XBees have high bandwidth, but are limited on their range. Others can communicate over long distances, some even up to 15 miles, but have small bandwidth. I’ve used the XBee in all kinds of projects in the past including an Open Source Cellphone that you can read about here. In most of my projects, I will either use a breakout board or wire the XBee directly to the controller of whatever I’m working on. Even though I can wire the XBee to the Raspberry Pi using the GPIO pins, I wanted to keep this project simple, but still useful. So, I decided to use USB dongles with my XBees. Before going any further, you should checkout my article about wireless communication with XBee and C#. I will be making a few references to that article in this one. After you’ve read that article, come back here and we’ll get started.
To begin with, you will first need a Raspberry Pi. In this article, I will be working with the B Model 2.0 which you can pickup from Amazon for around $35.00. You will also need to purchase an SD card and image it using any of the Raspberry Pi images. For my experiment, I used Raspbian “wheezy” and would recommend you use the same if you want to get the same results I did. For more information on imaging your SD card for use with the Raspberry Pi, checkout my article “Getting Started with the Raspberry Pi“. I would also recommend that you go thru my article called “Raspberry Pi First Boot and Configuration” if you haven’t already.
Once you have your RPi up and running, the next things you will need are 2 XBee’s. You can get any 2 XBee’s you want from Amazon which will run you about $28.00 or so. It’s best if you purchase 2 of the same kind so that you don’t have to do any extra configuration in order for your XBee’s to talk with each other out-of-the-box. While you’re at Amazon, you will also need to pick up 2 XBee Explorer Dongles which will also run you about $30.00 or so. The XBee Explorer Dongle allows you to connect any XBee to any USB device. All you have to do is attach the XBee to the Dongle and plug the Dongle into an empty USB port. For Windows users, you will need to install the drivers for the specific XBee model you will be working with. You can find out more about that in the article I mentioned earlier.
After you have your XBee’s attached to their Dongles, go ahead and connect 1 XBee to your Windows computer and connect the other to your RPi. On the Windows computer, you can use the same Python code that we will be using on the RPi. However, I already wrote a nice little XBee utility using C# that was also explained in the article I mentioned earlier. So, I want to use that same project for this experiment as well. You can download the latest version of the app from http://www.prodigyproductionsllc.com/downloads/XBeeController.v2.0.zip. Download that file to your Windows filesystem and unzip it. There you will find a single EXE file. Double-click to run that application. At the top of that application you will see a dropdown for “COM Port”. Click the arrow next to that field and you should see a list of COM ports that are in use on your computer. Most likely, the XBee will be connected to the last value in the list. To verify this, make a note of which ports are listed. Then, disconnect the XBee Dongle from your Windows computer and see which value disappeared. Plug the XBee Dongle back into your computer and the port should reappear. That will be the port you need to select. So, go ahead and select the port and click the “Open Port” button. If something went wrong, it will tell you. Otherwise, nothing will happen which is OK at this point.
Now that you have your Windows side setup and ready, it’s time to setup the Raspberry Pi. Before we jump into the Python code, we first need to make sure that the RPi is recognizing the Dongle and XBee. To do that, type “lsusb” and press enter. This will print a list of all compatible USB devices attached to the RPi. The device you will need to look for should have a name of something like “Future Technology Devices International, Ltd”. It should also say something like “USB-Serial (UART)”. If you see something like that, then you are ready to move on.
Next, you will need to install the “serial” module for Python which we will be using to communicate with the XBee. To do that, type
sudo apt-get install python-serial
and press enter. Make sure that you have configured your RPi for internet access and that it is currently online as this command will tell Raspbian to go out to the web and fetch the Python module we need. After you have the “serial” module installed, use the “cd” command to change directories into the folder you want to create and execute your Python application on your RPi. For this article, I chose to create a new folder on my Desktop called “xbee“. But, you can create your app wherever you want. Once you’re in the folder of choice, type
and press enter. This will open the vim text editor which we will use for writing our code. If you would rather, you can always use Idle or any other text editor. For now, we’ll stick with vim. To begin writing your code, you will need to press the “i” (lower-case I) key. This will put the editor into “insert” mode at which point you can begin typing. You will need to add the following code for this example:
import serial ser = serial.Serial('/dev/ttyUSB0', 9600) string = 'Hello from Raspberry Pi' print 'Sending "%s"' % string ser.write('%s\n' % string) while True: incoming = ser.readline().strip() print 'Received %s' % incoming ser.write('RPi Received: %s\n' % incoming)
After you have typed in all of the code (or copy-pasted), press the Escape key and type
and press enter. This will tell vim to write the file and exit the editor. At this point you are ready to test your app. To do that, type the following and press enter.
If everything went accordingly, you should see the message “Hello from Raspberry Pi” in the C# application and the message “Got it!” in the RPi screen. The reason you should see the message “Got it!” is because the C# application is set to automatically reply with that message every time it receives a message from someone else. This will demonstrate that the RPi is now capable of sending and receiving messages using the XBee. To test it even further, type a message into the “Data Out” textbox in the C# XBee Communicator app and press the “Send” button. Everything you type in the C# app should also appear in the RPi screen. Here’s an example of both apps in action.
When you are finished testing your app, simply press Ctrl+C on your RPi to terminate your Python app.
That’s it! You now have everything you need for communicating between devices using the XBee and Raspberry Pi.
Here it is in action on YouTube at http://youtu.be/CqDCsFow-y8.
PayPal will open in a new tab.