0 votes
in WeatherStationClassic by (150 points)
retagged by

Hi All :D

I'm using the Weather Station Classic with the addition of a frame for Teamspeak and it works great, I only have a small inconvenience and unfortunately I'm not a good programmer, in fact, I'm not really! From Teamspeak I get the string thingspeak.getFieldValue(n), and the value is for example 15,50000. How can I round off the result obtained by TS? I know it's a stupid question but I have been studying the code for hours but I can not find an elegant solution!

At the moment with thingspeak.getFieldValue(n).substring(0,4) the value on the display is 15,5 but if the temperature drop below 10°C the value on the display will be 9,90

1 Answer

0 votes
by (17.4k points)
selected by
Best answer

As per https://github.com/ThingPulse/esp8266-weather-station/blob/master/src/ThingspeakClient.h#L51 getFieldValue() returns a String. Hence, you first need to convert that String to a numeric value before you can round it.

String s = "47.11";
float f = round(s.toFloat());

I trust you can adjust that example to your needs.

by (150 points)
Thanks to your suggestion I created a function that returns a string ready for the display.

String newstring(String data, int number){

float f = data.toFloat();

 String t1 = String(f, number);

 return t1; }

In this way I can also decide the number of digits after the decimal point with the value "number".

display->drawString(64 + x, 10 + y, "Temp: " + newstring(thingspeak.getFieldValue(0), 1) + "°C");

It works perfectly but if I insert a Serial.println inside the function we see that it is executed many times for the duration of the frame .... is it normal?

Excuse my unclear approach, I do my best but I still have a lot to learn!
by (17.4k points)
While a frame slides from right to left to its final position the content is redrawn several times. This creates the "illusion" of it actually sliding.

However, as the result of newstring(thingspeak.getFieldValue(0), 1) is always the same you could calculate the value only once and then store it in a variable defined outside the drawing function.
by (150 points)
Tonight I try to define the function, declare the variables and try your approach.
Thank you!
by (3.6k points)

String rounded(String sourcedata, int roundedto){
  return String(sourcedata.toFloat(), roundedto);


Welcome to ThingPulse Q&A, where you can ask questions and receive answers from other members of the community.