1 | /*
|
---|
2 | PubNub sample JSON-parsing client
|
---|
3 |
|
---|
4 | This sample client will properly parse JSON-encoded PubNub subscription
|
---|
5 | replies using the aJson library. It will send a simple message, then
|
---|
6 | properly parsing and inspecting a subscription message received back.
|
---|
7 |
|
---|
8 | This is achieved by integration with the aJson library. You will need
|
---|
9 | a version featuring Wiring Stream integration, that can be found
|
---|
10 | at http://github.com/interactive-matter/aJson as of 2012-11-12.
|
---|
11 |
|
---|
12 | Circuit:
|
---|
13 | * Ethernet shield attached to pins 10, 11, 12, 13
|
---|
14 | * (Optional) Analog sensors attached to analog pin.
|
---|
15 | * (Optional) LEDs to be dimmed attached to PWM pins 8 and 9.
|
---|
16 |
|
---|
17 |
|
---|
18 | Note that due to use of the aJSON library, this sketch is more memory
|
---|
19 | sensitive than the others. In order to be able to use it on the boards
|
---|
20 | based on ATMega328, some special care is needed. Memory saving tips:
|
---|
21 |
|
---|
22 | (i) Remove myI, dnsI from the sketch unless you need them.
|
---|
23 |
|
---|
24 | (ii) In the aJSON library's utility/stringbuffer.c, change BUFFER_SIZE
|
---|
25 | from 256 to 128 or less. Note that this will limit the length of JSON
|
---|
26 | messages your script will be able to process.
|
---|
27 |
|
---|
28 | (iii) In the file hardware/arduino/cores/arduino/HardwareSerial.cpp
|
---|
29 | which is part of your Arduino installation, decrease SERIAL_BUFFER_SIZE
|
---|
30 | from 64 to 16 or even smaller value (depends on your Serial usage).
|
---|
31 |
|
---|
32 | Usually, (i) and (ii) is enough; try (iii) if you can't do one of these.
|
---|
33 | If you are still short on memory (will manifest as connection error,
|
---|
34 | there will be not enough memory left for EthernetClient), some very
|
---|
35 | aggressive code changes would be required, or re-consider whether you
|
---|
36 | cannot write ad-hoc JSON generating/parsing code covering just your
|
---|
37 | particular scenario instead of using the generic aJSON library.
|
---|
38 |
|
---|
39 |
|
---|
40 | created 26 October 2012
|
---|
41 | by Petr Baudis
|
---|
42 |
|
---|
43 | https://github.com/pubnub/pubnub-api/tree/master/arduino
|
---|
44 | This code is in the public domain.
|
---|
45 | */
|
---|
46 |
|
---|
47 | #include <SPI.h>
|
---|
48 | #include <Ethernet.h>
|
---|
49 | #include <PubNub.h>
|
---|
50 | #include <aJSON.h>
|
---|
51 |
|
---|
52 | // Some Ethernet shields have a MAC address printed on a sticker on the shield;
|
---|
53 | // fill in that address here, or choose your own at random:
|
---|
54 | const static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
---|
55 |
|
---|
56 | // Memory saving tip: remove myI and dnsI from your sketch if you
|
---|
57 | // are content to rely on DHCP autoconfiguration.
|
---|
58 | IPAddress myI(10, 42, 0, 2);
|
---|
59 | IPAddress dnsI(8, 8, 8, 8);
|
---|
60 |
|
---|
61 | const static char pubkey[] = "demo";
|
---|
62 | const static char subkey[] = "demo";
|
---|
63 | const static char channel[] = "hello_world";
|
---|
64 |
|
---|
65 | void setup()
|
---|
66 | {
|
---|
67 | Serial.begin(9600);
|
---|
68 | Serial.println("Serial set up");
|
---|
69 |
|
---|
70 | Ethernet.begin((byte*) mac, myI, dnsI);
|
---|
71 | Serial.println("Ethernet set up");
|
---|
72 |
|
---|
73 | PubNub.begin(pubkey, subkey);
|
---|
74 | Serial.println("PubNub set up");
|
---|
75 | }
|
---|
76 |
|
---|
77 | aJsonObject *createMessage()
|
---|
78 | {
|
---|
79 | aJsonObject *msg = aJson.createObject();
|
---|
80 |
|
---|
81 | aJsonObject *sender = aJson.createObject();
|
---|
82 | aJson.addStringToObject(sender, "name", "Arduino");
|
---|
83 | aJson.addNumberToObject(sender, "mac_last_byte", mac[5]);
|
---|
84 | aJson.addItemToObject(msg, "sender", sender);
|
---|
85 |
|
---|
86 | int analogValues[6];
|
---|
87 | for (int i = 0; i < 6; i++) {
|
---|
88 | analogValues[i] = analogRead(i);
|
---|
89 | }
|
---|
90 | aJsonObject *analog = aJson.createIntArray(analogValues, 6);
|
---|
91 | aJson.addItemToObject(msg, "analog", analog);
|
---|
92 | return msg;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /* Process message like: { "pwm": { "8": 0, "9": 128 } } */
|
---|
96 | void processPwmInfo(aJsonObject *item)
|
---|
97 | {
|
---|
98 | aJsonObject *pwm = aJson.getObjectItem(item, "pwm");
|
---|
99 | if (!pwm) {
|
---|
100 | Serial.println("no pwm data");
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | const static int pins[] = { 8, 9 };
|
---|
105 | const static int pins_n = 2;
|
---|
106 | for (int i = 0; i < pins_n; i++) {
|
---|
107 | char pinstr[3];
|
---|
108 | snprintf(pinstr, sizeof(pinstr), "%d", pins[i]);
|
---|
109 |
|
---|
110 | aJsonObject *pwmval = aJson.getObjectItem(pwm, pinstr);
|
---|
111 | if (!pwmval) continue; /* Value not provided, ok. */
|
---|
112 | if (pwmval->type != aJson_Int) {
|
---|
113 | Serial.print(" invalid data type ");
|
---|
114 | Serial.print(pwmval->type, DEC);
|
---|
115 | Serial.print(" for pin ");
|
---|
116 | Serial.println(pins[i], DEC);
|
---|
117 | continue;
|
---|
118 | }
|
---|
119 |
|
---|
120 | Serial.print(" setting pin ");
|
---|
121 | Serial.print(pins[i], DEC);
|
---|
122 | Serial.print(" to value ");
|
---|
123 | Serial.println(pwmval->valueint, DEC);
|
---|
124 | analogWrite(pins[i], pwmval->valueint);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | void dumpMessage(Stream &s, aJsonObject *msg)
|
---|
129 | {
|
---|
130 | int msg_count = aJson.getArraySize(msg);
|
---|
131 | for (int i = 0; i < msg_count; i++) {
|
---|
132 | aJsonObject *item, *sender, *analog, *value;
|
---|
133 | s.print("Msg #");
|
---|
134 | s.println(i, DEC);
|
---|
135 |
|
---|
136 | item = aJson.getArrayItem(msg, i);
|
---|
137 | if (!item) { s.println("item not acquired"); delay(1000); return; }
|
---|
138 |
|
---|
139 | processPwmInfo(item);
|
---|
140 |
|
---|
141 | /* Below, we parse and dump messages from fellow Arduinos. */
|
---|
142 |
|
---|
143 | sender = aJson.getObjectItem(item, "sender");
|
---|
144 | if (!sender) { s.println("sender not acquired"); delay(1000); return; }
|
---|
145 |
|
---|
146 | s.print(" mac_last_byte: ");
|
---|
147 | value = aJson.getObjectItem(sender, "mac_last_byte");
|
---|
148 | if (!value) { s.println("mac_last_byte not acquired"); delay(1000); return; }
|
---|
149 | s.print(value->valueint, DEC);
|
---|
150 |
|
---|
151 | s.print(" A2: ");
|
---|
152 | analog = aJson.getObjectItem(item, "analog");
|
---|
153 | if (!analog) { s.println("analog not acquired"); delay(1000); return; }
|
---|
154 | value = aJson.getArrayItem(analog, 2);
|
---|
155 | if (!value) { s.println("analog[2] not acquired"); delay(1000); return; }
|
---|
156 | s.print(value->valueint, DEC);
|
---|
157 |
|
---|
158 | s.println();
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | void loop()
|
---|
163 | {
|
---|
164 | Ethernet.maintain();
|
---|
165 |
|
---|
166 | EthernetClient *client;
|
---|
167 |
|
---|
168 | /* Publish */
|
---|
169 |
|
---|
170 | Serial.print("publishing a message: ");
|
---|
171 | aJsonObject *msg = createMessage();
|
---|
172 | char *msgStr = aJson.print(msg);
|
---|
173 | aJson.deleteItem(msg);
|
---|
174 |
|
---|
175 | // msgStr is returned in a buffer that can be potentially
|
---|
176 | // needlessly large; this call will "tighten" it
|
---|
177 | msgStr = (char *) realloc(msgStr, strlen(msgStr) + 1);
|
---|
178 |
|
---|
179 | Serial.println(msgStr);
|
---|
180 | client = PubNub.publish(channel, msgStr);
|
---|
181 | free(msgStr);
|
---|
182 | if (!client) {
|
---|
183 | Serial.println("publishing error");
|
---|
184 | delay(1000);
|
---|
185 | return;
|
---|
186 | }
|
---|
187 | client->stop();
|
---|
188 |
|
---|
189 | /* Subscribe and load reply */
|
---|
190 |
|
---|
191 | Serial.println("waiting for a message (subscribe)");
|
---|
192 | client = PubNub.subscribe(channel);
|
---|
193 | if (!client) {
|
---|
194 | Serial.println("subscription error");
|
---|
195 | delay(1000);
|
---|
196 | return;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* Parse */
|
---|
200 |
|
---|
201 | aJsonClientStream stream(client);
|
---|
202 | msg = aJson.parse(&stream);
|
---|
203 | client->stop();
|
---|
204 | if (!msg) { Serial.println("parse error"); delay(1000); return; }
|
---|
205 | dumpMessage(Serial, msg);
|
---|
206 | aJson.deleteItem(msg);
|
---|
207 |
|
---|
208 | delay(10000);
|
---|
209 | }
|
---|