Connect Raspberry Pi to PubNub in 2 Steps

2 min read Michael Carroll on Jun 19, 2014

With all the buzz surrounding the Internet of Things, its only natural for us at PubNub to dive right into it. We want to make it as easy and reliable to connect your IoT devices, whether you’re connecting your car, your smart home, or embedded wearables. We like to say we’re a New Network for The Internet of Things.

The Raspberry Pi is a fully functional mini-computer, and is a cheap solution for harnessing the Internet of Things. It includes plenty of inputs and outputs for sensory add-ons to test light, temperature, humidity and more.

This blog post walks you through the steps of connecting a Raspberry Pi to PubNub. This enables you to connect with millions of other devices on our global network and start sending and receiving data between Raspberry Pi devices.

PubNub provides extensive Raspberry Pi documentation and support for the Raspberry Pi . Once you are up and running with the Pi, feel free to choose your favorite language and start connecting. I decided to use Python for this specific project.

Basics of Connecting the Raspberry Pi to PubNub

Follow these steps to get connected:

STEP 1: Connect the Pi to a monitor, keyboard, mouse and ethernet cable. Check out the Raspberry Pi website to set up your Pi. Log in to the Raspberry Pi OS and open IDLE, the python IDE. You’ll also need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal.

STEP 2: Open a new window and paste the following code. Be sure to input your PubNub publish/subscribe keys with demo.

The above code lets you set up the following :

  • publish key using “publish_key”
  • subscribe key using “subscribe_key”
  • UUID using “uuid”
  • channel you wish to subscribe and publish to using “channel”
  • message that you want to send. This is a JSON message

We create an instance of the iotbridge called pi and we can use the SEND and CONNECT methods belonging to that class.

SEND: This invokes the publish() API to send any message over a desired channel.

CONNECT: This invokes the subscribe() API to listen for messages from other embedded devices connected to the same channel. The call back function receiver is invoked when subscribe runs.

You can customize the channels you publish/subscribe to, the UUID you assume and also the messages you send by just changing the above fields.

And huzzah! Thats it! You can now talk Raspberry Pi to any other device or application subscribing/publishing to the same channel.

Check out all the code you need on our Github page to see it in action locally.