IN THIS ARTICLE
This blog post will show you how to integrate basic pub/sub messaging for your Raspberry Pi, connecting it to any other device(s) with an Internet connection. This could be a UI on a laptop or phone, another Raspberry Pi, or remote modules. In this case, using the Python programming language.
If you want to get every last ounce of performance out of your Raspberry Pi, you may want to code in C. We got you covered in the same tutorial here.
We assume that if you are reading this article, you already have a Raspberry Pi 2 and you’re ready to get started building realtime embedded projects using PubNub. We also assume that you have the device running with your favorite OS.
In this tutorial we used Raspbian, however, using another distribution should work as well. Just make sure you use the proper package manager instead of apt-get in the case that you are not using a Debian-based distribution.
Note that Snappy Ubuntu Core is also available now on Raspberry Pi 2. It should work fine with this tutorial – if you find any issues, let us know on the community forum!
The last thing you’ll want to make sure you have is a proper internet connection. Plug in that Raspberry Pi (or enable WiFi) and you’re ready to roll!
To get your unique publish/subscribe keys, you’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Admin Dashboard. Our free Sandbox tier should give you all the bandwidth you need to build and test your app with the PubNub API.
Installing Libraries & Dependencies
At this point, you’ll want to have the necessary libraries to start coding with PubNub. Run to your terminal and use your package manager, and if you are not sure if you are up to date, you can start by running:
Next step is to get those libraries:
Now, let’s install PubNub.
Let’s get the code from GitHub:
How PubNub Works
Let’s move on to running an example to make sure it works. We will write two simple pieces of code. One that publishes a message on a PubNub channel, and another which subscribes to the same channel.
You can picture a PubNub channel like a room. When you want to say something to the people in the room, you publish a message. On the other hand, if you want to listen to what people are saying in the room, you simply subscribe to the channel.
Open your favorite editor and let’s start coding.
Publishing On A Channel (Sending Messages)
Let’s create a file called send.py:
We simply import PubNub and initialize it using our publish and subscribe keys (remember, you got your publish/subscribe keys earlier in the blog post).
All we need to do now is to publish a message on a channel:
At this point we should be able to send some messages. Let’s write the code to receive it!
Subscribing To A Channel (Receiving Messages)
Our new file will be called listen.py. The beginning of the code will be the same as in our previous code: import and initialize PubNub.
When we subscribe to a channel, we are notified by events through callbacks we need to implement. In our case, we’ll just print the code as we receive these messages.
Now let’s subscribe to the channel!
And that’s it. Let’s try it out.
Testing our Code
Open two terminals. Start with running the listen.py. In the other terminal, send messages with our send.py.
And that’s it! Hopefully this gives you a feel for the power of what’s possible for building connected applications using Python and PubNub on Raspberry Pi 2. We’ve got a ton of other tutorials and demos for Raspberry Pi, from Raspberry Pi 101, to home automation, to realtime weather stations. Check out them all here!