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!