Trouble with DHT21 / AM2301 heat humidity sensor

Hi and thanks for reading,

In arduino, dht 21 and 22 are semi compatible and work with eachother.
I have tried multiple library’s and loadout(both with dht21 and 22 libs but get both the same result.

Heres the tinyest code i was able to get.

// This #include statement was automatically added by the Spark IDE.
#include "idDHT22/idDHT22.h"

// declaration for DHT11 handler
int idDHT22pin = D2; //Digital pin for comunications
void dht22_wrapper(); // must be declared before the lib initialization
int t;
// DHT instantiate
idDHT22 DHT22(idDHT22pin, dht22_wrapper);


void setup()
{
    t = 0.0;
	Spark.variable("temp",&t, INT);
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dht22_wrapper() {
	DHT22.isrCallback();
}
void loop()
{
    t = DHT22.getCelsius()*100;
	//delay(100);
	DHT22.acquire();
	while (DHT22.acquiring()){};
	int result = DHT22.getStatus();

	delay(2000);
}

by going to the browser:
https://api.spark.io/v1/devices/x/temp?access_token=x

i get

{
  "cmd": "VarReturn",
  "name": "temp",
  "result": 1104150528,
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-06-12T12:50:34.094Z",
    "connected": true,
    "deviceID": "x"
  }
}

“result”: 1104150528 does chance when i heat the sensor.
Where is it going wrong?

Thanks in advance,
Erik

@erikbozelie, though the DHT11 and DHT22 are similar, reading their data is different. The code for idDHT22 was written specifically for the DHT22 since that was the sensor most people inquired about. I may be able to modify the code to work with a DHT11 and create a new library. I’ll see what I can do. :smile:

for a start you’re defining t as an int then assigning 0.0 (float) to it, and i don’t think Spark.variable() copes with floats yet.

have you tried making it a string just for debugging?

#include "application.h"
#include "idDHT22/idDHT22.h"

int idDHT22pin = D2;
void dht22_wrapper();
int t = 0;
char dhtTemp[8];

idDHT22 DHT22(idDHT22pin, dht22_wrapper);

void dht22_wrapper() {
    DHT22.isrCallback();
}

void setup()
{
    Spark.variable("temp",&dhtTemp, STRING);
}

void loop()
{
    DHT22.acquire();
    while (DHT22.acquiring()){};
    t = DHT22.getCelsius()*100;
    sprintf(dhtTemp,"%d",t);
    delay(2000);
}

@sej7278, FYI - Spark.variable() can do DOUBLE which also handles floats. :smile:

ah thanks for that. btw if you're thinking of doing a dht11 library, the dht11 only returns integers (and is a pile of poo).

@sej7278, yup that’s why I always recommend the DHT22 :smile:

@erikbozelie, any chance you can switch to a DHT22? It will give you less problems and it has a ready-to-go library!

hi,
sorry if it’s a stupid question. Can i use the DHT11 without a library? or is there a library for this in the Core IDE?
Thanks

@protagonist, you DO need a library and @wgbartley had posted a library for it on this topic:

:smile:

what do I have to do with this Gist to use it for the Core?

By the looks of it, I guess you only have to copy/paste it to the web IDE, after which you should be able to flash it. If you then wire the sensor as explained in the post, you should be good to go.

@protagonist, the code was written when we did not have “tabs” on the IDE so it is “all inclusive” meaning everything is in the one file. All you have to do is copy/paste the entire gist to a new APP on the IDE. Then, change #define DHTTYPE DHT22 to #define DHTTYPE DHT11. The last missing thing is serial output so you can see the readings!. Change the setup() and loop() code to the following:

void setup() {
  dht.begin();
  Serial.begin(9600);
}
 
 
void loop() {

	delay(2000);
    f = 0;
    h = dht.readHumidity();
    t = dht.readTemperature();
    
    if (t==NAN)
		Serial.println("Temperature reading is bad");
	else {
		Serial.print("Temperature is ");
		Serial.println(t);
		Serial.println(" F");
	}
		
	if (h==NAN)
		Serial.println("Humidity reading is bad");
	else {
		Serial.print("Humidity is ");
		Serial.print(h);
		Serial.println(" %");
	}		
 
}

Then, verify the code and flash it over to your core. Assuming your core is connected via USB, open a terminal program and connect to the core’s COM port and you should see the readings. :smile:

Thanks again. so something is happening but its probably not the Temperatur: 1103101952. The Humidity is 1108606976. Does someone have an idea whats wrong?

EDIT: Sorry. the Problem was that I tried to send it to a web app and called a integer not a float. now I get 25°C and 35% RH . At least the Temperatur is Correct

Thanks yall for the reply's.
Reason why i was using the dht21 is because i already had 10 of them.
Wired these things are expensive.

So we had the same issue, how did you send a float to the web app? double?
thought variable didn't support float.

@erikbozelie, 35% RH sounds about right. What were you expecting?

When you say “send a float to a webpage” do you mean using Spark.variable(), Spark.publish() or something else?

Bought new DHT22’s, but now… there is a new problem.
I am trying to migrate 2 seperate codes together and think i found the problem.
Uncommenting “UDP Udp;” provides my spark with a red led boot loop.
Any idea why?

Thanks in advance,
Erik


// This #include statement was automatically added by the Spark IDE.
#include "idDHT22/idDHT22.h"

// declaration for DHT11 handler
int idDHT22pin = D4; //Digital pin for comunications
void dht22_wrapper(); // must be declared before the lib initialization

// DHT instantiate
idDHT22 DHT22(idDHT22pin, dht22_wrapper);

double temp = 0.0;
double hum = 0.0;

TCPServer server = TCPServer(23);
TCPClient client;
//UDP Udp;

void setup()
{
    Spark.variable("temp",&temp, DOUBLE);
    Spark.variable("hum",&hum, DOUBLE);
}

void dht22_wrapper() {
	DHT22.isrCallback();
}

void loop()
{
	DHT22.acquire();
	while (DHT22.acquiring())
		;
	DHT22.getStatus();
    temp =DHT22.getCelsius();
    hum = DHT22.getHumidity();
	delay(2000);
}

@erikbozelie, running both TCPServer and TCPClient will cause an out-of-RAM error (red flashing). Each takes a lot of RAM for buffering. There are lots of ways to NOT use these. Can I ask what you’re trying to do so perhaps I can suggest a different approach? :smile:

In short, setting up communication with java on a local network.
spark broadcasts a udp packet for which java listens to.
Java then takes the ip from that packet and setups a tcp connection.

This code works.

However getting the 2 codes to work together creates a red led.

Heres my communication

@erikbozelie, the Spark Team has been working on reducing RAM usage with great success during this sprint. The code is available for local builds but it will be released soon to the web IDE. :smile:

1 Like

The RAM updates are available :smiley:!