Get values from dht11 to a variable

Hi,

I have the follow code showing on the screen the humidity and temperature, the code works.
So now I need the humidity and temperature values inside the follow variables to get using CURL.

umidade= DHTB.getHumidity();
temperatura=DHTB.getCelsius();
  
sprintf(message, "{\"umidade\":%f,\"temperatura\":%f}", umidade, temperatura);

So I receive from curl this values:

 "cmd": "VarReturn",
  "name": "message",
  "result": "{\"umidade\":-5.000000,\"temperatura\":-5.000000}",
  "coreInfo": {
    "connected": true
  }
}    

I receive only -5.000000, where is the problem?

One problem keeps appearing over and over again.

Don’t use the ampersand & in this line. The variable message is already a pointer.

hi, I tried removing & but the result is the same

Just to make sure:

What values do you get back when requesting the other two Spark.variables?
Do these show 5.0 aswell?
Do you get correct readings via Serial.print?


Edit:
You’ve got

before printSensorData() - why not move it after the line DHTB.acquire();?

using Serial.print I can see for example the temperature cº 28 and the humidity 45%

Have you read my edit about your while() above?

thanks now working fine. Just one more question, After more or less one minute the serial.print stop an if I try a curl I receive a timeout

What does the RGB LED do? What colors do you see?

Have you still got the ampersand removed on the STRING Spark.variable?

Also try to add SPARK_WLAN_Loop(); in your while(), just in case acquiring takes longer than expected.

@rbbahia, you allocated 50 bytes for message[] which is not enough. You have two choices:

  1. Declare message[80] so it is bigger
  2. Reduce the amount of decimal points in your sprintf since the output of the DHT are not accurate to that many decimal points anyway:
sprintf(message, "{\"umidade\":%3.2f,\"temperatura\":%3.2f}", umidade, temperatura);

:smile:

1 Like

thank you very much, now is fine

1 Like