Connect Raspberry Pi to PubNub in 2 Steps

2 min read PubNub Staff on Jun 19, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

How to get Raspberry Pi up and running with PubNub quickly.

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 the PubNub Data Stream Network. This enables you to connect with millions of other devices on our global data streams 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.

More from PubNub

Top Trends to Engage Your App Users
InsightsSep 27, 20224 min read

Top Trends to Engage Your App Users

Take a look at the top trends that are the most effective in attracting customers and reducing churn, and how you can incorporate...

PubNub Staff

PubNub Staff

Comparing Game Engines: Unity vs Unreal vs the Rest
Real-Time Gaming BlogSep 21, 20225 min read

Comparing Game Engines: Unity vs Unreal vs the Rest

Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...

PubNub Staff

PubNub Staff

Python Socket Programming: Client, Server and Peer Libraries
BuildSep 21, 20226 min read

Python Socket Programming: Client, Server and Peer Libraries

Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...

PubNub Staff

PubNub Staff