dh11 decimal formatting (solved)

Hell guys,

i'm running the script from bruh automation, FULL CODE RAW

here is the thing:

i want to change the output of the dh11 temperature and humidity, because they show up like 45.00 and 23.00

so i want to get rid of the tho zeroes.

what i did try was this in the void sendState() section for my mqtt json string :

float t = dht.readTemperature();
root["temperature"] = t ,0 ;

in the monitor it shows up loke this :
"temperature":31,"
there is a comma i can't get rid of it...

when i use this in the void LOOP section it shows up correctly but i can't use it :
float t = dht.readTemperature();
if (isnan(t) || isnan(h)) {
Serial.println("Error dht sensor");
} else {
t = t - 1.00; //correction
Serial.print("humid: ");
Serial.print(h,0);
Serial.print(" %\t ");
Serial.print("Temperature: ");
Serial.print (t,0);
Serial.println("*C");
}

it prints like this :
temperature : 23

regards

so i want to get rid of the tho zeroes.

The simplest solution, then, is to store the temperature and humidity as ints, not floats.

what i did try was

misusing the comma operator.

The comma separates arguments in a function call. When no function is present, the comma does something COMPLETELY different.

PaulS:
The simplest solution, then, is to store the temperature and humidity as ints, not floats.
misusing the comma operator.

The comma separates arguments in a function call. When no function is present, the comma does something COMPLETELY different.

Hi Paul,

Can you give me an example how to store as ints?
im not very good at coding, still learning a lot.

thatks for the fast reply

Welcome,

Use round(), and cast to int if needed. Example

float f = 34.56;
int i = (int)round(f);

i = 35

guix:
Welcome,

Use round(), and cast to int if needed. Example

float f = 34.56;

int i = (int)round(f);



i = 35

shows up like this

"temperature":31,"

still that comma there...

i did this :
float t = dht.readTemperature();
int i = (int)round(t);
root["temperature"] = i;

i found it , i did change it to (String)i; and it works now.

thanks all for the quick reply!!!

i found it , i did change it to (String)i; and it works now.

That was the worst possible way to address the problem.

You probably should visit the http://snippets-r-us.com forum instead of the Arduino forum, since you are so fond of snippets.