-->
Page 1 of 3

Interperting data with WebSocketServer

PostPosted: Fri Feb 12, 2016 6:37 pm
by primateio
I am using the ArduinoWebSocket library here. https://github.com/Links2004/arduinoWebSockets. I have my esp8266 acting as a server and using the demo code. I have it serving a webpage and I want to be able to send chunks of data with variables through the web-socket and interprete them. I basically need 2 int sent through, preferably in one datastream. The problem is, the library seems to get the payload as an uint8_t * array (unsigned char array). And the callback that it uses is like this.

Code: Select allvoid webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
  delay(1);
  switch (type) {
      case WStype_TEXT:
            DBG_OUTPUT_PORT.printf("[%u] get Text: %s\n", num, payload);
            break;
}


Does anyone have any idea how I can send a string such as "x654y33" and be able to turn it into,

int x = 654;
int y = 33;

If i can get it into a String I can manipulate it pretty easy in arduino, but so far, everything I try either does not compile, or it does compile but crashes the esp8266 as soon as the websocket sends data.

Thanks in advance.

Re: Interperting data with WebSocketServer

PostPosted: Fri Feb 12, 2016 6:56 pm
by martinayotte
There is many ways to fix such issue.

Since the webSocketEvent() is receiving "uint8_t * payload" which look like a simple string, the easiest one would be to use ArduinoJson and send a JSON structure such as :

Code: Select all{ "x": 654, "y": 33}


If Link2004 WebSocket library is supporting "binary" payload (which I don't know yet, I have to look at it), then a simple C structure could be use :
Code: Select alltypedef struct {
  int x;
  int y;
} coordinate;

But in such case, we need to pay attention if both sender/receiver are compatible, because having "little endian" conflict with "big endian" can become an issue.

Re: Interperting data with WebSocketServer

PostPosted: Fri Feb 12, 2016 7:18 pm
by primateio
Thanks for your reply . I just figured it out and was coming back to post what I got working for future users. Here is what I did to get it working. This is probably not best practice, but worked for me. Instead of sending something like "x334y543" I just send "334 543". Since I am only going to be sending 2 vars, this works. I break out the data into int32_t using the following code.

Code: Select allint32_t x;
int32_t y;
char *tmp;
x = (int32_t) strtol((const char *)payload,&tmp,0);
y = (int32_t)strtol(tmp,&tmp,0);


It is working great so far. I might look into it a little more to be able to send a better payload, but for what I need for this project, it works. I just have to make sure I always send the payload in the right order.

Re: Interperting data with WebSocketServer

PostPosted: Fri Feb 12, 2016 7:28 pm
by martinayotte
Yes ! that is a good workaround !
As I said, there are many ways to accomplish things. ;)
Doing similar things on other platform, I've used sscanf(), but the current ESP libc_replacements doesn't provide yet.