I am trying to send temperature read from DS18B20 to a web server. I know two things: to read a temperature, and to send something to web server. What I don't know is how to convert float that temperature reading function is giving to me to string that I need to send to the server.

I am reading temperature with OneWire and DallasTemperature libraries (just a standard sketch ending with:

Serial.println(sensors.getTempCByIndex(0));//sensors.getTempCBy... returns float, from my understanding

I am sending data via ethernet shield, and important part is:

 client.println("GET /led-ekran-vremenska-prognoza/?temp1=" + String(sensors.getTempCByIndex(0)) + "&temp2=" + String(dht.readTemperature()) + "&humidity="+String(dht.readHumidity())+"&secret=111 HTTP/1.1");

I know that the problem is that String(sensors.getTempCByIndex(0)) is not converting to right value. Arduino just stops working there, if that makes sense. At least serial communication stops working, since I don't see any more output...

Basically, TL:DR, I think that the question is how to convert float to string in Arduino? I have read a million topics on this but still cant figure it out, especially sicne it works for temp2 and humidity in example above.

  • I think that the question is how to convert float to string in Arduino? ... why the misleading title? ... why all the stuff about 1-wire and temperature? .... ask what you mean to ask, do not give a long unrelated story – jsotola Feb 27 '18 at 22:13
  • Uhm, maybe because "I think" part? I am not so good with Arduino programming, and I don't see how including as much RELEVANT information as possible is a bad thing? – Milos Tosic Feb 27 '18 at 23:28

Having re-read the question just before posting this answer, I find it odd that the DHT temp and humidity (also floats) work for you - how odd

Use dtostrf

char tStr[9] = { 0 }; // buffer to store the string - length 9, because at most you need 8 chars "-23.5678" , plus null terminator
dtostrf(sensors.getTempCByIndex(0), 7, 3, tStr)

Note: dtostrf returns char pointer to tStr

The 7, 3 is ... 7 = total length of string, 3 = number of decimal digits - So depending on your resolution, you can change those values

Resolution     Arguments to dtostrf
 9             5, 1                  -nn.n
10             6, 2                  -nn.nn
11             7, 3                  -nn.nnn
12             8, 4                  -nn.nnnn

Of course, you can use any arguments for any resolution, the table just shows what you would need if you wanted to show the exact data coming in

For example, I use resolution 12, but I'm not really interested in displaying more than tenths of a degree - so I use 5,1 for the format

  • This is an awesome explanation of dtostrf. I experimented a little with samples that I found online, but could not figure it out. Anyway, I tried the code that you posted, and I get the error when I try to combine results with the rest of the string: client.println("GET /led-ekran-vremenska-prognoza/?temp1=" + tStr + "&temp2=" + String(dht.readTemperature()) + "&humidity="+String(dht.readHumidity())+"&secret= HTTP/1.1"); Error: invalid operands of types 'const char [42]' and 'char [9]' to binary 'operator+' – Milos Tosic Feb 28 '18 at 0:03
  • the issue is the + operator on char * .. try + String(tStr) + ... – Jaromanda X Feb 28 '18 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.