Raspberry Pi LogoToday 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 runDigi XBee Pro 900 XSC U.FL 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.

xbee_dongleAfter 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

vi xbee_test.py

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

:wq!

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.

python xbee_test.py

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.

Raspberry Pi Python XBee Test

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.

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.
$2.00
$5.00
Other

Related Posts

Tagged with:  

7 Responses to Add XBee to Raspberry Pi with Python

  1. Javier117 says:

    Thanks so much =D

  2. yeowhong says:

    Hi,

    I followed your tutorial, but i only managed to get it talking in one-way communication.

    Setup:
    1. 1 x XBee S2 Pro configured in Coordinator AT mode connected to Raspberry Pi running on Python
    2. 1 x XBee S2 Pro configured in Router/Endpoint AT mode connected to Windows Computer
    3. Both residing in the same PAN ID

    Problem:
    When the Raspberry Pi received the message from the Windows Computer, I don’t receive the acknowledment message back on the Windows Computer.

    Is there anything that I’ve done wrongly?

    Thanks!

    • LuCuS says:

      If you’re using my example C# app and the Python code above, it should be working. Make sure you include the \n (backslash n) at the end of your messages. This tells the other end that it has sent the entire message. Also, make sure you have the C# app listening before you start the Python app, as shown in the video, since the Python app sends its message to Windows as soon as it starts up. Inside the while-loop in Python, you can also include the following code which allows you to manually enter new messages to send to Windows.

      string = raw_input(“Enter message to send to Windows: “)
      ser.write(‘%s\n’ % string)

  3. A7Xchevas says:

    Hi… what should I do if I want to use the chat with 2 raspberrys and the PC?

    • LuCuS says:

      If you leave the XBee radios with their default settings, you should be able to just add the radios to both Pi’s and the computer. The radios will already (out-of-the-box) be programmed to transmit and receive on the same channels. If it doesn’t work out-of-the-box for you, you’ll need to use XCTU to configure one of the radios to be the coordinator and make sure they’re all speaking on the same channel.

  4. A7Xchevas says:

    another question how I need to modify the code if a want to also respond from the raspberry to visual studio application?

  5. xuandong says:

    hi

    what should i do if i wan to use the incoming data as a condition so that i can execute something else?

    thank you!

Leave a Reply