Skip to content

Commit 7816bb3

Browse files
committed
adding example for new socket.IO support
1 parent adf2b07 commit 7816bb3

File tree

2 files changed

+69
-54
lines changed

2 files changed

+69
-54
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ script:
2828
- export PATH="$HOME/arduino_ide:$PATH"
2929
- which arduino
3030
- mkdir -p $HOME/Arduino/libraries
31+
32+
- wget https://github.com/bblanchon/ArduinoJson/archive/6.x.zip
33+
- unzip 6.x.zip
34+
- mv ArduinoJson-6.x $HOME/Arduino/libraries/ArduinoJson
3135
- cp -r $TRAVIS_BUILD_DIR $HOME/Arduino/libraries/arduinoWebSockets
3236
- source $TRAVIS_BUILD_DIR/travis/common.sh
3337
- get_core $CPU

examples/esp8266/WebSocketClientSocketIO/WebSocketClientSocketIO.ino

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,45 @@
1010
#include <ESP8266WiFi.h>
1111
#include <ESP8266WiFiMulti.h>
1212

13+
#include <ArduinoJson.h>
14+
1315
#include <WebSocketsClient.h>
1416

1517
#include <Hash.h>
1618

1719
ESP8266WiFiMulti WiFiMulti;
18-
WebSocketsClient webSocket;
19-
20+
SocketIOclient socketIO;
2021

2122
#define USE_SERIAL Serial1
2223

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) {
3325
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");
3728
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);
4731
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);
5334
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);
5649
hexdump(payload, length);
57-
58-
// send data to server
59-
// webSocket.sendBIN(payload, length);
6050
break;
6151
}
62-
6352
}
6453

6554
void setup() {
@@ -79,35 +68,57 @@ void setup() {
7968
delay(1000);
8069
}
8170

71+
// disable AP
72+
if(WiFi.getMode() & WIFI_AP) {
73+
WiFi.softAPdisconnect(true);
74+
}
75+
8276
WiFiMulti.addAP("SSID", "passpasspass");
8377

8478
//WiFi.disconnect();
8579
while(WiFiMulti.run() != WL_CONNECTED) {
8680
delay(100);
8781
}
8882

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);
9288

89+
// event handler
90+
socketIO.onEvent(socketIOEvent);
9391
}
9492

93+
unsigned long messageTimestamp = 0;
9594
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);
112123
}
113124
}

0 commit comments

Comments
 (0)