WebSocketClientSocketIOack.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * WebSocketClientSocketIOack.ino
  3. *
  4. * Created on: 20.07.2019
  5. *
  6. */
  7. #include <Arduino.h>
  8. #include <WiFi.h>
  9. #include <WiFiMulti.h>
  10. #include <WiFiClientSecure.h>
  11. #include <ArduinoJson.h>
  12. #include <WebSocketsClient.h>
  13. #include <SocketIOclient.h>
  14. WiFiMulti 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. break;
  64. case sIOtype_ERROR:
  65. USE_SERIAL.printf("[IOc] get error: %u\n", length);
  66. break;
  67. case sIOtype_BINARY_EVENT:
  68. USE_SERIAL.printf("[IOc] get binary: %u\n", length);
  69. break;
  70. case sIOtype_BINARY_ACK:
  71. USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
  72. break;
  73. }
  74. }
  75. void setup() {
  76. //USE_SERIAL.begin(921600);
  77. USE_SERIAL.begin(115200);
  78. //Serial.setDebugOutput(true);
  79. USE_SERIAL.setDebugOutput(true);
  80. USE_SERIAL.println();
  81. USE_SERIAL.println();
  82. USE_SERIAL.println();
  83. for(uint8_t t = 4; t > 0; t--) {
  84. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  85. USE_SERIAL.flush();
  86. delay(1000);
  87. }
  88. WiFiMulti.addAP("SSID", "passpasspass");
  89. //WiFi.disconnect();
  90. while(WiFiMulti.run() != WL_CONNECTED) {
  91. delay(100);
  92. }
  93. String ip = WiFi.localIP().toString();
  94. USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
  95. // server address, port and URL
  96. socketIO.begin("10.11.100.100", 8880, "/socket.io/?EIO=4");
  97. // event handler
  98. socketIO.onEvent(socketIOEvent);
  99. }
  100. unsigned long messageTimestamp = 0;
  101. void loop() {
  102. socketIO.loop();
  103. uint64_t now = millis();
  104. if(now - messageTimestamp > 2000) {
  105. messageTimestamp = now;
  106. // creat JSON message for Socket.IO (event)
  107. DynamicJsonDocument doc(1024);
  108. JsonArray array = doc.to<JsonArray>();
  109. // add evnet name
  110. // Hint: socket.on('event_name', ....
  111. array.add("event_name");
  112. // add payload (parameters) for the event
  113. JsonObject param1 = array.createNestedObject();
  114. param1["now"] = (uint32_t) now;
  115. // JSON to String (serializion)
  116. String output;
  117. serializeJson(doc, output);
  118. // Send event
  119. socketIO.sendEVENT(output);
  120. // Print JSON for debugging
  121. USE_SERIAL.println(output);
  122. }
  123. }