Xenon and DHT11/22 - wan't read sensors data

Dear Community,
need your help - to my Xenon i’ve connected DHT11 (also tried DHT22) with different libraries - Adafruit_DHT and PietteTech_DHT - but still can’t get reading data - usually its 0 or -5 instead of real data.
Can you please help

Kind regards,
Anton

-5 indicates a problem acquiring data - there might be multiple reasons like wiring, timing or initialisation that can cause that.

Can you share the code and hardware setup you are using?

BTW, have you searched the forum before opening a new thread?
I know there are multiple other threads dealing with this very general problem description and many of them would ask the same questions as I have asked above and some more and reading through these threads would prevent the need to reiterate the same things over and over.

sure - here we go:

// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>
#define DHTTYPE  DHT11              // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   D1           	    // Digital pin for communications
#define DHT_SAMPLE_INTERVAL   2000  // Sample every two seconds

int LED = 0;

void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
// globals
unsigned int DHTnextSampleTime;	    // Next time we want to start sample
bool bDHTstarted;		    // flag to indicate we started acquisition
int n;

void setup() {
  Serial.begin();
  pinMode(A0,INPUT);      // Чтение для Фоторезистора
  pinMode(LED,OUTPUT);    // LED на PIN D0

  DHTnextSampleTime = 0;  // Start the first sample immediately
}

void dht_wrapper() {
  DHT.isrCallback();
}

void loop() {

// read Sensors
  int light = analogRead(A5); 

  float h = DHT.getHumidity();
  float t = DHT.getCelsius();

// convert for Publishing
  String light_x = String(light);
  String temp_x = String(t);
  String humi_x = String(h);

//Publish
  Particle.publish("temp_x", temp_x, PRIVATE);
  Particle.publish("humi_x", humi_x, PRIVATE);
  Particle.publish("light_x", light_x, PRIVATE);

 digitalWrite(LED, LOW);  
  delay(30000);               // Wait for 30 seconds

 digitalWrite(LED, HIGH);
  delay(1000);
}

If you look at the example that comes with that library you would notice that your code is missing the DHT.begin() as well as DHT.acquire() or DHT.acquireAndWait(1000) calls :wink:

BTW, it’s usually better to wrap all your readings into a single Particle.publish()
e.g. like this

  char data[64];
  snprintf(data, sizeof(data), "Temp %.1f °C, Humidity %.1f %%, Light %d", t, h, light);
  Particle.publish("x", data, PRIVATE);
1 Like

hmmm - unusuall stuff - i’ve copied code from link you’ve provided (bookmark: DHT_example.ino)
and added publish events for controll and now not receiving any data at all …

#include "PietteTech_DHT.h"  
#define DHTTYPE  DHT11              // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   D1                 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL   2000  // Sample every two seconds

SerialLogHandler logger(LOG_LEVEL_ERROR, {{ "app", LOG_LEVEL_TRACE }});

void dht_wrapper();                 // must be declared before the lib initialization
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
void dht_wrapper() {
  DHT.isrCallback();
}

bool bDHTstarted;		                // flag to indicate we started acquisition
int n;                              // counter

int LED = 0;

void setup()
{
  Serial.begin(115200);

  delay(100);

  Serial.println("DHT Simple program using DHT.acquire");
  Serial.printlnf("LIB version: %s", (const char*)DHTLIB_VERSION);
  Serial.println("---------------");

  DHT.begin();
  pinMode(LED,OUTPUT);    // LED на PIN D0
}

void loop()
{
  static uint32_t msLastSample = 0;
  if (millis() - msLastSample <  DHT_SAMPLE_INTERVAL) return;

  if (!bDHTstarted) {               // start the sample
    Serial.printlnf("\r\n%d: Retrieving information from sensor: ", n);
    DHT.acquire();
    bDHTstarted = true;
  }

  if (!DHT.acquiring()) {           // has sample completed?
    int result = DHT.getStatus();

    Serial.print("Read sensor: ");
    switch (result) {
      case DHTLIB_OK:
        Serial.println("OK");
        break;
      case DHTLIB_ERROR_CHECKSUM:
        Serial.println("Error\n\r\tChecksum error");
        break;
      case DHTLIB_ERROR_ISR_TIMEOUT:
        Serial.println("Error\n\r\tISR time out error");
        break;
      case DHTLIB_ERROR_RESPONSE_TIMEOUT:
        Serial.println("Error\n\r\tResponse time out error");
        break;
      case DHTLIB_ERROR_DATA_TIMEOUT:
        Serial.println("Error\n\r\tData time out error");
        break;
      case DHTLIB_ERROR_ACQUIRING:
        Serial.println("Error\n\r\tAcquiring");
        break;
      case DHTLIB_ERROR_DELTA:
        Serial.println("Error\n\r\tDelta time to small");
        break;
      case DHTLIB_ERROR_NOTSTARTED:
        Serial.println("Error\n\r\tNot started");
        break;
      default:
        Serial.println("Unknown error");
        break;
    }

    Serial.printlnf("Humidity (%): %.2f", DHT.getHumidity());
    Serial.printlnf("Temperature (oC): %.2f", DHT.getCelsius());
// get sensor data
    float t = DHT.getCelsius();
    float h = DHT.getHumidity();
  String temp_x = String(t);
  String humi_x = String(h);

//Publish
  Particle.publish("temp_x", temp_x, PRIVATE);
  Particle.publish("humi_x", humi_x, PRIVATE);
    
    n++;  
    bDHTstarted = false;  // reset the sample flag so we can take another
    msLastSample = millis();
  }
 digitalWrite(LED, LOW);  delay(2500);
 digitalWrite(LED, HIGH); delay(1000); // blink LED then cycle is over
 }

