Skip to content

Instantly share code, notes, and snippets.

@domadev812
Forked from ToeJamson/1.h
Last active November 15, 2017 21:06
Show Gist options
  • Save domadev812/a8f87fc275270d1ac14c19d4475dee42 to your computer and use it in GitHub Desktop.
Save domadev812/a8f87fc275270d1ac14c19d4475dee42 to your computer and use it in GitHub Desktop.
Arduino To PubNub In Two Steps
#include <SPI.h>
#include <Ethernet.h>
#include <Pubnub.h>
#include "string.h"
#include "iotconnector.h"
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char pubkey[] = "PUBLISH KEYS HERE";char subkey[] = "SUBSCRIBE KEYS HERE";char channel[] = "iotchannel";char uuid[] = "Arduino";
iotbridge arduino;
void initialize(){
Serial.begin(9600);
Serial.println("Serial set up");
while (!Ethernet.begin(mac)) {
Serial.println("Ethernet setup error");
delay(1000);
}
Serial.println("Ethernet set up");
PubNub.begin("demo", "demo");
}
void do_something(String value){
Serial.println("in the callback");
Serial.println(value);
}
void setup()
{
initialize();
arduino.init( pubkey, subkey, uuid);
}
void loop()
{
String returnmessage;
Ethernet.maintain();
//Publish
arduino.send(channel,"\"Hey There\"");
//Subscribe
returnmessage = arduino.connect(channel);
//callback function of sorts, to work with the received message
do_something(returnmessage);
Serial.println();
}
bool iotbridge::send(const char *channel, const char *message){
EthernetClient *client;
client = PubNub.publish(channel, message);
return client;
}
String iotbridge::connect(const char *channel){
PubSubClient *pclient = PubNub.subscribe(channel);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment