Get an Arduino to wirelessly talk to a Raspberry Pi



This tutorial will show you how to get an Arduino talking to a Raspberry Pi via a wireless link

The hardware setup is very simple;

  • The Pi has a ‘slice of pi’ GPIO breakout board with an XBee module configured as a coordinator on it.
  • The Arduino has an XBee shield with the module configured as a Router/End Point.

Both XBees are configured in API=2 mode which allows for some more advance control. I will try and get around to writing a tutorial on configuring the XBee modules with X-CTU, however the information is out there already.

We will utilise 2 libraries to help with the XBee communication within the programs:

Follow the instructions in those links to download and install those libraries for use.


The Arduino will run a simple program to periodically send a message to the Raspberry Pi.

This code is pretty much lifted from the example provided with the XBee Arduino library that we are using.

First up we need to include the XBee library and create a few variables to use later.
#include <XBee.h>
XBee xbee;
ZBTxRequest zbTx;
So now we have an XBee object xbee to do all the XBee stuff and another object zbTX in which to store our message to transmit.

The xbee object takes a little bit of setting up, the Arduino communicates with it via the serial port so we need to set that up and pass it to the xbee object.
void setup() {
  xbee = XBee();

Now the XBee is all setup we need to configure the message we want to send.
  uint8_t payload[] = { 'H', 'e', 'l', 'l', 'o' };
  XBeeAddress64 addr64 = XBeeAddress64(0, 0);
  zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
Here we created an array of uint8_t’s to hold our message, we also created an address variable to store the address of the XBee module we want to send out message to. In this case we are sending it to address 0. This will send the message to the XBee coordinator in the network (our Pi’s XBee is configured as a coordinator).

All that is left is to get the Arduino to actually send our message.
void loop() {
This will wait 5 seconds and then send the message ‘Hello’, then loop around and do it again..forever!

Raspberry Pi

The Pi will run another simple program, this one written in Python to listen for messages from the XBee module and print them to the screen.

Create a new file called
import serial
from xbee import ZigBee
serial_port = serial.Serial('/dev/ttyAMA0', 9600)
xbee = ZigBee(serial_port, escaped=True)
while True:
  print xbee.wait_read_frame()
This code creates a serial port and an XBee object. The serial port is passed to the XBee module, much in the same way as in the Arduino code.

After that the program goes in to an infinite loop, in which it reads messages from the XBee module, waiting till there is a message available before continuing (known as a blocking read). When it receives a message it will print it to the screen.


That is all the code required. To test it, run the python program on the Raspberry Pi like so:
sudo python
Then power up the Arduino, after 5 seconds you should start seeing the “Hello” message being printed on the Pi.

You can get the code for this tutorial from Github

Leave a Reply

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

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

Facebook photo

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

Connecting to %s