WebSocketClientSocketIOack.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * WebSocketClientSocketIOack.ino
  3. *
  4. * Created on: 20.07.2019
  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 Serial
  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. {
  29. char * sptr = NULL;
  30. int id = strtol((char *)payload, &sptr, 10);
  31. USE_SERIAL.printf("[IOc] get event: %s id: %d\n", payload, id);
  32. if(id) {
  33. payload = (uint8_t *)sptr;
  34. }
  35. DynamicJsonDocument doc(1024);
  36. DeserializationError error = deserializeJson(doc, payload, length);
  37. if(error) {
  38. USE_SERIAL.print(F("deserializeJson() failed: "));
  39. USE_SERIAL.println(error.c_str());
  40. return;
  41. }
  42. String eventName = doc[0];
  43. USE_SERIAL.printf("[IOc] event name: %s\n", eventName.c_str());
  44. // Message Includes a ID for a ACK (callback)
  45. if(id) {
  46. // creat JSON message for Socket.IO (ack)
  47. DynamicJsonDocument docOut(1024);
  48. JsonArray array = docOut.to<JsonArray>();
  49. // add payload (parameters) for the ack (callback function)
  50. JsonObject param1 = array.createNestedObject();
  51. param1["now"] = millis();
  52. // JSON to String (serializion)
  53. String output;
  54. output += id;
  55. serializeJson(docOut, output);
  56. // Send event
  57. socketIO.send(sIOtype_ACK, output);
  58. }
  59. }
  60. break;
  61. case sIOtype_ACK:
  62. USE_SERIAL.printf("[IOc] get ack: %u\n", length);
  63. hexdump(payload, length);
  64. break;
  65. case sIOtype_ERROR:
  66. USE_SERIAL.printf("[IOc] get error: %u\n", length);
  67. hexdump(payload, length);
  68. break;
  69. case sIOtype_BINARY_EVENT:
  70. USE_SERIAL.printf("[IOc] get binary: %u\n", length);
  71. hexdump(payload, length);
  72. break;
  73. case sIOtype_BINARY_ACK:
  74. USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
  75. hexdump(payload, length);
  76. break;
  77. }
  78. }
  79. void setup() {
  80. //USE_SERIAL.begin(921600);
  81. USE_SERIAL.begin(115200);
  82. //Serial.setDebugOutput(true);
  83. USE_SERIAL.setDebugOutput(true);
  84. USE_SERIAL.println();
  85. USE_SERIAL.println();
  86. USE_SERIAL.println();
  87. for(uint8_t t = 4; t > 0; t--) {
  88. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  89. USE_SERIAL.flush();
  90. delay(1000);
  91. }
  92. // disable AP
  93. if(WiFi.getMode() & WIFI_AP) {
  94. WiFi.softAPdisconnect(true);
  95. }
  96. WiFiMulti.addAP("SSID", "passpasspass");
  97. //WiFi.disconnect();
  98. while(WiFiMulti.run() != WL_CONNECTED) {
  99. delay(100);
  100. }
  101. String ip = WiFi.localIP().toString();
  102. USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
  103. // server address, port and URL
  104. socketIO.begin("10.11.100.100", 8880, "/socket.io/?EIO=4");
  105. // event handler
  106. socketIO.onEvent(socketIOEvent);
  107. }
  108. unsigned long messageTimestamp = 0;
  109. void loop() {
  110. socketIO.loop();
  111. uint64_t now = millis();
  112. if(now - messageTimestamp > 2000) {
  113. messageTimestamp = now;
  114. // creat JSON message for Socket.IO (event)
  115. DynamicJsonDocument doc(1024);
  116. JsonArray array = doc.to<JsonArray>();
  117. // add evnet name
  118. // Hint: socket.on('event_name', ....
  119. array.add("event_name");
  120. // add payload (parameters) for the event
  121. JsonObject param1 = array.createNestedObject();
  122. param1["now"] = (uint32_t) now;
  123. // JSON to String (serializion)
  124. String output;
  125. serializeJson(doc, output);
  126. // Send event
  127. socketIO.sendEVENT(output);
  128. // Print JSON for debugging
  129. USE_SERIAL.println(output);
  130. }
  131. }