-
Notifications
You must be signed in to change notification settings - Fork 49
/
PubNubPublisher.ino
88 lines (72 loc) · 1.92 KB
/
PubNubPublisher.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
PubNub sample client
This sample client will publish raw (JSON pre-encoded) PubNub messages.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* (Optional.) Analog sensors on pins A0 to A5.
* (Optional.) LED on pin 9 for success indication.
This code is in the public domain.
*/
#include <Ethernet.h>
#include <PubNub.h>
#include <SPI.h>
// Some Ethernet shields have a MAC address printed on a sticker on the shield;
// fill in that address here, or choose your own at random:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
const int pubLedPin = 9;
char pubkey[] = "demo";
char subkey[] = "demo";
char channel[] = "hello_world";
void setup()
{
pinMode(pubLedPin, OUTPUT);
digitalWrite(pubLedPin, LOW);
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(pubkey, subkey);
Serial.println("PubNub set up");
}
void flash(int ledPin, int times)
{
for (int i = 0; i < times; i++) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}
void loop()
{
Ethernet.maintain();
char msg[64] = "{\"analog\":[";
for (int i = 0; i < 6; i++) {
sprintf(msg + strlen(msg), "%d", analogRead(i));
if (i < 5)
strcat(msg, ",");
}
strcat(msg, "]}");
Serial.print("publishing message: ");
Serial.println(msg);
auto client = PubNub.publish(channel, msg);
if (!client) {
Serial.println("publishing error");
}
else {
PublishCracker cheez;
cheez.read_and_parse(client);
if (cheez.outcome() == cheez.sent) {
flash(pubLedPin, 2);
}
else {
flash(pubLedPin, 4);
}
Serial.println(cheez.description());
client->stop();
}
delay(5000);
}