Getting Started with Raspberry Pi 2 and PubNub in C

4 min read Michael Carroll on Jul 21, 2015

Attention embedded Linux fans – in early February 2015, Raspberry Pi released a new generation mini-computer, the Raspberry Pi 2! It looks a lot like the previous model, but don’t be fooled, it is 6 times faster than its older version!- To celebrate this great event, we decided to give you a little jump start to get up and running using PubNub with the Raspberry Pi 2, in this case, using the C programming language.

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.


We assume that if you are reading this article, you already have a Raspberry Pi 2 and you’re ready to get started building real-time embedded projects using PubNub. We also assume that you have the device running with your favorite OS.

In this tutorial we used Raspbian, but it should also work using another distribution. 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 

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!


Getting Keys

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:

You’ll probably be prompted to also install these packages dependencies, make sure you do.

Alright, now we should be able to install PubNub!

Getting PubNub

Let’s get the code from GitHub, or download the PubNub C SDK here.

Nice. Now let’s get in that “c” folder, and install this:

Testing The Magic

No problem so far? We can move on to running an example to make sure it works! Let’s find it, build it and run it!

How does it look? You should be getting something like:

Next, let’s try your personal keys! Use your favorite text editor and edit the example-sync-demo.c file.

The main is basically divided in 4 parts. We first initialize PubNub, we publish on a channel, we view the last 10 messages of a channel and we subscribe to a channel. This code can be a good guide to start coding your own app.

We will want to edit the first part, especially lines 16 and 17:

You’ll want to plug in your unique publish/subscribe keys that you got when you signed up for PubNub earlier in the post.


How PubNub Works

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.

You can publish readings from a Raspberry Pi sensor, or even send data to trigger device action. Let’s give a quick glance at how to publish a message:

We like sending JSONs as messages, it makes communicating objects across platforms much easier.

The subscribe is a little trickier in C:

We need to create an infinite loop. In this case we are subscribing to two different channels. We catch the message and print it out if it is not empty, else we prompt the user that we are connected. Pretty straight forward.

Wrapping Up

And that’s it! If you want this same tutorial using Python, check it out here.

We’ve got a ton of other tutorials and demos for Raspberry Pi, from Raspberry Pi 101, to home automation, to real-time weather stations. Check out them all here!