-1

I am trying to connect three separate DHT11 to my Wemos d1 r1. I have each sensor hooked to a power pin which turns on and off, the sensor pin and a common ground. Here is my code:

#include <DHTesp.h>
#include <ESP8266WiFi.h>

String apiKey = "xxxxxxxxxxxxxx";  
const char* ssid = "xxxxxxxxxxx";  
const char* password = "xxxxxxxxxxxx";  
const char* server = "api.thingspeak.com";

DHTesp dht;
DHTesp dht2;
DHTesp dht3;
WiFiClient client;

void setup() {

  pinMode(D6,OUTPUT);
  pinMode(D8,OUTPUT);
  pinMode(D1,OUTPUT);

    Serial.begin(115200);
    dht.setup(D5, DHTesp::DHT11);
    dht2.setup(D7, DHTesp::DHT11);
    dht3.setup(D2, DHTesp::DHT11);
    WiFi.begin(ssid, password);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
digitalWrite(D6,HIGH);

}


void loop() {
 digitalWrite(D6,HIGH);
  delay(8000);

    float t = dht.getTemperature();
    float f = (t*1.8) + 32;

    if (isnan(t))
  {
    Serial.println("Failed to read from DHT2 sensor!");
    return;
  }
    Serial.print(", \"maintemp\": ");
    Serial.print(f);
    Serial.print("}\n");



digitalWrite(D6,LOW);
digitalWrite(D8,HIGH);
delay(8000);

    float t2 = dht2.getTemperature();
    float f2 = (t2*1.8) + 32;

    if (isnan(t2))
  {
    Serial.println("Failed to read from DHT3 sensor!");
    return;
  }
    Serial.print(", \"lowtemp\": ");
    Serial.print(f2);
    Serial.print("}\n");


digitalWrite(D6,LOW);
digitalWrite(D1,HIGH);
delay(8000);

    float t3 = dht3.getTemperature();
    float f3 = (t3*1.8) + 32;

    if (isnan(t3))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
    Serial.print(", \"uptemp\": ");
    Serial.print(f3);
    Serial.print("}\n");

    digitalWrite(D1,LOW);

    delay(2000);

    if (client.connect(server, 80)) {
    String postStr = apiKey;
    postStr += "&field3=";
    postStr += String(f);
    postStr += "&field4=";
    postStr += String(f2);
    postStr += "&field5=";
    postStr += String(f3);
    postStr += "\r\n\r\n\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n\n\n");
    client.print(postStr);

    Serial.print("Temperature: ");
    Serial.print(f);

    Serial.println("Sending data to Thingspeak");
  }
  client.stop();

  Serial.println("Waiting 20 secs");
  // thingspeak needs at least a 15 sec delay between updates
  // 20 seconds to be safe

  delay(53700);
}  

As you can see I assign a separate DHT to each sensor pin and turn on and off the power pins to each sensor. They all have a common ground.

When I run the code the first sensor reads the temperature but the second one says "Failed to read from DHT3 sensor!".

It is not the sensor because if I swap the first and second sensor the first one always read and the second fails. If I eliminate the second sensor the third one fails. I have tried to power all of the sensor pins in SetUP but still the first one is the only one to work. Is there some kind of trick I have to use here.
I am using DHTESP.h since the regular DHT.h didn't work with the Wemos Pro Mini.

Has anyone had any experience with this to give me some guidance.

Thanks

  • 1
    Can you add a print of getStatusString in your error? For instance if (isnan(t)) { Serial.print("Failed to read from DHT2 sensor: status "); Serial.println(dht2.getStatusString()); return; }. Do this for all three sensors, then post the output; it will tell you at least what is the error it detects (timeout? checksum? ...) – frarugi87 Jun 12 at 14:11
  • Can you draw a schematic diagram? What's the total measured pullup from the data line to the +5V line? – Maximilian Gerhardt Jun 12 at 14:14
0

You probably have to create an set up a new DHT object each time you turn the power on. My guess is that the sensor needs to be initialized by the Arduino before it is ready to take readings, and when you turn the power off and back on it loses its configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.