Write float value to WifiClient (esp8266)

Hello,

I'm quite new to Arduino programing and C programing in general.

I need to send some float values to a client using WifiClient class.

My code :

float startVelocity = 10.80;

serverClients[0].write(startVelocity );

I get the error :

WiFiClient.h: In instantiation of size_t WiFiClient::write(T&) [with T = float; size_t = unsigned int]

Can you please help me with this error?

Thanks in advance.

What does the class documentation (or header file) states for the write() method? Did you check? Can you pass a float as parameter?

Do you know the difference between write() and print()? It's time you learned.

PaulS,

You are right, I have to learn a lot more about sending datas using write() vs print().

I finally managed to resolve my problem using the PString library.

float startVelocity = 10.80;

char startVelocityStr[20] = { 0 };
PString(startVelocityStr, sizeof(startVelocityStr), startVelocity);


serverClients[0].write((const uint8_t *)startVelocityStr, sizeof(startVelocityStr));

Why do you want to make a String out of the float, and then send that string as binary data? Why the f**k don't you just use print() and let it handle converting the float to a string?

well now that you got what's the syntax to send data with write - why don't you just send the actual 4 bytes representing your floats rather than an approximation in form of a char string?

float startVelocity = 10.80;
serverClients[0].write((const uint8_t *) & startVelocity, sizeof(startVelocity));

why don't you just send the actual 4 bytes representing your floats rather than an approximation in form of a char string?

Perhaps the client just needs a string representation of the float. Recreating the float from 4 bytes requires knowing the order of the bytes AND that the bytes represent a float. No such problem with a string representation of a float.

Use of write versus print was suggesting a desire for direct memory transfer - but Perhaps indeed - just wondering aloud if this has even been considered