Transmitting floating values out to ethernet

I'm having problems with some code I have which transmits values out to a internet site (devicehub.net) using EtherCard.h

It successfully posts integers but when I try and post a variable that is a float I get weird values.

     float WaterT = 21.2;
    char queryString[256] = {0};
    sprintf(queryString, "Key=1&WaterTemp=%d", WaterT);
    ether.browseUrl(PSTR("/io/72/"), queryString, website, my_callback);
    Serial.println(queryString);

The Serial.println(queryString) prints out a value of "Key=1&WaterTemp=-26214

I've seen from a few posts that the Serial.print doesn't handle float but am not sure how to proceed? Will the floating problem with Serial.println also effect the posting of the string via ether.browser?

Use dtostrf().

%d is the format specifier for ints. Using that to convert a float to a string WILL result in abnormal output.

The %f format specifier is not supported on the Arduino, so you need to figure out a different way. One way is to convert the float to two ints - the whole value and the fractional part multiplied by some constant based on the accuracy you want to see. Another is to use dtostrf() to convert the value to a string.

Thanks - that works perfectly!

GregM:

     float WaterT = 21.2;

I’ve seen from a few posts that the Serial.print doesn’t handle float

This is not true, or should I correct that by saying not necessarily true, and I don’t understand where the problem lies. You should be able to simply write Serial.print (WaterT); and see 21.2 on the screen.

Maybe this is a problem caused by the Ethercard, or an older IDE. I recall having a temperature reader that did have to Serial.print (Whole); then (".") then (Frac), but I don’t suppose it was working with floats.

@Nick: It is an ethernet thing. You can use "Serial.println(WaterT); " and it will work. But sprintf still fails using floats last time I checked. It will print a '?' instead of the value.

sprintf(queryString, "Key=1&WaterTemp=%f", WaterT);

That puts "Key=1&WaterTemp=?" into queryString.

The challenge with the ethernet is every call to a write(), print() or println() function creates another packet. The fewer the packets, the faster the transaction. The best performance is obtained by sending the entire request as one packet.

This really bad example sends the request in 10 packets.

client.print("GET ");
client.print(thisPage);
client.print(" HTTP/1.1");
client.println();
client.print("Host: ");
client.print(thisHost);
client.println();
client.print("Connection: close");
client.println();
client.println();

This sends the same request as one packet.

sprintf(queryString,"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n",thisPage,thisHost);
client.println(queryString);

SurferTim:
@Nick: It is an ethernet thing. The best performance is obtained by sending the entire request as one packet.

OK. I wondered about that. The only internet experience I have is a put to cosm, where a single packet is assembled from several floats.