Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WebSocketsServer webSocket = WebSocketsServer(81); //Web socket server running on port 81.
class SerialOverWiFi {
public:
void subscribe(uint8_t num) {
webSocket.subscribe(num);
}
void unsubscribe(uint8_t num) {
webSocket.unsubscribe(num);
}
void send(String data) {
webSocket.broadcastSerialOverWiFi(data); //broadcast the data to every connected client.
}
};
SerialOverWiFi serialOverWiFi;
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { // When a WebSocket message is received
switch (type) {
case WStype_DISCONNECTED:
serialOverWiFi.send("1 Device Disconnected");
break;
case WStype_CONNECTED:
serialOverWiFi.send("1 Device Connected");
break;
case WStype_TEXT:
if (payload[0] == '#') {
serialOverWiFi.subscribe(num);
} else if (payload[0] == '$') {
serialOverWiFi.unsubscribe(num);
}
break;
}
}
void initializeWebSocket() {
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
serialOverWiFi.send("Hey There !");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment