Convert a Websocket payload into two integers

Hey, I got an eps8266 and I am trying to create a joystick as input for further projects. I managed to get the HTML page and the javascript running. The transfer between server and client is done with a Websocket. My problem right now is that I don't get the payload which is an array of integers in the javascript to two integers on my board.
I managed to print the values with the following code:

for(int i=0;i < lenght; i++){  Serial.print((char)payload[i]);}

this works perfectly fine, the problem comes when I try to assign the values to integers. My attempt looks like this:

  posX =  atoi((const char*)&payload[0]);
  posY =  atoi((const char*)&payload[lenght/2]);

The X value works fine, but posY only works for some values like -100. I tried to play around with the index but I didn't help. I hope you can help me, I think I have spent already way to much time on this. :frowning:

index.h (8.65 KB)

JoystickControlls.ino (1.62 KB)

If payload varies in length (<10, <100, <100) then how in the world do you know where to start parsing the second parameter.
A more typical approach would be to print X, then a separator like ',' and then Y so the receiving code can key off of the separator and succeed.

I get the idea,

   for(int i=0;i < lenght; i++){
   Serial.print((char)payload[i]);
   }

if I use this, it prints out the values separated by an ','.
But I don't know how to convert my payload, which is uint8_t* datatype to two integers.
All I could find was something like this

atoi((const char*)&payload[0])

but I guess it's not helpful here

atoi() works on strings. If your payload is uint8_t, that means an 8 bit value (0-255) so you still have the problem of not a consistent amount of chars per integer.

I got it working:

  int i= 0;
  while ((char)payload[i] != 44){
    i++;
  }
  char str1[i-1];
  char str2[lenght-i];

  for(int j = 0; j<i;j++){
    str1[j]=(char)payload[j];
  }
  for(int j = i+1; j < lenght;j++){
    str2[j-i -1]=(char)payload[j];

  posX =  atoi(str1);
  posY =  atoi(str2);
  }