I’ve also done this with code from bookmark DHT_simple.ino with same control publish piece
in this case i’ve at least got value = -5.0

// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>
#define DHTTYPE  DHT11       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   D1          // Digital pin for communications
PietteTech_DHT DHT(DHTPIN, DHTTYPE);
int n;      // counter

int LED = 0;

void setup()
{
  Serial.begin(9600);
  while (!Serial.available() && millis() < 30000) {
    Serial.println("Press any key to start.");
    Particle.process();
    delay(1000);
  }
  Serial.println("DHT Simple program using DHT.acquireAndWait");
  Serial.print("LIB version: ");
  Serial.println(DHTLIB_VERSION);
  Serial.println("---------------");
  DHT.begin();
  pinMode(LED,OUTPUT);    // LED на PIN D0
}

void loop()
{
  Serial.print("\n");
  Serial.print(n);
  Serial.print(": Retrieving information from sensor: ");
  Serial.print("Read sensor: ");

  int result = DHT.acquireAndWait(1000); // wait up to 1 sec (default indefinitely)

  switch (result) {
  case DHTLIB_OK:
    Serial.println("OK");
    break;
  case DHTLIB_ERROR_CHECKSUM:
    Serial.println("Error\n\r\tChecksum error");
    break;
  case DHTLIB_ERROR_ISR_TIMEOUT:
    Serial.println("Error\n\r\tISR time out error");
    break;
  case DHTLIB_ERROR_RESPONSE_TIMEOUT:
    Serial.println("Error\n\r\tResponse time out error");
    break;
  case DHTLIB_ERROR_DATA_TIMEOUT:
    Serial.println("Error\n\r\tData time out error");
    break;
  case DHTLIB_ERROR_ACQUIRING:
    Serial.println("Error\n\r\tAcquiring");
    break;
  case DHTLIB_ERROR_DELTA:
    Serial.println("Error\n\r\tDelta time to small");
    break;
  case DHTLIB_ERROR_NOTSTARTED:
    Serial.println("Error\n\r\tNot started");
    break;
  default:
    Serial.println("Unknown error");
    break;
  }
  Serial.print("Humidity (%): ");
  Serial.println(DHT.getHumidity(), 2);

  Serial.print("Temperature (oC): ");
  Serial.println(DHT.getCelsius(), 2);

    float t = DHT.getCelsius();
    float h = DHT.getHumidity();
  String temp_x = String(t);
  String humi_x = String(h);

//Publish
  Particle.publish("temp_x", temp_x, PRIVATE);
  Particle.publish("humi_x", humi_x, PRIVATE);

  n++;

 digitalWrite(LED, LOW);  delay(2500);
 digitalWrite(LED, HIGH); delay(1000); // blink LED then cycle is over
}

Still missing this tho’

Xenon Device OS: 1.5.2 connected in Mesh network via Argon Device OS: 1.5.0 (argon is doing its job properly and sends data from its sensors)
DHT11 - regular
what additional info is needed ?

How have you connected the DHT11?
Which exact sensor board are you using?
Has it the required pull-up resistor between Vcc and Dout in place?

DHT11 from AOSONG
4 pins:1st pin - Vcc 2nd-Data 3rd-null 4th-GND
1st goes to 3.3 pin on Xenon
2nd - to respective D pin (according to code)
4th connected to GND on Xenon
Have 220OM resistor between Vcc and Dout (but reaction same with or w\o resistor)

sample logs:
|humi_x|-3.000000|agron_xenon|6/14/20 at 12:13:10 am|
|temp_x|-3.000000|agron_xenon|6/14/20 at 12:13:10 am|
|humi_x|-3.000000|agron_xenon|6/14/20 at 12:13:03 am|
|temp_x|-3.000000|agron_xenon|6/14/20 at 12:13:03 am|
|humi_x|34.000000|agron_xenon|6/14/20 at 12:12:56 am|
|temp_x|24.000000|agron_xenon|6/14/20 at 12:12:56 am|
|humi_x|-4.000000|agron_xenon|6/14/20 at 12:12:49 am|
|temp_x|-4.000000|agron_xenon|6/14/20 at 12:12:49 am|
|humi_x|-3.000000|agron_xenon|6/14/20 at 12:12:42 am|

220Ohm is too low.
With a correct resistor (4k7…10k) the code above works for me just fine (tested on two DHT11 and one DHT22).

1 Like