Hi all,
After asking google for some answers, i decided to ask for help as i am not getting any further with floats on an ESP32.
i'll try to explain what i wish to realise, and put some off the code in here what i think matters for the understanding.
i recieve an UDP stream, and put it in a packetBuffer.
Udp.read(packetBuffer, 1367); // Read the protocol UDP packet, and put in it packetBuffer 1367
So ie. for storing the FuelCapacity, i read the packetbuffer 111 who hold the FuelCapacity value (Byte), like this:
FuelCapacity = (packetBuffer[111]); // Fuel Capacity
So far it all works.
But in the same the UDP stream there are 4 byte's, who hold the data for my ie. FuelLevel
These byte's are at position 116, 117, 118 and 119.
And what ever i try i don't get a proper value. (RANGE = 0.0f->1.0f)
- So my question is: how do i read the UDP stream 116 to 119 as an float
What i have tried so far (and doesn't work) is as follow:
FuelLevel = (packetBuffer[119] << 24, packetBuffer[118] << 16, packetBuffer[117] << 8, packetBuffer[116],) << 0)
or
highWord = word(packetBuffer[119] , packetBuffer[118]);
lowWord = word(packetBuffer[117] , packetBuffer[116]);
FuelLevel = highWord << 16 | lowWord;
also another attempt doesn't work:
FuelLevel = (packetBuffer[119]) << 24 | (packetBuffer[118] & 0xff) << 16 | (packetBuffer[117] & 0xff) << 8 | (packetBuffer[116] & 0xff);
this time i put them in FuelLevel 116 and 117, and print those value's:
FuelLevel116 = float((packetBuffer[119]) << 24 | (packetBuffer[118] & 0xff) << 16 | (packetBuffer[117] & 0xff) << 8 | (packetBuffer[116]& 0xff));
FuelLevel117 = float((packetBuffer[119]) << 24 | (packetBuffer[118]) << 16 | (packetBuffer[117]) << 8 | (packetBuffer[116]) << 0 );
After this i have to multiply FuelLevel * FuelCapacity, to get the actual Fuellevel in Litres.
i can share the whole code (and i will when it works), but i hope this is enough information to get some help started.
Thanks in advance,