Using incoming UDP values

Hi,

I'm trying to use a value that I have coming in over a UDP socket, but I can't seem to get the value to be anything other than 0. I'm not sure if this is because of how I'm casting it or what.

#include <WiFi.h>
#include <WiFiUdp.h>

/* WiFi network name and password */
const char * ssid = "ATT8Y6A737";
const char * pwd = "554n3b4z275%";
int freq = 5000;
int ledChannel = 0;
int resolution = 8;

const char * udpAddress = "192.168.1.65";
const int udpPort = 3000;

//create UDP instance
WiFiUDP udp;

void setup(){

  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(2, ledChannel);
  
  Serial.begin(115200);
  
  //Connect to the WiFi network
  WiFi.begin(ssid, pwd);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  //This initializes udp and transfer buffer
  udp.begin(udpPort);
}

void loop(){

  //processing incoming packet, must be called before reading the buffer
  uint8_t buffer[50];
  udp.parsePacket();
  //receive response from server, it will be HELLO WORLD
  if(udp.read(buffer, 50) > 0){
    Serial.println((char *)buffer);
   
    int val = (atoi((char *)buffer) * 255);
    Serial.println(val);
    ledcWrite(ledChannel, val);
  }
  delay(10);
}

But these are the values I get:

0
0.976233;

0
0.985221;

0
0.98966;


0
0.99418;

I'm just trying to use the value from the buffer as an int, but I can't seem to get it right. Thanks in advance for any help!

I don't know how you are sending the value, but it appears to be working. The atoi function will stop reading when it encounters anything but a number. A decimal point is not a number.

  udp.parsePacket();

parsePacket() returns a value. What is the point of calling this function if you don't care what the value is?

  if(udp.read(buffer, 50) > 0){
    Serial.println((char *)buffer);

buffer is NOT a string. You should NOT pass that array to a function that expects a string. You REALLY need to understand the difference between a string and a char array.

Anonymous printing sucks. Stop doing that.

    int val = (atoi((char *)buffer) * 255);

buffer is STILL not a string. atoi() expects a string that contains the representation of an integer. atof() knows how to deal with a string that contains the representation of a float.