WebSocketClientSocketIO.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // join default namespace (no auto join in Socket.IO V3)
  25. socketIO.send(sIOtype_CONNECT, "/");
  26. break;
  27. case sIOtype_EVENT:
  28. USE_SERIAL.printf("[IOc] get event: %s\n", payload);
  29. break;
  30. case sIOtype_ACK:
  31. USE_SERIAL.printf("[IOc] get ack: %u\n", length);
  32. hexdump(payload, length);
  33. break;
  34. case sIOtype_ERROR:
  35. USE_SERIAL.printf("[IOc] get error: %u\n", length);
  36. hexdump(payload, length);
  37. break;
  38. case sIOtype_BINARY_EVENT:
  39. USE_SERIAL.printf("[IOc] get binary: %u\n", length);
  40. hexdump(payload, length);
  41. break;
  42. case sIOtype_BINARY_ACK:
  43. USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
  44. hexdump(payload, length);
  45. break;
  46. }
  47. }
  48. void setup() {
  49. // USE_SERIAL.begin(921600);
  50. USE_SERIAL.begin(115200);
  51. //Serial.setDebugOutput(true);
  52. USE_SERIAL.setDebugOutput(true);
  53. USE_SERIAL.println();
  54. USE_SERIAL.println();
  55. USE_SERIAL.println();
  56. for(uint8_t t = 4; t > 0; t--) {
  57. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  58. USE_SERIAL.flush();
  59. delay(1000);
  60. }
  61. // disable AP
  62. if(WiFi.getMode() & WIFI_AP) {
  63. WiFi.softAPdisconnect(true);
  64. }
  65. WiFiMulti.addAP("SSID", "passpasspass");
  66. //WiFi.disconnect();
  67. while(WiFiMulti.run() != WL_CONNECTED) {
  68. delay(100);
  69. }
  70. String ip = WiFi.localIP().toString();
  71. USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
  72. // server address, port and URL
  73. socketIO.begin("10.11.100.100", 8880, "/socket.io/?EIO=4");
  74. // event handler
  75. socketIO.onEvent(socketIOEvent);
  76. }
  77. unsigned long messageTimestamp = 0;
  78. void loop() {
  79. socketIO.loop();
  80. uint64_t now = millis();
  81. if(now - messageTimestamp > 2000) {
  82. messageTimestamp = now;
  83. // creat JSON message for Socket.IO (event)
  84. DynamicJsonDocument doc(1024);
  85. JsonArray array = doc.to<JsonArray>();
  86. // add evnet name
  87. // Hint: socket.on('event_name', ....
  88. array.add("event_name");
  89. // add payload (parameters) for the event
  90. JsonObject param1 = array.createNestedObject();
  91. param1["now"] = (uint32_t) now;
  92. // JSON to String (serializion)
  93. String output;
  94. serializeJson(doc, output);
  95. // Send event
  96. socketIO.sendEVENT(output);
  97. // Print JSON for debugging
  98. USE_SERIAL.println(output);
  99. }
  100. }