BH1750 DS18B20 and DHT11

Hello again averyone.

I am stucked in my “home sensor” project and I have tried every single possibilites I can think of. I will tear my hair of soon because I cannot understand what Im doing wrong.

Hardware:
Wemos D1 Mini

DHT 11 Sensor (This is the easy one that everybody can do but now Im stucked with it)
BH1750 Lightsensor
DS18B20 Temperature Sensor

This is a printscreen from my blynk app.

Virtual Pins:

V5 - BH1750 (Lightsensor)
V4 - DS18B20 (Outside Temperature)
V7 - DHT11 (Humidity)
V6 - DHT11 (Indoor Temperature)

Below is my code for all this and every sensor works but not the DHT11 !?

I have tried the DHT11 sensor with the scetchbuilder and the same wiring as I have now and then it works.
But when I try my own code, it will not work !
Please help me ! Im sure it is simple for an experianced coder out there but for me as a beginner, its not. I also hope this can help someone else.

So here is the code

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define ONE_WIRE_BUS 14        // DS18B20 Data wire is plugged into port 14 on the Wemos D1mini 

#define DHTPIN 2          // What digital pin we're connected to
#define DHTTYPE DHT11     // DHT 11

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <Wire.h>
#include <BH1750.h>
#include <DallasTemperature.h>
#include <DHT.h>

OneWire oneWire(ONE_WIRE_BUS);        // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature. 
BH1750 lightMeter;
DHT dht(DHTPIN, DHTTYPE);

char auth[] = "-----------------------";
char ssid[] = "-----------------------";
char pass[] = "----------------------";

SimpleTimer timer;

float outsideTemperature; //Outside Temperature in Celsius

/********************************************************************************************/

void sendUptime(){
  
  uint16_t lux = lightMeter.readLightLevel();
  Blynk.virtualWrite(V5, lux);

  sensors.requestTemperatures();                     // Send the command to get temperatures
  outsideTemperature = sensors.getTempCByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V4, outsideTemperature);        // Send temperature to Blynk app virtual pin.

  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V7, h);
  Blynk.virtualWrite(V6, t);

}

void setup(){

  Serial.begin(9600);
  
  Blynk.begin(auth, ssid, pass);
  
  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  Wire.begin();
  // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
  
  lightMeter.begin();

  sensors.begin();  // Start up the library for DS18B20

  dht.begin();

  timer.setInterval(1000L, sendUptime);    // Temperature sensor read interval. 2000 (ms) = 2 seconds.
}

void loop() {
  Blynk.run();
  timer.run();
}

Two things jump out at me:

  1. The DHT11 is a very slow sensor. Querying it every second (1000ms) is way to often. Every 5 seconds is a more realistic figure.

  2. GPIO2 (Pin D4 on a NodeMCU) isn’t a good choice of pin for your DHT11.
    Read this thread:

Pete.

@PeteKnight Thank you for your thoughts and tips but unfortunately they did not help me.

  1. I changed the timer to diffrent values 5s, 6s, 7s etc. but it did not make any diffrence.

  2. As for the GPIO pin, first of all I use a Wemos D1 Mini and not a NodeMCU for this but thats okay.
    The GPIO pin works with the blynk example code wich I will show you down here. Same wiring, same GPIO. It works with the example code from blynk scetch builder but not with my program abowe.

So I still dont know what to do. I hope someone can help me.

Here is the example code from Blynk Scetch Builder and that code works just fine:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "---------------";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "-----------------";
char pass[] = "----------------";

#define DHTPIN 2          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V7, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);


  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Thanks anyway :+1::pray:

Sorry, I didn’t see that you said it was a Wemos D1 Mini that you are using, but exactly the same comments apply as with the NodeMCU.

Your screenshots shows a humidity reading of 17% and an indoor temperature of 24°. These numbers are coming from the DHT11, so exactly what isn’t working?

Pete.

@PeteKnight Yes you are right, my screenshots shows values but when I touch the sensor or blow at it or even put it outside it doesent change values at all.
It stays at 17% Humidity and 24°C indoor whatever I do.

Niklas

And is your serial monitor showing any “Failed to read from DHT sensor” messages?

Pete.

@PeteKnight Yes it is. In my program but not in the program from Blynk Schetch Builder.
Looking at these two programs right now. I printed them out on a paper to sit down and try to figure this out. It makes me kind of frustrated because I know it should work, something in the code is not correct for sure but because Im a newbie its a learning process and it takes time.

Niklas

So I changed my code a little bit. I set up a new void and put the dht in that void alone as you can see. It now works but I still get errors in serial monitor sometimes “Failed to read from DHT”. Very often accualy but I read in another post that another guy had the same problem.

At least it works now, but as I said not without problems so if anyone can help, I would be more then happy

Niklas

New Code:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define ONE_WIRE_BUS 14        // DS18B20 Data wire is plugged into port 14 on the Wemos D1mini 

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <Wire.h>
#include <BH1750.h>
#include <DallasTemperature.h>
#include <DHT.h>

OneWire oneWire(ONE_WIRE_BUS);        // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature. 
BH1750 lightMeter;


char auth[] = "";
char ssid[] = "";
char pass[] = "";

#define DHTPIN 13          // What digital pin we're connected to
#define DHTTYPE DHT11     // DHT 11

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

float outsideTemperature; //Outside Temperature in Celsius

/********************************************************************************************/

void sendUpdht(){

  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, t); 
  Blynk.virtualWrite(V7, h);
  
}

void sendUptime(){

  sensors.requestTemperatures();                     // Send the command to get temperatures
  outsideTemperature = sensors.getTempCByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V4, outsideTemperature);        // Send temperature to Blynk app virtual pin.

  uint16_t lux = lightMeter.readLightLevel();
  Blynk.virtualWrite(V5, lux);

}

void setup(){

  Serial.begin(9600);
  
  Blynk.begin(auth, ssid, pass);
  
  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  Wire.begin();
  // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
  
  lightMeter.begin();

  sensors.begin();  // Start up the library for DS18B20

  dht.begin();

  timer.setInterval(1000L, sendUpdht);    // Temperature sensor read interval. 2000 (ms) = 2 seconds.

  timer.setInterval(1000L, sendUptime);    // Temperature sensor read interval. 2000 (ms) = 2 seconds.
  
}

void loop() {
  Blynk.run();
  timer.run();
}

You’re still querying your DHT too frequently.

Pete.