1 | /*
|
---|
2 | PubNub sample client
|
---|
3 |
|
---|
4 | This sample client will use just the minimal-footprint raw PubNub
|
---|
5 | interface where it is your responsibility to deal with the JSON encoding.
|
---|
6 |
|
---|
7 | It will just send a hello world message and retrieve one back, reporting
|
---|
8 | its deeds on serial console.
|
---|
9 |
|
---|
10 | Circuit:
|
---|
11 | * Ethernet shield attached to pins 10, 11, 12, 13
|
---|
12 | * (Optional.) LED on pin 8 for reception indication.
|
---|
13 | * (Optional.) LED on pin 9 for publish indication.
|
---|
14 |
|
---|
15 | created 23 October 2012
|
---|
16 | by Petr Baudis
|
---|
17 |
|
---|
18 | https://github.com/pubnub/pubnub-api/tree/master/arduino
|
---|
19 | This code is in the public domain.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <SPI.h>
|
---|
23 | #include <Ethernet.h>
|
---|
24 | #include <PubNub.h>
|
---|
25 |
|
---|
26 | // Some Ethernet shields have a MAC address printed on a sticker on the shield;
|
---|
27 | // fill in that address here, or choose your own at random:
|
---|
28 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
---|
29 |
|
---|
30 | const int subLedPin = 8;
|
---|
31 | const int pubLedPin = 9;
|
---|
32 |
|
---|
33 | char pubkey[] = "demo";
|
---|
34 | char subkey[] = "demo";
|
---|
35 | char channel[] = "hello_world";
|
---|
36 |
|
---|
37 | void setup()
|
---|
38 | {
|
---|
39 | pinMode(subLedPin, OUTPUT);
|
---|
40 | pinMode(pubLedPin, OUTPUT);
|
---|
41 | digitalWrite(subLedPin, LOW);
|
---|
42 | digitalWrite(pubLedPin, LOW);
|
---|
43 |
|
---|
44 | Serial.begin(9600);
|
---|
45 | Serial.println("Serial set up");
|
---|
46 |
|
---|
47 | while (!Ethernet.begin(mac)) {
|
---|
48 | Serial.println("Ethernet setup error");
|
---|
49 | delay(1000);
|
---|
50 | }
|
---|
51 | Serial.println("Ethernet set up");
|
---|
52 |
|
---|
53 | PubNub.begin(pubkey, subkey);
|
---|
54 | Serial.println("PubNub set up");
|
---|
55 | }
|
---|
56 |
|
---|
57 | void flash(int ledPin)
|
---|
58 | {
|
---|
59 | /* Flash LED three times. */
|
---|
60 | for (int i = 0; i < 3; i++) {
|
---|
61 | digitalWrite(ledPin, HIGH);
|
---|
62 | delay(100);
|
---|
63 | digitalWrite(ledPin, LOW);
|
---|
64 | delay(100);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | void loop()
|
---|
69 | {
|
---|
70 | Ethernet.maintain();
|
---|
71 |
|
---|
72 | EthernetClient *client;
|
---|
73 |
|
---|
74 | Serial.println("publishing a message");
|
---|
75 | client = PubNub.publish(channel, "\"\\\"Hello world!\\\" she said.\"");
|
---|
76 | if (!client) {
|
---|
77 | Serial.println("publishing error");
|
---|
78 | delay(1000);
|
---|
79 | return;
|
---|
80 | }
|
---|
81 | while (client->connected()) {
|
---|
82 | while (client->connected() && !client->available()) ; // wait
|
---|
83 | char c = client->read();
|
---|
84 | Serial.print(c);
|
---|
85 | }
|
---|
86 | client->stop();
|
---|
87 | Serial.println();
|
---|
88 | flash(pubLedPin);
|
---|
89 |
|
---|
90 | Serial.println("waiting for a message (subscribe)");
|
---|
91 | PubSubClient *pclient = PubNub.subscribe(channel);
|
---|
92 | if (!pclient) {
|
---|
93 | Serial.println("subscription error");
|
---|
94 | delay(1000);
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | while (pclient->wait_for_data()) {
|
---|
98 | char c = pclient->read();
|
---|
99 | Serial.print(c);
|
---|
100 | }
|
---|
101 | pclient->stop();
|
---|
102 | Serial.println();
|
---|
103 | flash(subLedPin);
|
---|
104 |
|
---|
105 | Serial.println("retrieving message history");
|
---|
106 | client = PubNub.history(channel);
|
---|
107 | if (!client) {
|
---|
108 | Serial.println("history error");
|
---|
109 | delay(1000);
|
---|
110 | return;
|
---|
111 | }
|
---|
112 | while (client->connected()) {
|
---|
113 | while (client->connected() && !client->available()) ; // wait
|
---|
114 | char c = client->read();
|
---|
115 | Serial.print(c);
|
---|
116 | }
|
---|
117 | client->stop();
|
---|
118 | Serial.println();
|
---|
119 |
|
---|
120 | delay(10000);
|
---|
121 | }
|
---|