WebSocketClientSocketIO.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * WebSocketClientSocketIO.ino
  3. *
  4. * Created on: 06.06.2016
  5. *
  6. */
  7. #include <Arduino.h>
  8. #include <ESP8266WiFi.h>
  9. #include <ESP8266WiFiMulti.h>
  10. #include <ArduinoJson.h>
  11. #include <WebSocketsClient.h>
  12. #include <SocketIOclient.h>
  13. #include <Hash.h>
  14. ESP8266WiFiMulti WiFiMulti;
  15. SocketIOclient socketIO;
  16. #define USE_SERIAL Serial1
  17. void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
  18. switch(type) {
  19. case sIOtype_DISCONNECT:
  20. USE_SERIAL.printf("[IOc] Disconnected!\n");
  21. break;
  22. case sIOtype_CONNECT:
  23. USE_SERIAL.printf("[IOc] Connected to url: %s\n", payload);
  24. break;
  25. case sIOtype_EVENT:
  26. USE_SERIAL.printf("[IOc] get event: %s\n", payload);
  27. break;
  28. case sIOtype_ACK:
  29. USE_SERIAL.printf("[IOc] get ack: %u\n", length);
  30. hexdump(payload, length);
  31. break;
  32. case sIOtype_ERROR:
  33. USE_SERIAL.printf("[IOc] get error: %u\n", length);
  34. hexdump(payload, length);
  35. break;
  36. case sIOtype_BINARY_EVENT:
  37. USE_SERIAL.printf("[IOc] get binary: %u\n", length);
  38. hexdump(payload, length);
  39. break;
  40. case sIOtype_BINARY_ACK:
  41. USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
  42. hexdump(payload, length);
  43. break;
  44. }
  45. }
  46. void setup() {
  47. // USE_SERIAL.begin(921600);
  48. USE_SERIAL.begin(115200);
  49. //Serial.setDebugOutput(true);
  50. USE_SERIAL.setDebugOutput(true);
  51. USE_SERIAL.println();
  52. USE_SERIAL.println();
  53. USE_SERIAL.println();
  54. for(uint8_t t = 4; t > 0; t--) {
  55. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  56. USE_SERIAL.flush();
  57. delay(1000);
  58. }
  59. // disable AP
  60. if(WiFi.getMode() & WIFI_AP) {
  61. WiFi.softAPdisconnect(true);
  62. }
  63. WiFiMulti.addAP("SSID", "passpasspass");
  64. //WiFi.disconnect();
  65. while(WiFiMulti.run() != WL_CONNECTED) {
  66. delay(100);
  67. }
  68. String ip = WiFi.localIP().toString();
  69. USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
  70. // server address, port and URL
  71. socketIO.begin("10.11.100.100", 8880);
  72. // event handler
  73. socketIO.onEvent(socketIOEvent);
  74. }
  75. unsigned long messageTimestamp = 0;
  76. void loop() {
  77. socketIO.loop();
  78. uint64_t now = millis();
  79. if(now - messageTimestamp > 2000) {
  80. messageTimestamp = now;
  81. // creat JSON message for Socket.IO (event)
  82. DynamicJsonDocument doc(1024);
  83. JsonArray array = doc.to<JsonArray>();
  84. // add evnet name
  85. // Hint: socket.on('event_name', ....
  86. array.add("event_name");
  87. // add payload (parameters) for the event
  88. JsonObject param1 = array.createNestedObject();
  89. param1["now"] = now;
  90. // JSON to String (serializion)
  91. String output;
  92. serializeJson(doc, output);
  93. // Send event
  94. socketIO.sendEVENT(output);
  95. // Print JSON for debugging
  96. USE_SERIAL.println(output);
  97. }
  98. }