Websocketserver Parsing and Using Payload

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();
}

.

Well, since you are printing payload, I will assume it is a nul terminated C string.
If the first character is a digit from '0' to '7', you can just grab the first char

byte number = payload[0] - '0'; // turn ASCII digit into number

If the rest of it is some string, you can just copy it.

char bin[16];
strcpy(bin, payload+1); // skip first char

If you want to make it more robust, you can check the length of payload with strlen() and make sure it will fit into bin.

I get a compile error when I add those examples:
"invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive]"

07gtimyfast:
I get a compile error when I add those examples:
"invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive]"

so do the conversion...

char bin[16];
strcpy(bin, (char *)(payload+1)); // skip first char

Here is what I got to compile, I have not tested it's compiled and maybe more clearly demonstrates the data substrings.

 IPAddress ip = webSocket.remoteIP(num);
        Serial.printf("[%u] Connection from ", num);
        Serial.println(ip.toString());
        String str;
        for(int i = 0; i < length; i++) Serial.print((char) payload[i]);
         Serial.println(str);
        String color = str.substring(0, 7);
        String bin = str.substring(8);
        inputMessage = bin;
         myColor = strtol(bin.c_str(), NULL, 16);

"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)."

Something to think about is to package the data on the sending end in a format that is easy to parse on the receiving end.

zoomkat:
"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)."

Something to think about is to package the data on the sending end in a format that is easy to parse on the receiving end.

I'm using the FastLED library and sending the hex value because it is a standard length, the bin location can vary so I added it to the end.

an example message would be "0xFF44DD59C102"
where I then store that into a String, then split it into separate Strings:

"0xFF44DD" as the color
"59C102" as the bin location