I'm having the hardest time figuring out how to actually pull the data out of a payload message and do something with it. I followed the example by Shawn Hymel on websocketserver and am able to receive a payload from python and echo it back. How would you go about pulling the payload out and actually doing something with it? BTW this is on an ESP32
My plan is to send one message containing a Hex value (0-7) then a bin location (combination of letters and numbers typically 8-15).
Here is the example code from the tutorial:
#include <WiFi.h>
#include <WebSocketsServer.h>
// Constants
const char* ssid = "MySSID";
const char* password = "MyPassword";
// Globals
WebSocketsServer webSocket = WebSocketsServer(80);
// Called when receiving any WebSocket message
void onWebSocketEvent(uint8_t num,
WStype_t type,
uint8_t * payload,
size_t length) {
// Figure out the type of WebSocket event
switch(type) {
// Client has disconnected
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
// New client has connected
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connection from ", num);
Serial.println(ip.toString());
}
break;
// Echo text message back to client
case WStype_TEXT:
Serial.printf("[%u] Text: %s\n", num, payload);
webSocket.sendTXT(num, payload);
break;
// For everything else: do nothing
case WStype_BIN:
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
default:
break;
}
}
void setup() {
// Start Serial port
Serial.begin(115200);
// Connect to access point
Serial.println("Connecting");
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
// Print our IP address
Serial.println("Connected!");
Serial.print("My IP address: ");
Serial.println(WiFi.localIP());
// Start WebSocket server and assign callback
webSocket.begin();
webSocket.onEvent(onWebSocketEvent);
}
void loop() {
// Look for and handle WebSocket data
webSocket.loop();
}
.