-->
Page 1 of 1

Convert payload data from a websocket

PostPosted: Fri May 12, 2017 3:02 pm
by Tertius21
i am using Links2004/arduinoWebSockets https://github.com/Links2004/arduinoWebSockets

all works fine but i have problems to convert the payload data. I try this example: https://github.com/Links2004/arduinoWebSockets/blob/master/examples/WebSocketServer_LEDcontrol/WebSocketServer_LEDcontrol.ino
Code: Select allvoid webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {

    switch(type) {
        case WStype_DISCONNECTED:
            USE_SERIAL.printf("[%u] Disconnected!\n", num);
            break;
        case WStype_CONNECTED: {
            IPAddress ip = webSocket.remoteIP(num);
            USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);

            // send message to client
            webSocket.sendTXT(num, "Connected");
        }
            break;
        case WStype_TEXT:
            USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);

            if(payload[0] == '#') {
                // we get RGB data

                // decode rgb data
                uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);

                analogWrite(LED_RED,    ((rgb >> 16) & 0xFF));
                analogWrite(LED_GREEN,  ((rgb >> 8) & 0xFF));
                analogWrite(LED_BLUE,   ((rgb >> 0) & 0xFF));
            }

            break;
    }

}


i will try to get something like this:
Code: Select all     if (payload[0] == '-') {            // we get brightness back
int brightness = payload +1
}


but this does not work. how can i get the payload value without the first char - into an integer?

thanks a lot....

Re: Convert payload data from a websocket

PostPosted: Sat May 13, 2017 8:42 am
by martinayotte
int brightness = atoi(&payload[1]);

Re: Convert payload data from a websocket

PostPosted: Sat May 13, 2017 10:58 am
by Tertius21
thanks for your answer.

but error:
invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive]

Re: Convert payload data from a websocket

PostPosted: Sun May 14, 2017 8:44 am
by martinayotte
int brightness = atoi((const char*)&payload[1]);