-
Notifications
You must be signed in to change notification settings - Fork 49
/
PubNubDemo.ino
135 lines (116 loc) · 3.4 KB
/
PubNubDemo.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
PubNub sample client
This sample client will use just the minimal-footprint raw PubNub
interface where it is your responsibility to deal with the JSON encoding.
It will just send a hello world message and retrieve one back, reporting
its deeds on serial console.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* (Optional.) LED on pin 8 for reception indication.
* (Optional.) LED on pin 9 for publish indication.
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubNub.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 subLedPin = 8;
const int pubLedPin = 9;
char pubkey[] = "demo";
char subkey[] = "demo";
char channel[] = "hello_world";
void setup()
{
pinMode(subLedPin, OUTPUT);
pinMode(pubLedPin, OUTPUT);
digitalWrite(subLedPin, LOW);
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();
Serial.println("publishing a message");
auto client = PubNub.publish(channel, "\"\\\"Hello world!\\\" she said.\"");
if (!client) {
Serial.println("publishing error");
delay(1000);
return;
}
PublishCracker cheez;
cheez.read_and_parse(client);
/** You're mostly interested in `outcome()`, and, if it's
"failed", then `description()`, but let's print all, for
completeness.
*/
Serial.print("Outcome: ");
Serial.print(cheez.outcome());
Serial.print(' ');
Serial.println(cheez.to_str(cheez.outcome()));
Serial.print("description: ");
Serial.println(cheez.description());
Serial.print("timestamp: ");
Serial.println(cheez.timestamp());
Serial.print("state: ");
Serial.print(cheez.state());
Serial.print(' ');
Serial.println(cheez.to_str(cheez.state()));
client->stop();
Serial.println();
flash(pubLedPin, 3);
Serial.println("waiting for a message (subscribe)");
PubSubClient* subclient = PubNub.subscribe(channel);
if (!subclient) {
Serial.println("subscription error");
delay(1000);
return;
}
String msg;
SubscribeCracker ritz(subclient);
while (!ritz.finished()) {
ritz.get(msg);
if (msg.length() > 0) {
Serial.print("Received: ");
Serial.println(msg);
}
}
subclient->stop();
flash(subLedPin, 3);
Serial.println("retrieving message history");
auto hisclient = PubNub.history(channel);
if (!hisclient) {
Serial.println("history error");
delay(1000);
return;
}
HistoryCracker tuc(hisclient);
while (!tuc.finished()) {
tuc.get(msg);
if (msg.length() > 0) {
Serial.print("Received: ");
Serial.println(msg);
}
}
hisclient->stop();
flash(pubLedPin, 2);
delay(5000);
}