8000 add Socket.IO example for ESP32 · thebigG/arduinoWebSockets@f8da05a · GitHub
[go: up one dir, main page]

Skip to content

Commit f8da05a

Browse files
committed
add Socket.IO example for ESP32
1 parent 72aae52 commit f8da05a

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* WebSocketClientSocketIOack.ino
3+
*
4+
* Created on: 20.07.2019
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <WiFi.h>
11+
#include <WiFiMulti.h>
12+
#include <WiFiClientSecure.h>
13+
14+
#include <ArduinoJson.h>
15+
16+
#include <WebSocketsClient.h>
17+
#include <SocketIOclient.h>
18+
19+
WiFiMulti WiFiMulti;
20+
SocketIOclient socketIO;
21+
22+
#define USE_SERIAL Serial
23+
24+
25+
void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
26+
switch(type) {
27+
case sIOtype_DISCONNECT:
28+
USE_SERIAL.printf("[IOc] Disconnected!\n");
29+
break;
30+
case sIOtype_CONNECT:
31+
USE_SERIAL.printf("[IOc] Connected to url: %s\n", payload);
32+
33+
// join default namespace (no auto join in Socket.IO V3)
34+
socketIO.send(sIOtype_CONNECT, "/");
35+
break;
36+
case sIOtype_EVENT:
37+
{
38+
char * sptr = NULL;
39+
int id = strtol((char *)payload, &sptr, 10);
40+
USE_SERIAL.printf("[IOc] get event: %s id: %d\n", payload, id);
41+
if(id) {
42+
payload = (uint8_t *)sptr;
43+
}
44+
DynamicJsonDocument doc(1024);
45+
DeserializationError error = deserializeJson(doc, payload, length);
46+
if(error) {
47+
USE_SERIAL.print 10000 (F("deserializeJson() failed: "));
48+
USE_SERIAL.println(error.c_str());
49+
return;
50+
}
51+
52+
String eventName = doc[0];
53+
USE_SERIAL.printf("[IOc] event name: %s\n", eventName.c_str());
54+
55+
// Message Includes a ID for a ACK (callback)
56+
if(id) {
57+
// creat JSON message for Socket.IO (ack)
58+
DynamicJsonDocument docOut(1024);
59+
JsonArray array = docOut.to<JsonArray>();
60+
61+
// add payload (parameters) for the ack (callback function)
62+
JsonObject param1 = array.createNestedObject();
63+
param1["now"] = millis();
64+
65+
// JSON to String (serializion)
66+
String output;
67+
output += id;
68+
serializeJson(docOut, output);
69+
70+
// Send event
71+
socketIO.send(sIOtype_ACK, output);
72+
}
73+
}
74+
break;
75+
case sIOtype_ACK:
76+
USE_SERIAL.printf("[IOc] get ack: %u\n", length);
77+
break;
78+
case sIOtype_ERROR:
79+
USE_SERIAL.printf("[IOc] get error: %u\n", length);
80+
break;
81+
case sIOtype_BINARY_EVENT:
82+
USE_SERIAL.printf("[IOc] get binary: %u\n", length);
83+
break;
84+
case sIOtype_BINARY_ACK:
85+
USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
86+
break;
87+
}
88+
}
89+
90+
void setup() {
91+
//USE_SERIAL.begin(921600);
92+
USE_SERIAL.begin(115200);
93+
94+
//Serial.setDebugOutput(true);
95+
USE_SERIAL.setDebugOutput(true);
96+
97+
USE_SERIAL.println();
98+
USE_SERIAL.println();
99+
USE_SERIAL.println();
100+
101+
for(uint8_t t = 4; t > 0; t--) {
102+
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
103+
USE_SERIAL.flush();
104+
delay(1000);
105+
}
106+
107+
WiFiMulti.addAP("SSID", "passpasspass");
108+
109+
//WiFi.disconnect();
110+
while(WiFiMulti.run() != WL_CONNECTED) {
111+
delay(100);
112+
}
113+
114+
String ip = WiFi.localIP().toString();
115+
USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
116+
117+
// server address, port and URL
118+
socketIO.begin("10.11.100.100", 8880, "/socket.io/?EIO=4");
119+
120+
// event handler
121+
socketIO.onEvent(socketIOEvent);
122+
}
123+
124+
unsigned long messageTimestamp = 0;
125+
void loop() {
126+
socketIO.loop();
127+
128+
uint64_t now = millis();
129+
130+
if(now - messageTimestamp > 2000) {
131+
messageTimestamp = now;
132+
133+
// creat JSON message for Socket.IO (event)
134+
DynamicJsonDocument doc(1024);
135+
JsonArray array = doc.to<JsonArray>();
136+
137+
// add evnet name
138+
// Hint: socket.on('event_name', ....
139+
array.add("event_name");
140+
141+
// add payload (parameters) for the event
142+
JsonObject param1 = array.createNestedObject();
143+
param1["now"] = (uint32_t) now;
144+
145+
// JSON to String (serializion)
146+
String output;
147+
serializeJson(doc, output);
148+
149+
// Send event
150+
socketIO.sendEVENT(output);
151+
152+
// Print JSON for debugging
153+
USE_SERIAL.println(output);
154+
}
155+
}

0 commit comments

Comments
 (0)
0