PubNub Arduino SDK

Unsupported SDK

PubNub no longer supports this SDK, but you are welcome to contribute.

Get Code: Source

https://github.com/pubnub/arduino

Since v1.1.1 Pubnub Arduino SDK is available as a library in the Arduino Library Manager, ready available in the Arduino IDE (version 1.6.2 or newer). So, you just include it in your sketch.

Arduino online library repository can take a long time to update to a new version of the Arduino SDK, so, you may need to download a release from Github and copy it manually (for example, if it contains a fix for your networking hardware). If you do that, be aware that Arduino IDE may give a warning about using a bad version, which you can safely ignore.

Arduino remarks

Since Arduino platform is designed mostly for small MCUs with modest resources, Arduino SDK is also small and modest and different from every other Pubnub SDKs, including the C-core.

It officially supports only publish and subscribe, though there is a minimal unofficial history() call available.

There is no support for SSL/TLS.

Communication - network hardware

Because of modest resources, Pubnub Arduino SDK needs to be instructed on the communication hardware to use.

The default is the Ethernet Shield. If you are using another Shield, or some Arduino supported board that that has integrated network interface, you need to define the PubNub_BASE_CLIENT preprocessor macro to the class of the network interface before including Pubnub.h.

Hello World

This section illustrates include info, instance creation, and an example Hello World snippet, using the default hardware:

Ethernet Shield Code Sample

Note

Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the UUID, you won't be able to connect to PubNub.

#include <Ethernet.h>
#include <Pubnub.h>
void setup() {
Serial.begin(9600);
while (!Ethernet.begin(mac)) {
Serial.println("Ethernet setup error");
delay(1000);
}

PubNub.begin("demo", "demo");
}

void loop() {
Ethernet.maintain();
EthernetClient *client;
show all 45 lines

Wifi101 Shield Sample

Note

Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the UUID, you won't be able to connect to PubNub.

#include <WiFi101.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <Pubnub.h>

const static char ssid[] = "your-wifi-ssid";
const static char pass[] = "your-wifi-password";

void setup() {
Serial.begin(9600);
status = WiFi.begin(ssid, pass);

if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while (true);
}
show all 52 lines

Copy and paste examples

In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:

Init

Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance.

Note

Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the UUID, you won't be able to connect to PubNub.

Pubnub.begin(/*publish key*/"demo", /*subscribe key*/"demo");

Subscribe

Subscribe (listen on) a channel (it's async!):

PubSubClient *sclient = Pubnub.subscribe("my_channel");
if (sclient != 0) {
while (sclient->wait_for_data()) {
Serial.write(sclient->read());
}
sclient->stop();
}

Publish

Publish a message to a channel:

PubNub_BASE_CLIENT *client = PubNub.publish("my_channel", "\"message\"");
if (client != 0) {
client->stop();
}