├── README.md ├── SocketIOClient.cpp ├── SocketIOClient.h └── examples ├── Hello_Time_ESP8266.ino ├── Hello_time.ino ├── Hello_time_sparkfun.ino ├── app.js └── index.html /README.md: -------------------------------------------------------------------------------- 1 | Hacked up Socket.io library 2 | Original from: https://github.com/washo4evr/Socket.io-v1.x-Library 3 | 4 | Not attempting to make these changes integratable back to the master... 5 | 6 | === 7 | 8 | # Socket.io-v1.x-Library 9 | Socket.io Library for Arduino 10 | 11 | Works with W5100 & ENC28J60 as well as ESP8266 12 | 13 | Library will not work when the ESP8266 is being driven by a Uno 14 | 15 | Support for JSON was added using https://github.com/bblanchon/ArduinoJson 16 | Users will need to download the library directly from this link and install it. 17 | 18 | Instead of using client.send, you can now use client.sendJSON 19 | 20 | 21 | thanks to funkyguy4000, users can now use #define ESP8266, #define W5100 or #define ENC28J60 22 | no need to edit socketio.h anymore 23 | 24 | REST API added : getREST(path), postREST(path, type, data), putREST(path, type, data), deleteREST(path) 25 | headers are not yet handled, soon 26 | 27 | incoming messages can now be longer than 125 bytes (at least up to 255 bytes) 28 | will do more testing and edit if necessary 29 | 30 | thank you all for your patience 31 | -------------------------------------------------------------------------------- /SocketIOClient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | socket.io-arduino-client: a Socket.IO client for the Arduino 3 | Based on the Kevin Rohling WebSocketClient & Bill Roy Socket.io Lbrary 4 | Copyright 2015 Florent Vidal 5 | Supports Socket.io v1.x 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the "Software"), to deal in the Software without 9 | restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following 13 | conditions: 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | 26 | /* 27 | * Modified by RoboJay 28 | */ 29 | 30 | #include 31 | 32 | SocketIOClient::SocketIOClient() { 33 | for (int i = 0; i < MAX_ON_HANDLERS; i++) { 34 | onId[i] = ""; 35 | } 36 | } 37 | 38 | bool SocketIOClient::connect(String thehostname, int theport) { 39 | thehostname.toCharArray(hostname, MAX_HOSTNAME_LEN); 40 | port = theport; 41 | 42 | if (!internets.connect(hostname, theport)){ return false; } 43 | 44 | sendHandshake(hostname); 45 | return readHandshake(); 46 | } 47 | 48 | bool SocketIOClient::connectHTTP(String thehostname, int theport) { 49 | thehostname.toCharArray(hostname, MAX_HOSTNAME_LEN); 50 | port = theport; 51 | if (!internets.connect(hostname, theport)){ return false; } 52 | return true; 53 | } 54 | 55 | bool SocketIOClient::reconnect(String thehostname, int theport) { 56 | thehostname.toCharArray(hostname, MAX_HOSTNAME_LEN); 57 | port = theport; 58 | 59 | if (!internets.connect(hostname, theport)){ return false; } 60 | 61 | sendHandshake(hostname); 62 | return readHandshake(); 63 | } 64 | 65 | bool SocketIOClient::connected() { return internets.connected();} 66 | 67 | void SocketIOClient::disconnect() { internets.stop(); } 68 | 69 | void SocketIOClient::eventHandler(int index) { 70 | String id = ""; 71 | String data = ""; 72 | String rcvdmsg = ""; 73 | int sizemsg = databuffer[index + 1]; // 0-125 byte, index ok Fix provide by Galilei11. Thanks 74 | if (databuffer[index + 1]>125) { 75 | sizemsg = databuffer[index + 2]; // 126-255 byte 76 | index += 1; // index correction to start 77 | } 78 | if (ioDebug) Serial.print("[eventHandler] Message size = "); //Can be used for debugging 79 | if (ioDebug) Serial.println(sizemsg); //Can be used for debugging 80 | for (int i = index + 2; i < index + sizemsg + 2; i++) { rcvdmsg += (char)databuffer[i]; } 81 | if (ioDebug) Serial.print("[eventHandler] Received message = "); //Can be used for debugging 82 | if (ioDebug) Serial.println(rcvdmsg); //Can be used for debugging 83 | switch (rcvdmsg[0]) { 84 | case '2': 85 | if (ioDebug) Serial.println("[eventHandler] Ping received - Sending Pong"); 86 | heartbeat(1); 87 | break; 88 | case '3': 89 | if (ioDebug) Serial.println("[eventHandler] Pong received - All good"); 90 | break; 91 | case '4': 92 | switch (rcvdmsg[1]) { 93 | case '0': 94 | if (ioDebug) Serial.println("[eventHandler] Upgrade to WebSocket confirmed"); 95 | break; 96 | case '2': 97 | id = rcvdmsg.substring(4, rcvdmsg.indexOf("\",")); 98 | if (ioDebug){ Serial.println("[eventHandler] id = " + id); } 99 | data = rcvdmsg.substring(rcvdmsg.indexOf("\",") + 3, rcvdmsg.length () - 2); 100 | if (ioDebug){ Serial.println("[eventHandler] data = " + data); } 101 | for (uint8_t i = 0; i < onIndex; i++) { 102 | if (id == onId[i]) { 103 | if (ioDebug) Serial.println("[eventHandler] Found handler = " + String(i)); 104 | (*onFunction[i])(data); 105 | } 106 | } 107 | break; 108 | } 109 | } 110 | } 111 | 112 | bool SocketIOClient::monitor() { 113 | int index = -1; 114 | int index2 = -1; 115 | String tmp = ""; 116 | *databuffer = 0; 117 | static unsigned long pingTimer = 0; 118 | 119 | if (!internets.connected()) { 120 | if (ioDebug) Serial.println("[monitor] Client not connected."); 121 | if (connect(hostname, port)) { 122 | if (ioDebug) Serial.println("[monitor] Connected."); 123 | return true; 124 | } else { 125 | if (ioDebug) Serial.println("[monitor] Can't connect. Aborting."); 126 | } 127 | } 128 | 129 | // the PING_INTERVAL from the negotiation phase should be used 130 | // this is a temporary hack 131 | if (internets.connected() && millis() >= pingTimer) { 132 | heartbeat(0); 133 | pingTimer = millis() + PING_INTERVAL; 134 | } 135 | 136 | if (!internets.available()){ return false;} 137 | while (internets.available()) { // read availible internets 138 | readLine(); 139 | tmp = databuffer; 140 | dataptr = databuffer; 141 | index = tmp.indexOf((char)129); //129 DEC = 0x81 HEX = sent for proper communication 142 | index2 = tmp.indexOf((char)129, index + 1); 143 | if (index != -1) { eventHandler(index); } 144 | if (index2 != -1) { eventHandler(index2);} 145 | } 146 | return false; 147 | } 148 | 149 | void SocketIOClient::sendHandshake(char hostname[]) { 150 | internets.println(F("GET /socket.io/1/?transport=polling&b64=true HTTP/1.1")); 151 | internets.print(F("Host: ")); 152 | internets.println(hostname); 153 | internets.println(F("Origin: Arduino\r\n")); 154 | } 155 | 156 | bool SocketIOClient::waitForInput(void) { 157 | unsigned long now = millis(); 158 | while (!internets.available() && ((millis() - now) < 30000UL)) { ; } 159 | return internets.available(); 160 | } 161 | 162 | void SocketIOClient::eatHeader(void) { 163 | while (internets.available()) { // consume the header 164 | readLine(); 165 | if (strlen(databuffer) == 0) { break; } 166 | } 167 | } 168 | 169 | bool SocketIOClient::readHandshake() { 170 | if (!waitForInput()){ return false; } 171 | // check for happy "HTTP/1.1 200" response 172 | readLine(); 173 | if (atoi(&databuffer[9]) != 200) { 174 | while (internets.available()) readLine(); 175 | internets.stop(); 176 | return false; 177 | } 178 | eatHeader(); 179 | readLine(); 180 | String tmp = databuffer; 181 | 182 | int sidindex = tmp.indexOf("sid"); 183 | int sidendindex = tmp.indexOf("\"", sidindex + 6); 184 | int count = sidendindex - sidindex - 6; 185 | 186 | for (int i = 0; i < count; i++) { 187 | sid[i] = databuffer[i + sidindex + 6]; 188 | } 189 | if (ioDebug) Serial.print(F("[readHandshake] Connected. SID=")); 190 | if (ioDebug) Serial.println(sid); // sid:transport:timeout 191 | 192 | while (internets.available()) readLine(); 193 | internets.stop(); 194 | delay(1000); 195 | 196 | // reconnect on websocket connection 197 | if (!internets.connect(hostname, port)) { 198 | if (ioDebug) Serial.print(F("[readHandshake] Websocket failed.")); 199 | return false; 200 | } 201 | if (ioDebug) Serial.println(F("[readHandshake] Connecting via Websocket")); 202 | 203 | internets.print(F("GET /socket.io/1/websocket/?transport=websocket&b64=true&sid=")); 204 | internets.print(sid); 205 | internets.print(F(" HTTP/1.1\r\n")); 206 | internets.print(F("Host: ")); 207 | internets.print(hostname); 208 | internets.print("\r\n"); 209 | internets.print(F("Origin: ArduinoSocketIOClient\r\n")); 210 | internets.print(F("Sec-WebSocket-Key: ")); 211 | internets.print(sid); 212 | internets.print("\r\n"); 213 | internets.print(F("Sec-WebSocket-Version: 13\r\n")); 214 | internets.print(F("Upgrade: websocket\r\n")); // must be camelcase ?! 215 | internets.println(F("Connection: Upgrade\r\n")); 216 | 217 | if (!waitForInput()) { return false; } 218 | readLine(); 219 | if (atoi(&databuffer[9]) != 101) { // check for "HTTP/1.1 101 response, means Updrage to Websocket OK 220 | while (internets.available()) { readLine(); } 221 | internets.stop(); 222 | return false; 223 | } 224 | readLine(); 225 | readLine(); 226 | readLine(); 227 | for (int i = 0; i < 28; i++) { 228 | key[i] = databuffer[i + 22]; //key contains the Sec-WebSocket-Accept, could be used for verification 229 | } 230 | eatHeader(); 231 | 232 | /* 233 | Generating a 32 bits mask requiered for newer version 234 | Client has to send "52" for the upgrade to websocket 235 | */ 236 | randomSeed(analogRead(0)); 237 | String mask = ""; 238 | String masked = "52"; 239 | String message = "52"; 240 | for (int i = 0; i < 4; i++) { //generate a random mask, 4 bytes, ASCII 0 to 9 241 | char a = random(48, 57); 242 | mask += a; 243 | } 244 | for (int i = 0; i < message.length(); i++){ 245 | masked[i] = message[i] ^ mask[i % 4]; //apply the "mask" to the message ("52") 246 | } 247 | 248 | internets.print((char)0x81); //has to be sent for proper communication 249 | internets.print((char)130); //size of the message (2) + 128 because message has to be masked 250 | internets.print(mask); 251 | internets.print(masked); 252 | 253 | monitor(); // treat the response as input 254 | return true; 255 | } 256 | 257 | void SocketIOClient::getREST(String path) { 258 | String message = "GET /" + path + "/ HTTP/1.1"; 259 | internets.println(message); 260 | internets.print(F("Host: ")); internets.println(hostname); 261 | internets.println(F("Origin: Arduino")); 262 | internets.println(F("Connection: close\r\n")); 263 | } 264 | 265 | void SocketIOClient::postREST(String path, String type, String data) { 266 | String message = "POST /" + path + "/ HTTP/1.1"; 267 | internets.println(message); 268 | internets.print(F("Host: ")); internets.println(hostname); 269 | internets.println(F("Origin: Arduino")); 270 | internets.println(F("Connection: close\r\n")); 271 | internets.print(F("Content-Length: ")); internets.println(data.length()); 272 | internets.print(F("Content-Type: ")); internets.println(type); 273 | internets.println("\r\n"); 274 | internets.println(data); 275 | 276 | } 277 | 278 | void SocketIOClient::putREST(String path, String type, String data) { 279 | String message = "PUT /" + path + "/ HTTP/1.1"; 280 | internets.println(message); 281 | internets.print(F("Host: ")); internets.println(hostname); 282 | internets.println(F("Origin: Arduino")); 283 | internets.println(F("Connection: close\r\n")); 284 | internets.print(F("Content-Length: ")); internets.println(data.length()); 285 | internets.print(F("Content-Type: ")); internets.println(type); 286 | internets.println("\r\n"); 287 | internets.println(data); 288 | } 289 | 290 | void SocketIOClient::deleteREST(String path) { 291 | String message = "DELETE /" + path + "/ HTTP/1.1"; 292 | internets.println(message); 293 | internets.print(F("Host: ")); internets.println(hostname); 294 | internets.println(F("Origin: Arduino")); 295 | internets.println(F("Connection: close\r\n")); 296 | } 297 | 298 | void SocketIOClient::readLine() { 299 | for (int i = 0; i < DATA_BUFFER_LEN; i++) { databuffer[i] = ' '; } 300 | dataptr = databuffer; 301 | while (internets.available() && (dataptr < &databuffer[DATA_BUFFER_LEN - 2])){ 302 | char c = internets.read(); 303 | if (c == 0) {if (ioDebug) Serial.print("");} 304 | else if (c == 255) {if (ioDebug) Serial.println("");} 305 | else if (c == '\r') { ; } 306 | else if (c == '\n') break; 307 | else *dataptr++ = c; 308 | } 309 | *dataptr = 0; 310 | } 311 | 312 | 313 | void SocketIOClient::emit(String id, String data) { 314 | String message = "42[\"" + id + "\"," + data + "]"; 315 | if (ioDebug) Serial.println("[emit] " + message); 316 | int header[10]; 317 | header[0] = 0x81; 318 | int msglength = message.length(); 319 | if (ioDebug) Serial.println("[emit] " + String(msglength)); 320 | randomSeed(analogRead(0)); 321 | String mask = ""; 322 | String masked = message; 323 | for (int i = 0; i < 4; i++) { 324 | char a = random(48, 57); 325 | mask += a; 326 | } 327 | for (int i = 0; i < msglength; i++){ 328 | masked[i] = message[i] ^ mask[i % 4]; 329 | } 330 | 331 | internets.print((char)header[0]); // has to be sent for proper communication 332 | // Depending on the size of the message 333 | if (msglength <= 125) { 334 | header[1] = msglength + 128; 335 | internets.print((char)header[1]); //size of the message + 128 because message has to be masked 336 | } else if (msglength >= 126 && msglength <= 65535) { 337 | header[1] = 126 + 128; 338 | internets.print((char)header[1]); 339 | header[2] = (msglength >> 8) & 255; 340 | internets.print((char)header[2]); 341 | header[3] = (msglength)& 255; 342 | internets.print((char)header[3]); 343 | } else { 344 | header[1] = 127 + 128; 345 | internets.print((char)header[1]); 346 | header[2] = (msglength >> 56) & 255; 347 | internets.print((char)header[2]); 348 | header[3] = (msglength >> 48) & 255; 349 | internets.print((char)header[4]); 350 | header[4] = (msglength >> 40) & 255; 351 | internets.print((char)header[4]); 352 | header[5] = (msglength >> 32) & 255; 353 | internets.print((char)header[5]); 354 | header[6] = (msglength >> 24) & 255; 355 | internets.print((char)header[6]); 356 | header[7] = (msglength >> 16) & 255; 357 | internets.print((char)header[7]); 358 | header[8] = (msglength >> 8) & 255; 359 | internets.print((char)header[8]); 360 | header[9] = (msglength)& 255; 361 | internets.print((char)header[9]); 362 | } 363 | internets.print(mask); 364 | internets.print(masked); 365 | } 366 | 367 | void SocketIOClient::heartbeat(int select) { 368 | randomSeed(analogRead(0)); 369 | String mask = ""; 370 | String masked = ""; 371 | String message = ""; 372 | if (select == 0) { 373 | masked = "2"; 374 | message = "2"; 375 | } else { 376 | masked = "3"; 377 | message = "3"; 378 | } 379 | for (int i = 0; i < 4; i++) { //generate a random mask, 4 bytes, ASCII 0 to 9 380 | char a = random(48, 57); 381 | mask += a; 382 | } 383 | for (int i = 0; i < message.length(); i++){ 384 | masked[i] = message[i] ^ mask[i % 4]; //apply the "mask" to the message ("2" : ping or "3" : pong) 385 | } 386 | internets.print((char)0x81); //has to be sent for proper communication 387 | internets.print((char)129); //size of the message (1) + 128 because message has to be masked 388 | internets.print(mask); 389 | internets.print(masked); 390 | } 391 | 392 | void SocketIOClient::on(String id, functionPointer function) { 393 | if (onIndex == MAX_ON_HANDLERS){ return; } // oops... to many... 394 | onFunction[onIndex] = function; 395 | onId[onIndex] = id; 396 | onIndex++; 397 | } 398 | 399 | -------------------------------------------------------------------------------- /SocketIOClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | socket.io-arduino-client: a Socket.IO client for the Arduino 3 | Based on the Kevin Rohling WebSocketClient & Bill Roy Socket.io Lbrary 4 | Copyright 2015 Florent Vidal 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, 9 | copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the 11 | Software is furnished to do so, subject to the following 12 | conditions: 13 | JSON support added using https://github.com/bblanchon/ArduinoJson 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | 28 | /* 29 | * Modifications by RoboJay 30 | */ 31 | 32 | #ifndef SocketIoClient_H 33 | #define SocketIoClient_H 34 | 35 | #include "Arduino.h" 36 | 37 | #ifndef ioDebug 38 | #define ioDebug true 39 | #endif 40 | 41 | #ifdef W5100 42 | #include 43 | #include "SPI.h" //For W5100 44 | #endif 45 | 46 | #ifdef ENC28J60 47 | #include 48 | #include "SPI.h" //For ENC28J60 49 | #endif 50 | 51 | #ifdef ESP8266 52 | #include //For ESP8266 53 | #endif 54 | 55 | #if !defined(W5100) && !defined(ENC28J60) && !defined(ESP8266) //If no interface is defined 56 | #error "Please specify an interface such as W5100, ENC28J60, or ESP8266" 57 | #error "above your includes like so : #define ESP8266 " 58 | #endif 59 | 60 | // Length of static data buffers 61 | #define DATA_BUFFER_LEN 120 62 | #define SID_LEN 24 63 | 64 | // prototype for 'on' handlers 65 | // only dealing with string data at this point 66 | typedef void (*functionPointer)(String data); 67 | 68 | // Maxmimum number of 'on' handlers 69 | #define MAX_ON_HANDLERS 8 70 | 71 | #define MAX_HOSTNAME_LEN 128 72 | 73 | #define PING_INTERVAL 5000 74 | 75 | class SocketIOClient { 76 | public: 77 | SocketIOClient(); 78 | bool connect(String thehostname, int port = 80); 79 | bool connectHTTP(String thehostname, int port = 80); 80 | bool connected(); 81 | void disconnect(); 82 | bool reconnect(String thehostname, int port = 80); 83 | bool monitor(); 84 | void emit(String id, String data); 85 | void on(String id, functionPointer f); 86 | void heartbeat(int select); 87 | void getREST(String path); 88 | void postREST(String path, String type, String data); 89 | void putREST(String path, String type, String data); 90 | void deleteREST(String path); 91 | private: 92 | void eventHandler(int index); 93 | void sendHandshake(char hostname[]); 94 | 95 | //EthernetClient internets; //For ENC28J60 or W5100 96 | WiFiClient internets; //For ESP8266 97 | bool readHandshake(); 98 | void readLine(); 99 | char *dataptr; 100 | char databuffer[DATA_BUFFER_LEN]; 101 | char sid[SID_LEN]; 102 | char key[28]; 103 | char hostname[128]; 104 | int port; 105 | 106 | bool waitForInput(void); 107 | void eatHeader(void); 108 | 109 | functionPointer onFunction[MAX_ON_HANDLERS]; 110 | String onId[MAX_ON_HANDLERS]; 111 | uint8_t onIndex = 0; 112 | }; 113 | 114 | #endif // SocketIoClient_H 115 | -------------------------------------------------------------------------------- /examples/Hello_Time_ESP8266.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This sketch sends data via HTTP GET requests to data.sparkfun.com service. 3 | * 4 | * You need to get streamId and privateKey at data.sparkfun.com and paste them 5 | * below. Or just customize this script to talk to other HTTP servers. 6 | * 7 | */ 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | StaticJsonBuffer<200> jsonBuffer; 14 | 15 | 16 | SocketIOClient client; 17 | const char* ssid = "SSID"; 18 | const char* password = "WPA KEY"; 19 | 20 | char host[] = "192.168.0.5"; 21 | int port = 1234; 22 | extern String RID; 23 | extern String Rname; 24 | extern String Rcontent; 25 | 26 | unsigned long previousMillis = 0; 27 | long interval = 5000; 28 | unsigned long lastreply = 0; 29 | unsigned long lastsend = 0; 30 | String JSON; 31 | JsonObject& root = jsonBuffer.createObject(); 32 | void setup() { 33 | 34 | root["sensor"] = "gps"; 35 | root["time"] = 1351824120; 36 | JsonArray& data = root.createNestedArray("data"); 37 | data.add(double_with_n_digits(48.756080, 6)); 38 | data.add(double_with_n_digits(2.302038, 6)); 39 | root.printTo(JSON); 40 | Serial.begin(115200); 41 | delay(10); 42 | 43 | // We start by connecting to a WiFi network 44 | 45 | Serial.println(); 46 | Serial.println(); 47 | Serial.print("Connecting to "); 48 | Serial.println(ssid); 49 | 50 | WiFi.begin(ssid, password); 51 | 52 | while (WiFi.status() != WL_CONNECTED) { 53 | delay(500); 54 | Serial.print("."); 55 | } 56 | 57 | Serial.println(""); 58 | Serial.println("WiFi connected"); 59 | Serial.println("IP address: "); 60 | Serial.println(WiFi.localIP()); 61 | 62 | if (!client.connect(host, port)) { 63 | Serial.println("connection failed"); 64 | return; 65 | } 66 | if (client.connected()) 67 | { 68 | client.send("connection", "message", "Connected !!!!"); 69 | } 70 | } 71 | 72 | void loop() { 73 | unsigned long currentMillis = millis(); 74 | if (currentMillis - previousMillis > interval) 75 | { 76 | previousMillis = currentMillis; 77 | //client.heartbeat(0); 78 | 79 | 80 | 81 | 82 | Serial.println(JSON); 83 | client.send("atime", "message", "Time please?"); 84 | client.sendJSON("JSON", JSON); 85 | 86 | lastsend = millis(); 87 | } 88 | if (client.monitor()) 89 | { 90 | lastreply = millis(); 91 | Serial.println(RID); 92 | if (RID == "atime" && Rname == "time") 93 | { 94 | Serial.print("Il est "); 95 | Serial.println(Rcontent); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /examples/Hello_time.ino: -------------------------------------------------------------------------------- 1 | #include "SocketIOClient.h" 2 | #include "Ethernet.h" 3 | #include "SPI.h" 4 | 5 | SocketIOClient client; 6 | 7 | byte mac[] = { 0xAA, 0x00, 0xBE, 0xEF, 0xFE, 0xEE }; 8 | char hostname[] = "192.168.0.5"; 9 | int port = 1234; 10 | 11 | extern String RID; 12 | extern String Rname; 13 | extern String Rcontent; 14 | 15 | unsigned long previousMillis = 0; 16 | long interval = 10000; 17 | void setup() { 18 | pinMode(10, OUTPUT); //for some ethernet shield on the mega : disables the SD and enables the W5100 19 | digitalWrite(10, LOW); 20 | pinMode(4, OUTPUT); 21 | digitalWrite(4, HIGH); 22 | Serial.begin(9600); 23 | 24 | Ethernet.begin(mac); 25 | 26 | if (!client.connect(hostname, port)) 27 | Serial.println(F("Not connected.")); 28 | if (client.connected()) 29 | { 30 | client.send("connection", "message", "Connected !!!!"); 31 | } 32 | else 33 | { 34 | Serial.println("Connection Error"); 35 | while(1); 36 | } 37 | delay(1000); 38 | } 39 | 40 | void loop() 41 | { 42 | unsigned long currentMillis = millis(); 43 | if(currentMillis - previousMillis > interval) 44 | { 45 | previousMillis = currentMillis; 46 | //client.heartbeat(0); 47 | client.send("atime", "message", "Time please?"); 48 | } 49 | if (client.monitor()) 50 | { 51 | Serial.println(RID); 52 | if (RID == "atime" && Rname == "time") 53 | { 54 | Serial.print("Time is "); 55 | Serial.println(Rcontent); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /examples/Hello_time_sparkfun.ino: -------------------------------------------------------------------------------- 1 | /* DISCLAIMER : IT MAY NOT WORK... 2 | * This sketch sends data via HTTP GET requests to data.sparkfun.com service. 3 | * 4 | * You need to get streamId and privateKey at data.sparkfun.com and paste them 5 | * below. Or just customize this script to talk to other HTTP servers. 6 | * 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | StaticJsonBuffer<200> jsonBuffer; 16 | int connectedWF = 0; 17 | 18 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 19 | char server[] = "firma-transport.ro"; 20 | 21 | SocketIOClient client; 22 | const char* ssid = "SSID"; 23 | const char* password = "WPA KEY"; 24 | 25 | char host[] = "192.168.0.5"; 26 | int port = 1234; 27 | extern String RID; 28 | extern String Rname; 29 | extern String Rcontent; 30 | 31 | unsigned long previousMillis = 0; 32 | long interval = 5000; 33 | unsigned long lastreply = 0; 34 | unsigned long lastsend = 0; 35 | String JSON; 36 | JsonObject& root = jsonBuffer.createObject(); 37 | void setup() { 38 | 39 | if (esp8266.begin()) { 40 | Serial.println("ESP8266 ready to go!"); 41 | connectToWifi(); 42 | } 43 | else { 44 | Serial.println("Unable to communicate with the ESP8266 :("); 45 | 46 | 47 | delay(2000); 48 | 49 | root["sensor"] = "gps"; 50 | root["time"] = 1351824120; 51 | JsonArray& data = root.createNestedArray("data"); 52 | data.add(double_with_n_digits(48.756080, 6)); 53 | data.add(double_with_n_digits(2.302038, 6)); 54 | root.printTo(JSON); 55 | Serial.begin(115200); 56 | delay(10); 57 | 58 | // We start by connecting to a WiFi network 59 | 60 | Serial.println(); 61 | Serial.println(); 62 | Serial.print("Connecting to "); 63 | Serial.println(ssid); 64 | 65 | WiFi.begin(ssid, password); 66 | 67 | while (WiFi.status() != WL_CONNECTED) { 68 | delay(500); 69 | Serial.print("."); 70 | } 71 | 72 | Serial.println(""); 73 | Serial.println("WiFi connected"); 74 | Serial.println("IP address: "); 75 | Serial.println(WiFi.localIP()); 76 | 77 | if (!client.connect(host, port)) { 78 | Serial.println("connection failed"); 79 | return; 80 | } 81 | if (client.connected()) 82 | { 83 | client.send("connection", "message", "Connected !!!!"); 84 | } 85 | } 86 | 87 | void connectToWifi() { 88 | int retVal; 89 | retVal = esp8266.connect("ssid", "pass"); 90 | if (retVal < 0) 91 | { 92 | Serial.println("Not connected"); 93 | } 94 | else { 95 | IPAddress myIP = esp8266.localIP(); 96 | Serial.println("Connected to internet OK"); 97 | connectedWF = 1; 98 | } 99 | } 100 | 101 | 102 | 103 | 104 | void loop() { 105 | if ( connectedWF == 0 ) { 106 | connectToWifi(); 107 | } 108 | unsigned long currentMillis = millis(); 109 | if (currentMillis - previousMillis > interval) 110 | { 111 | previousMillis = currentMillis; 112 | //client.heartbeat(0); 113 | 114 | 115 | 116 | 117 | Serial.println(JSON); 118 | client.send("atime", "message", "Time please?"); 119 | client.sendJSON("JSON", JSON); 120 | 121 | lastsend = millis(); 122 | } 123 | if (client.monitor()) 124 | { 125 | lastreply = millis(); 126 | Serial.println(RID); 127 | if (RID == "atime" && Rname == "time") 128 | { 129 | Serial.print("Il est "); 130 | Serial.println(Rcontent); 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /examples/app.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | var app = require('http').createServer(handler); 3 | var io = require('socket.io')(app); 4 | var fs = require('fs'); 5 | app.listen(3484); 6 | 7 | function handler (req, res) { 8 | fs.readFile(__dirname + '/index.html', 9 | function (err, data) { 10 | if (err) { 11 | res.writeHead(500); 12 | return res.end('Error loading index.html'); 13 | } 14 | 15 | res.writeHead(200); 16 | res.end(data); 17 | }); 18 | } 19 | function ParseJson(jsondata) { 20 | try { 21 | return JSON.parse(jsondata); 22 | } catch (error) { 23 | return null; 24 | } 25 | } 26 | function sendTime() { 27 | io.sockets.emit('atime', { time: new Date().toJSON() }); 28 | } 29 | io.on('connection', function (socket) { 30 | console.log("Connected"); 31 | socket.emit('welcome', { message: 'Connected !!!!' }); 32 | socket.on('connection', function (data) { 33 | console.log(data); }); 34 | socket.on('atime', function (data) { 35 | sendTime(); 36 | console.log(data); 37 | }); 38 | socket.on('JSON', function (data) { 39 | // console.log(data); 40 | var jsonStr = JSON.stringify(data); 41 | var parsed = ParseJson(jsonStr); 42 | console.log(parsed); 43 | console.log(parsed.sensor); 44 | }); 45 | socket.on('arduino', function (data) { 46 | io.sockets.emit('arduino', { message: 'R0' }); 47 | console.log(data); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /examples/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 30 | 31 | 32 | 33 |
    34 | 35 | 36 | 37 | --------------------------------------------------------------------------------