Talking to the Raspberry Pi using PubNub V4

Receive real-time messages on your Raspberry Pi from a website

Amanda Jarvinen
amandajarvinen
3 min readFeb 12, 2018

--

Note: This post was originally posted in April, 2017.

Purpose of this post

The purpose of this post is to get you started on your way to chatting up a storm with your raspberry pi. I’ll very briefly introduce PubNub and provide some basic code for notifying your Raspberry Pi that a button has been pressed on a website. I may expand on this in the future when I have more time. If you have any questions or suggestions feel free to ask/comment.

Note: This is my first time playing with PubNub so results may vary :).

Tools

Raspberry Pi
I am using a headless Raspberry Pi 3 with Raspbian Jessie Lite installed, ssh enabled and it is connected to the internet. For the code I will be using Python 3.

Website
You’ll need to create a PubNub account and have a basic understanding of HTML and JavaScript.

PubNub?

PubNub is a programmable network for developing realtime applications; an evolution from three-tier architecture, purpose-built to handle all the complexities of data streams: securing them, ensuring a realtime experience, guaranteeing reliability, and scaling them to any number of devices, anywhere in the world. ~ How PubNub Works, PubNub

Basically for the purposes of this post, PubNub allows us to stream data in real-time over the PubNub network. We are using the publish/subscribe message pattern for bidirectional communication. The example code has the website publishing a message to a channel, which the raspberry pi is subscribed to. The raspberry pi receives the message interprets it and prints ‘Button press was received…’ to the console.

Create a PubNub account

  1. Navigate to www.pubnub.com and create an account.
  2. Create a new app.
  3. Copy the publish key and subscribe keys into the code below.

Website

Edit the code below to include the publish and subscribe keys. Also specify a channel name, try to keep it short. Then launch your site.

I have tested it on a flask-server. If you are interested in flask I recommend checking out Miguel Grinberg’s blog and the YouTube video’s by Tony D from Adafruit.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PubNub Example</title>
</head>
<body>
{# The Button #}
<button id="button_id">Press Me!</button>

{# PubNub JavaScript SDK #}
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.8.0.js">
</script>

{# Press button to publish #}
<script>
$(document).ready(function(){
$("#button_id").on("click", function(){

var channel = 'channel-name-goes-here';

var pubnub= new PubNub({
subscribeKey:'subscription-goes-here',
publishKey: 'public-key-goes-here',
});

alert("Test: Send message to pi");//Testing only
pubnub.publish({
channel: channel,
message: 'on'
});
});
});
</script>
</body>
</html>

Raspberry Pi

Setup

Install python 3, pip and virtualenv. Create a directory for storing your python code, create a virtual environment, activate it and then install PubNub. I found that PubNub had no interest in cooperating if I didn’t specify pip3.4 as I ran into IncompleteRead issues.

sudo apt-get update
sudo apt-get install python3
sudo apt-get install python3-pip
sudo pip3 install virtualenv
mkdir pbexample
cd pbexample
virtualenv env
source env/bin/activate
pip3.4 install pubnub
nano examplecode.py

With examplecode.py open in nano, copy and paste the code below. Edit the code to include your keys and the channel name then save. To save in nano press Ctrl+X and enter Y for yes.

Note: If you only intend on subscribing to a stream you do not need to include the publishing key.

Code

from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.callbacks import SubscribeCallback
pnconfig = PNConfiguration()pnconfig.subscribe_key = 'subscription-goes-here'
pnconfig.publish_key = 'public-key-goes-here'
pubnub = PubNub(pnconfig)channel = 'channel-name-goes-here'
class Listener(SubscribeCallback):
def message(self, pubnub, message):
if message.message == 'on':
print('Button press was received...')
print('Listening...')
pubnub.add_listener(Listener())
pubnub.subscribe().channels(channel).execute()

Run

To run the code on your raspberry pi enter the following into the cli:

python3.4 examplecode.py

Have fun clicking the button and seeing ‘Button press was received…’ appear on your pi!

Relevant Documents

PubNub JavaScript Documentation
PubNub Python Documentation
Raspbian Jessie Lite
Virtualenv

--

--

Amanda Jarvinen
amandajarvinen

UX. Design. Software. Making. Always learning and eager to explore the world of tomorrow.