DHT11 for thermostat?


Fernando Nino Sr
 

Hi Howard, I removed my bme and switched it over to DHT11 can you help me with the coding on this to get the inside temp and humidity? Did not make sense to waste the bme for just these two readings.
Thanks
Nino


Howard Dutton
 
Edited

Think it should look like this:

// this gets called once on startup to initialize any thermostat sensors

#include "DHT.h"           // DHT temperature/humidity sensors https://github.com/adafruit/DHT-sensor-library
#define DHTPIN 2           // digital pin connected to the DHT sensor
#define DHTTYPE DHT11      // set DHT sensor model: DHT11, DHT22, or DHT21
DHT dht(DHTPIN, DHTTYPE);

bool thermostatInit() {
  dht.begin();
  return true;
}

// gets inside temperature in deg. C
// return (invalid) if not implemented or if there's an error
double thermostatInsideTemp() {
  static double t=invalid;
  static unsigned long next=1000;
  if (t == 0 || (long)(millis()-next) > 0) {
    next=millis()+60000;
    t=dht.readTemperature();
  }
  if (isnan(t) || t < -50 || t > 150) return invalid; else return t;
}

// gets inside RH in %
// return (invalid) if not implemented or if there's an error
double thermostatInsideHumidity() {
  static double h=invalid;
  static unsigned long next=1000;
  if (h == 0 || (long)(millis()-next) > 0) {
    next=millis()+60000;
    h=dht.readHumidity();
  }
  if (isnan(h) || h < 0 || h > 100) return invalid; else return h;
}


Fernando Nino Sr
 

Thanks Howard, it worked perfect.
Nino