Internet Connected Petduino - Part 3, Raspberry Pi Setup

Published: 17 December 2015

In our last post we took the first steps in our master plan to get our Petduino connected to the internet by setting up the PetduinoSerial sketch. In this article we’ll set up our Raspberry Pi to connect to the Petduino and proxy all actions and events to and from the internet via Pubnub, a web service that offers real time messaging for devices.

Because our Petduino doesn’t have it’s own internet connection, we need to connect it to something that does. This could very well be your own PC, but in order to create something more standalone we’ll use a Raspberry Pi, as we can setup our script and just have it run as soon as the Pi is switched on, and not require any kind of a monitor or keyboard from that point on. All we will really need is the Pi (with SD card flashed with Rasbian), a Wifi dongle and our Petduino, though we will need a monitor and keyboard for the initial setup.

Before we start with setting up the Raspbery Pi though, we’ll first setup a Pubnub account, and obtain a set of API keys we can use to connect with the Pubnub service.

Head on over to and click the “Get started” link, filling in your details to sign up. Once signed up, login to your account and head to the “apps” section. Pubnub should already have created an app for you, so just note down the various “keys” for that app for later use.

With Pubnub setup, we can now continue to setup our Raspberry Pi. I won’t go through the steps of setting up the OS on your Raspberry Pi as it’s been done many times before, but assuming you have a baseline install setup with Raspbian installed, wifi configured, and the system expanded, updated and upgraded, and you are booted up and logged in to the terminal interface, we’ll start our setup from this point.

First up, we’ll make sure we have the components we’ll need installed. The latest Raspian actually comes with quite a lot of what we will need installed, but one or two items will need installing manually. Start by installing python-crypto by issuing the command:

sudo apt-get install python-crypto

This is a prerequisite for the Pubnub library which we’ll install next by issuing the command:

pip install pubnub

Now that we have the Pubnub library install, we’ll use git to grab (clone) a copy of the PyPetduino library. We do this by issuing the following command:

git clone

This should have created a directory called py-petduino in your home directory which contains the source code for the PyPetduino library, as well as some example python scripts, one of which is a pre-built Pubnub proxy script. This will need a few settings changing however so our next step is to configure these. Open up the script in nano by issuing the following command:

sudo nano py-petduino/src/examples/

This should load up the Pubnub demo script. Using the arrow keys, scroll down to the configuration section and change both the PetduinoSerial port (this should be /dev/ttyUSB0 if you are using a Raspberry Pi, or the com port name if on a PC) details, and enter in the Pubnub subscribe / publish keys we got during Pubnub signup, and then enter a Pubnub channel name. This can be anything, but we’ll need to remember it when we come to setting up NodeRED in the next post. At this stage feel free to scroll around to review the code in the demo, but you’ll see it just listens for actions coming from the Petduino and sends them to the internet, and listens for events coming from the internet and sends them to the Petduino. When done, save your changes by typing CTRL+X, followed by Y then ENTER.

With the our script configured, we can test everything is working by running the script with the following command:

python ./py-petduino/src/examples/

With the Petduino connected, we should be able to see state events show on the console window as our wait animation plays as setup in the previous post, and if we interact with the buttons, we should see the press / release events display as well. To check the Pubnub commands are actually being recived by Pubnub however, we can use the Pubnub debug console (accessible from your app page). With the console open, you should see straight away state change events coming from the Petduino, and if we send an example action payload, we should be able to interact with our Petduino over Pubnub.

For example, the following payload should set the LED on

{ type:”action”, name:”setLed”, value:”1”  }

With the following payload turning it off again

{ type:”action”, name:”setLed”, value:”0”  }

As the last step, we’’ll set the script to run at startup so our Raspberry Pi becomes a dedicated bridge for our Petduino. As this post is already getting pretty long though, and this has been covered many times before, I’ll just point you the following post that explains just how to do this (, just replace their references to the scripT file to our /py-petduino/src/examples/ script.

And with that, we have completed our Raspberry Pi + PyPetduino + Pubnub setup and are ready to start connecting our Petduino to a web service. That, however, will have to wait till the next post.

To be continued…