Feedback

SelectPubNub Arduino SDK


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.

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.

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.



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

#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;

    Serial.println("publishing a message");
    client = PubNub.publish(channel, "\"\\\"Hello world!\\\" from Arduino.\"");
    if (!client) {
        Serial.println("publishing error");
        delay(1000);
        return;
    }
    while (client->connected()) {
        while (client->connected() && !client->available());
        char c = client->read();
        Serial.print(c);
    }
    client->stop();
    Serial.println();

    Serial.println("waiting for a message (subscribe)");
    PubSubClient *pclient = PubNub.subscribe(channel);
    if (!pclient) {
        Serial.println("subscription error");
        delay(1000);
        return;
    }
    while (pclient->wait_for_data()) {
        char c = pclient->read();
        Serial.print(c);
    }
    pclient->stop();
    Serial.println();
}
#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);
    } 
    else {
        PubNub.begin(pubkey, subkey);
    }
}

void loop() {
    WifiClient *client;

    Serial.println("publishing a message");
    client = PubNub.publish(channel, "\"\\\"Hello world!\\\" from Arduino.\"");
    if (!client) {
        Serial.println("publishing error");
        delay(1000);
        return;
    }
    while (client->connected()) {
        while (client->connected() && !client->available());
        char c = client->read();
        Serial.print(c);
    }
    client->stop();
    Serial.println();

    Serial.println("waiting for a message (subscribe)");
    PubSubClient *pclient = PubNub.subscribe(channel);
    if (!pclient) {
        Serial.println("subscription error");
        delay(1000);
        return;
    }
    while (pclient->wait_for_data()) {
        char c = pclient->read();
        Serial.print(c);
    }
    pclient->stop();
    Serial.println();
}
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance.
Pubnub.begin(/*publish key*/"demo", /*subscribe key*/"demo");
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 a message to a channel:
PubNub_BASE_CLIENT *client = PubNub.publish("my_channel", "\"message\"");
if (client != 0) {
    client->stop();
}