10
10
#include < ESP8266WiFi.h>
11
11
#include < ESP8266WiFiMulti.h>
12
12
13
+ #include < ArduinoJson.h>
14
+
13
15
#include < WebSocketsClient.h>
14
16
15
17
#include < Hash.h>
16
18
17
19
ESP8266WiFiMulti WiFiMulti;
18
- WebSocketsClient webSocket;
19
-
20
+ SocketIOclient socketIO;
20
21
21
22
#define USE_SERIAL Serial1
22
23
23
- #define MESSAGE_INTERVAL 30000
24
- #define HEARTBEAT_INTERVAL 25000
25
-
26
- uint64_t messageTimestamp = 0 ;
27
- uint64_t heartbeatTimestamp = 0 ;
28
- bool isConnected = false ;
29
-
30
- void webSocketEvent (WStype_t type, uint8_t * payload, size_t length) {
31
-
32
-
24
+ void socketIOEvent (socketIOmessageType_t type, uint8_t * payload, size_t length) {
33
25
switch (type) {
34
- case WStype_DISCONNECTED:
35
- USE_SERIAL.printf (" [WSc] Disconnected!\n " );
36
- isConnected = false ;
26
+ case sIOtype_DISCONNECT :
27
+ USE_SERIAL.printf (" [IOc] Disconnected!\n " );
37
28
break ;
38
- case WStype_CONNECTED:
39
- {
40
- USE_SERIAL.printf (" [WSc] Connected to url: %s\n " , payload);
41
- isConnected = true ;
42
-
43
- // send message to server when Connected
44
- // socket.io upgrade confirmation message (required)
45
- webSocket.sendTXT (" 5" );
46
- }
29
+ case sIOtype_CONNECT :
30
+ USE_SERIAL.printf (" [IOc] Connected to url: %s\n " , payload);
47
31
break ;
48
- case WStype_TEXT:
49
- USE_SERIAL.printf (" [WSc] get text: %s\n " , payload);
50
-
51
- // send message to server
52
- // webSocket.sendTXT("message here");
32
+ case sIOtype_EVENT :
33
+ USE_SERIAL.printf (" [IOc] get event: %s\n " , payload);
53
34
break ;
54
- case WStype_BIN:
55
- USE_SERIAL.printf (" [WSc] get binary length: %u\n " , length);
35
+ case sIOtype_ACK :
36
+ USE_SERIAL.printf (" [IOc] get ack: %u\n " , length);
37
+ hexdump (payload, length);
38
+ break ;
39
+ case sIOtype_ERROR :
40
+ USE_SERIAL.printf (" [IOc] get error: %u\n " , length);
41
+ hexdump (payload, length);
42
+ break ;
43
+ case sIOtype_BINARY_EVENT :
44
+ USE_SERIAL.printf (" [IOc] get binary: %u\n " , length);
45
+ hexdump (payload, length);
46
+ break ;
47
+ case sIOtype_BINARY_ACK :
48
+ USE_SERIAL.printf (" [IOc] get binary ack: %u\n " , length);
56
49
hexdump (payload, length);
57
-
58
- // send data to server
59
- // webSocket.sendBIN(payload, length);
60
50
break ;
61
51
}
62
-
63
52
}
64
53
65
54
void setup () {
@@ -79,35 +68,57 @@ void setup() {
79
68
delay (1000 );
80
69
}
81
70
71
+ // disable AP
72
+ if (WiFi.getMode () & WIFI_AP) {
73
+ WiFi.softAPdisconnect (true );
74
+ }
75
+
82
76
WiFiMulti.addAP (" SSID" , " passpasspass" );
83
77
84
78
// WiFi.disconnect();
85
79
while (WiFiMulti.run () != WL_CONNECTED) {
86
80
delay (100 );
87
81
}
88
82
89
- webSocket.beginSocketIO (" 192.168.0.123" , 81 );
90
- // webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization
91
- webSocket.onEvent (webSocketEvent);
83
+ String ip = WiFi.localIP ().toString ();
84
+ USE_SERIAL.printf (" [SETUP] WiFi Connected %s\n " , ip.c_str ());
85
+
86
+ // server address, port and URL
87
+ socketIO.begin (" 10.11.100.100" , 8880 );
92
88
89
+ // event handler
90
+ socketIO.onEvent (socketIOEvent);
93
91
}
94
92
93
+ unsigned long messageTimestamp = 0 ;
95
94
void loop () {
96
- webSocket.loop ();
97
-
98
- if (isConnected) {
99
-
100
- uint64_t now = millis ();
101
-
102
- if (now - messageTimestamp > MESSAGE_INTERVAL) {
103
- messageTimestamp = now;
104
- // example socket.io message with type "messageType" and JSON payload
105
- webSocket.sendTXT (" 42[\" messageType\" ,{\" greeting\" :\" hello\" }]" );
106
- }
107
- if ((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
108
- heartbeatTimestamp = now;
109
- // socket.io heartbeat message
110
- webSocket.sendTXT (" 2" );
111
- }
95
+ socketIO.loop ();
96
+
97
+ uint64_t now = millis ();
98
+
99
+ if (now - messageTimestamp > 2000 ) {
100
+ messageTimestamp = now;
101
+
102
+ // creat JSON message for Socket.IO (event)
103
+ DynamicJsonDocument doc (1024 );
104
+ JsonArray array = doc.to <JsonArray>();
105
+
106
+ // add evnet name
107
+ // Hint: socket.on('event_name', ....
108
+ array.add (" event_name" );
109
+
110
+ // add payload (parameters) for the event
111
+ JsonObject param1 = array.createNestedObject ();
112
+ param1[" now" ] = now;
113
+
114
+ // JSON to String (serializion)
115
+ String output;
116
+ serializeJson (doc, output);
117
+
118
+ // Send event
119
+ socketIO.sendEVENT (output);
120
+
121
+ // Print JSON for debugging
122
+ USE_SERIAL.println (output);
112
123
}
113
124
}
0 commit comments