[211] | 1 | /*
|
---|
| 2 | PubNub sample JSON-parsing client with WiFi support
|
---|
| 3 |
|
---|
| 4 | This combines two sketches: the PubNubJson example of PubNub library
|
---|
| 5 | and the WifiWebClientRepeating example of the WiFi library.
|
---|
| 6 |
|
---|
| 7 | This sample client will properly parse JSON-encoded PubNub subscription
|
---|
| 8 | replies using the aJson library. It will send a simple message, then
|
---|
| 9 | properly parsing and inspecting a subscription message received back.
|
---|
| 10 |
|
---|
| 11 | This is achieved by integration with the aJson library. You will need
|
---|
| 12 | a version featuring Wiring Stream integration, that can be found
|
---|
| 13 | at http://github.com/pasky/aJson as of 2013-05-30.
|
---|
| 14 |
|
---|
| 15 | Circuit:
|
---|
| 16 | * Wifi shield attached to pins 10, 11, 12, 13
|
---|
| 17 | * (Optional) Analog sensors attached to analog pin.
|
---|
| 18 | * (Optional) LEDs to be dimmed attached to PWM pins 8 and 9.
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | Please refer to the PubNubJson example description for some important
|
---|
| 22 | notes, especially regarding memory saving on Arduino Uno/Duemilanove.
|
---|
| 23 | You can also save some RAM by not using WiFi password protection.
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | created 30 May 2013
|
---|
| 27 | by Petr Baudis
|
---|
| 28 |
|
---|
| 29 | https://github.com/pubnub/pubnub-api/tree/master/arduino
|
---|
| 30 | This code is in the public domain.
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include <SPI.h>
|
---|
| 34 | #include <WiFi.h>
|
---|
| 35 | #include <PubNub.h>
|
---|
| 36 | #include <aJSON.h>
|
---|
| 37 |
|
---|
| 38 | static char ssid[] = "yourNetwork"; // your network SSID (name)
|
---|
| 39 | static char pass[] = "secretPassword"; // your network password
|
---|
| 40 | static int keyIndex = 0; // your network key Index number (needed only for WEP)
|
---|
| 41 |
|
---|
| 42 | const static char pubkey[] = "demo";
|
---|
| 43 | const static char subkey[] = "demo";
|
---|
| 44 | const static char channel[] = "hello_world";
|
---|
| 45 |
|
---|
| 46 | void setup()
|
---|
| 47 | {
|
---|
| 48 | Serial.begin(9600);
|
---|
| 49 | Serial.println("Serial set up");
|
---|
| 50 |
|
---|
| 51 | if (WiFi.status() == WL_NO_SHIELD) {
|
---|
| 52 | Serial.println("WiFi shield not present");
|
---|
| 53 | while(true); // stop
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | int status;
|
---|
| 57 | // attempt to connect to Wifi network:
|
---|
| 58 | do {
|
---|
| 59 | Serial.print("WiFi connecting to SSID: ");
|
---|
| 60 | Serial.println(ssid);
|
---|
| 61 |
|
---|
| 62 | // Connect to the network. Uncomment whichever line is right for you:
|
---|
| 63 | //status = WiFi.begin(ssid); // open network
|
---|
| 64 | //status = WiFi.begin(ssid, keyIndex, key); // WEP network
|
---|
| 65 | status = WiFi.begin(ssid, pass); // WPA / WPA2 Personal network
|
---|
| 66 | } while (status != WL_CONNECTED);
|
---|
| 67 | Serial.println("WiFi set up");
|
---|
| 68 |
|
---|
| 69 | PubNub.begin(pubkey, subkey);
|
---|
| 70 | Serial.println("PubNub set up");
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | aJsonObject *createMessage()
|
---|
| 74 | {
|
---|
| 75 | aJsonObject *msg = aJson.createObject();
|
---|
| 76 |
|
---|
| 77 | aJsonObject *sender = aJson.createObject();
|
---|
| 78 | aJson.addStringToObject(sender, "name", "Arduino");
|
---|
| 79 | aJson.addItemToObject(msg, "sender", sender);
|
---|
| 80 |
|
---|
| 81 | int analogValues[6];
|
---|
| 82 | for (int i = 0; i < 6; i++) {
|
---|
| 83 | analogValues[i] = analogRead(i);
|
---|
| 84 | }
|
---|
| 85 | aJsonObject *analog = aJson.createIntArray(analogValues, 6);
|
---|
| 86 | aJson.addItemToObject(msg, "analog", analog);
|
---|
| 87 | return msg;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /* Process message like: { "pwm": { "8": 0, "9": 128 } } */
|
---|
| 91 | void processPwmInfo(aJsonObject *item)
|
---|
| 92 | {
|
---|
| 93 | aJsonObject *pwm = aJson.getObjectItem(item, "pwm");
|
---|
| 94 | if (!pwm) {
|
---|
| 95 | Serial.println("no pwm data");
|
---|
| 96 | return;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | const static int pins[] = { 8, 9 };
|
---|
| 100 | const static int pins_n = 2;
|
---|
| 101 | for (int i = 0; i < pins_n; i++) {
|
---|
| 102 | char pinstr[3];
|
---|
| 103 | snprintf(pinstr, sizeof(pinstr), "%d", pins[i]);
|
---|
| 104 |
|
---|
| 105 | aJsonObject *pwmval = aJson.getObjectItem(pwm, pinstr);
|
---|
| 106 | if (!pwmval) continue; /* Value not provided, ok. */
|
---|
| 107 | if (pwmval->type != aJson_Int) {
|
---|
| 108 | Serial.print(" invalid data type ");
|
---|
| 109 | Serial.print(pwmval->type, DEC);
|
---|
| 110 | Serial.print(" for pin ");
|
---|
| 111 | Serial.println(pins[i], DEC);
|
---|
| 112 | continue;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | Serial.print(" setting pin ");
|
---|
| 116 | Serial.print(pins[i], DEC);
|
---|
| 117 | Serial.print(" to value ");
|
---|
| 118 | Serial.println(pwmval->valueint, DEC);
|
---|
| 119 | analogWrite(pins[i], pwmval->valueint);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | void dumpMessage(Stream &s, aJsonObject *msg)
|
---|
| 124 | {
|
---|
| 125 | int msg_count = aJson.getArraySize(msg);
|
---|
| 126 | for (int i = 0; i < msg_count; i++) {
|
---|
| 127 | aJsonObject *item, *sender, *analog, *value;
|
---|
| 128 | s.print("Msg #");
|
---|
| 129 | s.println(i, DEC);
|
---|
| 130 |
|
---|
| 131 | item = aJson.getArrayItem(msg, i);
|
---|
| 132 | if (!item) { s.println("item not acquired"); delay(1000); return; }
|
---|
| 133 |
|
---|
| 134 | processPwmInfo(item);
|
---|
| 135 |
|
---|
| 136 | /* Below, we parse and dump messages from fellow Arduinos. */
|
---|
| 137 |
|
---|
| 138 | sender = aJson.getObjectItem(item, "sender");
|
---|
| 139 | if (!sender) { s.println("sender not acquired"); delay(1000); return; }
|
---|
| 140 |
|
---|
| 141 | s.print(" A2: ");
|
---|
| 142 | analog = aJson.getObjectItem(item, "analog");
|
---|
| 143 | if (!analog) { s.println("analog not acquired"); delay(1000); return; }
|
---|
| 144 | value = aJson.getArrayItem(analog, 2);
|
---|
| 145 | if (!value) { s.println("analog[2] not acquired"); delay(1000); return; }
|
---|
| 146 | s.print(value->valueint, DEC);
|
---|
| 147 |
|
---|
| 148 | s.println();
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | void loop()
|
---|
| 153 | {
|
---|
| 154 | // Intriguingly, the WiFi library doesn't seem to have a call
|
---|
| 155 | // equivalent to Ethernet.maintain();
|
---|
| 156 | //WiFi.maintain();
|
---|
| 157 |
|
---|
| 158 | WiFiClient *client;
|
---|
| 159 |
|
---|
| 160 | /* Publish */
|
---|
| 161 |
|
---|
| 162 | Serial.print("publishing a message: ");
|
---|
| 163 | aJsonObject *msg = createMessage();
|
---|
| 164 | char *msgStr = aJson.print(msg);
|
---|
| 165 | aJson.deleteItem(msg);
|
---|
| 166 |
|
---|
| 167 | // msgStr is returned in a buffer that can be potentially
|
---|
| 168 | // needlessly large; this call will "tighten" it
|
---|
| 169 | msgStr = (char *) realloc(msgStr, strlen(msgStr) + 1);
|
---|
| 170 |
|
---|
| 171 | Serial.println(msgStr);
|
---|
| 172 | client = PubNub.publish(channel, msgStr);
|
---|
| 173 | free(msgStr);
|
---|
| 174 | if (!client) {
|
---|
| 175 | Serial.println("publishing error");
|
---|
| 176 | delay(1000);
|
---|
| 177 | return;
|
---|
| 178 | }
|
---|
| 179 | client->stop();
|
---|
| 180 |
|
---|
| 181 | /* Subscribe and load reply */
|
---|
| 182 |
|
---|
| 183 | Serial.println("waiting for a message (subscribe)");
|
---|
| 184 | client = PubNub.subscribe(channel);
|
---|
| 185 | if (!client) {
|
---|
| 186 | Serial.println("subscription error");
|
---|
| 187 | delay(1000);
|
---|
| 188 | return;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /* Parse */
|
---|
| 192 |
|
---|
| 193 | aJsonClientStream stream(client);
|
---|
| 194 | msg = aJson.parse(&stream);
|
---|
| 195 | client->stop();
|
---|
| 196 | if (!msg) { Serial.println("parse error"); delay(1000); return; }
|
---|
| 197 | dumpMessage(Serial, msg);
|
---|
| 198 | aJson.deleteItem(msg);
|
---|
| 199 |
|
---|
| 200 | delay(10000);
|
---|
| 201 | }
|
---|