forked from Links2004/arduinoWebSockets
adding example for new socket.IO support
This commit is contained in:
@ -28,6 +28,10 @@ script:
|
|||||||
- export PATH="$HOME/arduino_ide:$PATH"
|
- export PATH="$HOME/arduino_ide:$PATH"
|
||||||
- which arduino
|
- which arduino
|
||||||
- mkdir -p $HOME/Arduino/libraries
|
- mkdir -p $HOME/Arduino/libraries
|
||||||
|
|
||||||
|
- wget https://github.com/bblanchon/ArduinoJson/archive/6.x.zip
|
||||||
|
- unzip 6.x.zip
|
||||||
|
- mv ArduinoJson-6.x $HOME/Arduino/libraries/ArduinoJson
|
||||||
- cp -r $TRAVIS_BUILD_DIR $HOME/Arduino/libraries/arduinoWebSockets
|
- cp -r $TRAVIS_BUILD_DIR $HOME/Arduino/libraries/arduinoWebSockets
|
||||||
- source $TRAVIS_BUILD_DIR/travis/common.sh
|
- source $TRAVIS_BUILD_DIR/travis/common.sh
|
||||||
- get_core $CPU
|
- get_core $CPU
|
||||||
|
@ -10,56 +10,45 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WiFiMulti.h>
|
#include <ESP8266WiFiMulti.h>
|
||||||
|
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
#include <WebSocketsClient.h>
|
#include <WebSocketsClient.h>
|
||||||
|
|
||||||
#include <Hash.h>
|
#include <Hash.h>
|
||||||
|
|
||||||
ESP8266WiFiMulti WiFiMulti;
|
ESP8266WiFiMulti WiFiMulti;
|
||||||
WebSocketsClient webSocket;
|
SocketIOclient socketIO;
|
||||||
|
|
||||||
|
|
||||||
#define USE_SERIAL Serial1
|
#define USE_SERIAL Serial1
|
||||||
|
|
||||||
#define MESSAGE_INTERVAL 30000
|
void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
|
||||||
#define HEARTBEAT_INTERVAL 25000
|
|
||||||
|
|
||||||
uint64_t messageTimestamp = 0;
|
|
||||||
uint64_t heartbeatTimestamp = 0;
|
|
||||||
bool isConnected = false;
|
|
||||||
|
|
||||||
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
|
||||||
|
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case WStype_DISCONNECTED:
|
case sIOtype_DISCONNECT:
|
||||||
USE_SERIAL.printf("[WSc] Disconnected!\n");
|
USE_SERIAL.printf("[IOc] Disconnected!\n");
|
||||||
isConnected = false;
|
|
||||||
break;
|
break;
|
||||||
case WStype_CONNECTED:
|
case sIOtype_CONNECT:
|
||||||
{
|
USE_SERIAL.printf("[IOc] Connected to url: %s\n", payload);
|
||||||
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
|
|
||||||
isConnected = true;
|
|
||||||
|
|
||||||
// send message to server when Connected
|
|
||||||
// socket.io upgrade confirmation message (required)
|
|
||||||
webSocket.sendTXT("5");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case WStype_TEXT:
|
case sIOtype_EVENT:
|
||||||
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
|
USE_SERIAL.printf("[IOc] get event: %s\n", payload);
|
||||||
|
|
||||||
// send message to server
|
|
||||||
// webSocket.sendTXT("message here");
|
|
||||||
break;
|
break;
|
||||||
case WStype_BIN:
|
case sIOtype_ACK:
|
||||||
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
|
USE_SERIAL.printf("[IOc] get ack: %u\n", length);
|
||||||
|
hexdump(payload, length);
|
||||||
|
break;
|
||||||
|
case sIOtype_ERROR:
|
||||||
|
USE_SERIAL.printf("[IOc] get error: %u\n", length);
|
||||||
|
hexdump(payload, length);
|
||||||
|
break;
|
||||||
|
case sIOtype_BINARY_EVENT:
|
||||||
|
USE_SERIAL.printf("[IOc] get binary: %u\n", length);
|
||||||
|
hexdump(payload, length);
|
||||||
|
break;
|
||||||
|
case sIOtype_BINARY_ACK:
|
||||||
|
USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
|
||||||
hexdump(payload, length);
|
hexdump(payload, length);
|
||||||
|
|
||||||
// send data to server
|
|
||||||
// webSocket.sendBIN(payload, length);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
@ -79,6 +68,11 @@ void setup() {
|
|||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// disable AP
|
||||||
|
if(WiFi.getMode() & WIFI_AP) {
|
||||||
|
WiFi.softAPdisconnect(true);
|
||||||
|
}
|
||||||
|
|
||||||
WiFiMulti.addAP("SSID", "passpasspass");
|
WiFiMulti.addAP("SSID", "passpasspass");
|
||||||
|
|
||||||
//WiFi.disconnect();
|
//WiFi.disconnect();
|
||||||
@ -86,28 +80,45 @@ void setup() {
|
|||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
webSocket.beginSocketIO("192.168.0.123", 81);
|
String ip = WiFi.localIP().toString();
|
||||||
//webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization
|
USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
|
||||||
webSocket.onEvent(webSocketEvent);
|
|
||||||
|
|
||||||
|
// server address, port and URL
|
||||||
|
socketIO.begin("10.11.100.100", 8880);
|
||||||
|
|
||||||
|
// event handler
|
||||||
|
socketIO.onEvent(socketIOEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned long messageTimestamp = 0;
|
||||||
void loop() {
|
void loop() {
|
||||||
webSocket.loop();
|
socketIO.loop();
|
||||||
|
|
||||||
if(isConnected) {
|
uint64_t now = millis();
|
||||||
|
|
||||||
uint64_t now = millis();
|
if(now - messageTimestamp > 2000) {
|
||||||
|
messageTimestamp = now;
|
||||||
|
|
||||||
if(now - messageTimestamp > MESSAGE_INTERVAL) {
|
// creat JSON message for Socket.IO (event)
|
||||||
messageTimestamp = now;
|
DynamicJsonDocument doc(1024);
|
||||||
// example socket.io message with type "messageType" and JSON payload
|
JsonArray array = doc.to<JsonArray>();
|
||||||
webSocket.sendTXT("42[\"messageType\",{\"greeting\":\"hello\"}]");
|
|
||||||
}
|
// add evnet name
|
||||||
if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
|
// Hint: socket.on('event_name', ....
|
||||||
heartbeatTimestamp = now;
|
array.add("event_name");
|
||||||
// socket.io heartbeat message
|
|
||||||
webSocket.sendTXT("2");
|
// add payload (parameters) for the event
|
||||||
}
|
JsonObject param1 = array.createNestedObject();
|
||||||
|
param1["now"] = now;
|
||||||
|
|
||||||
|
// JSON to String (serializion)
|
||||||
|
String output;
|
||||||
|
serializeJson(doc, output);
|
||||||
|
|
||||||
|
// Send event
|
||||||
|
socketIO.sendEVENT(output);
|
||||||
|
|
||||||
|
// Print JSON for debugging
|
||||||
|
USE_SERIAL.println(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user