DHT22, DHT11 and similar (Blocking version)

Thankyou @mtnscott, that worked well in the Web IDE, I had to delete:

#include "application.h"

and add:

#include <math.h>

to get it to compile. It ran reliably and gave the same temperature as my digital thermometer.

Now I am going to compare with my port and work out what I got wrong!

By the way, did you notice _us which is used to store microseconds is an int, where as according to the definition of micros() is should be unsigned long?

Chris

1 Like

Awesome, That was what I did first, then I found:

float readTemperature(bool S=false);

and changed it to:

float readTemperature(bool S=true);
1 Like

Yeah that’s another way to do it, you are just changing the default initialization of the constructor. :smile: Not really proper, but if it’s just for you then it will work just fine!

2 Likes

@BDub i used your code my result is for Tempereture;

{
“cmd”: “VarReturn”,
“name”: “temperature”,
“result”: “537.60”,
“coreInfo”: {
“last_app”: “”,
“last_heard”: “2014-01-31T21:21:45.921Z”,
“connected”: true,
“deviceID”: “xxxxxxxxxxx”
}
}

For humidity is ;

{
“cmd”: “VarReturn”,
“name”: “humidity”,
“result”: “896.00”,
“coreInfo”: {
“last_app”: “”,
“last_heard”: “2014-01-31T21:22:13.489Z”,
“connected”: true,
“deviceID”: “xxxxxx”
}
}

I think this result is Fahrenheit. How can i change to with Celsius ?

@Yasin if you used my code just above it should default to Celcius. Seems really high though… are you making a Wifi enabled Reflow Oven controller?

@wgbartley any idea what might be wrong?

@BDub yes it is very very high … i don’t know why because i didn’t do anything just upload your code…

Do you have different idea?

@Yasin all i did was copy @wgbartley 's code and add some Spark.variable() hooks so you could make REST GET requests for the info as strings. I’m not sure what could be wrong so hopefully someone else with a DHT22 can chime in and offer a look at the code. CC: @mtnscott, @PaulRB :smile:

@PaulRB @wgbartley Do you have any idea ? i am using DHT11 and i used @BDub code but my results is so high.

for Tempereture;

{
“cmd”: “VarReturn”,
“name”: “temperature”,
“result”: “537.60”,
“coreInfo”: {
“last_app”: “”,
“last_heard”: “2014-01-31T21:21:45.921Z”,
“connected”: true,
“deviceID”: “xxxxxxxxxxx”
}
}

For humidity is ;

{
“cmd”: “VarReturn”,
“name”: “humidity”,
“result”: “896.00”,
“coreInfo”: {
“last_app”: “”,
“last_heard”: “2014-01-31T21:22:13.489Z”,
“connected”: true,
“deviceID”: “xxxxxx”
}
}

Hi @Yasin, I will try with my dht11 this weekend if I get time and let you know.

Paul

1 Like

I will be happy. Thanks.

@Yasin, you will indeed be happy to know that you are not going crazy!

{
  "cmd": "VarReturn",
  "name": "temperature",
  "result": "563.20",
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-02-01T16:39:55.520Z",
    "connected": true,
    "deviceID": "xxxxxxxxxxxxxxxxxxx"
  }
}

and:

{
  "cmd": "VarReturn",
  "name": "humidity",
  "result": "921.60",
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-02-01T16:41:45.655Z",
    "connected": true,
    "deviceID": "xxxxxxxxxxxxxxxxxxxxxx"
  }
}

So… lets see if I can spot the problem…

@PaulRB ohh it is good news ))))) thanks … I checked all @BDub code but did’nt see anything…

OK, @Yasin, time to appologise to @BDub!

Just fixed mine and I guess you made the same mistake.

Change:

#define DHTTYPE DHT22

to

#define DHTTYPE DHT11
2 Likes

Totally missed that little part where @Yasin said he was using a DHT11 ! nice catch @PaulRB No need for apologies :wink:

1 Like

@PaulRB @BDub code’s work for me now. This is a team effort.) nice job… This means “How i love spark!!!”

Thanks All.

2 Likes

Hello @yasin, can you please share your code ?
Thanks,
Rudy

@rudyvan it is same code with @BDub codes just we had mistake with below code.

Change:

#define DHTTYPE DHT22
to

#define DHTTYPE DHT11

@PaulRB So… It all works. But I get quite wide variations with temperature… ranging 3-4 degrees Celsius with every reading. Humidity is fairly constant. I noticed you dropped a 4.7kohm resistor on the data line. Is this necessary? A lot of schematics I’ve seen for this don’t include it. Does it need to be a 4.7kohm?

@Dom Try putting a 0.1uF cap directly across the power and ground pins of the DHT.

< I2C Talk Deleted! >

Edit: always question me to keep me honest!

BDub & @Dom , I’m a bit confused. The DHTxx sensors don’t actually used the I2C protocol and work using a user-defined digital pin for communicating. What does this have to do with SCA and SCL? The DHTxx do NOT have a pull-up built in and it is usually recommended that one be added between VCC and GND, at the sensor, somewhat like you suggest.

I have had varying success with running DHT22 sensors at 3.3V versus 5v, with some not working at all. One a teensy 3, I have a DHT22 (aka RHT03) running at 3.3V with no pull-up just fine. One factor that I find affects is noise and the capacitor you recommend is a great idea. Another is keeping the connections between the DHTxx and the input pins short and making sure the ground bus is good. A larger cap (> 10uF electrolytic) between the VCC and GND on the prototype board may also help. :smile: