2021-10-31 13:07:09 +00:00
|
|
|
// Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
|
2021-11-02 17:51:55 +00:00
|
|
|
#include "sensor_dht22.h"
|
2021-10-31 13:07:09 +00:00
|
|
|
|
|
|
|
Sensor_DHT22::Sensor_DHT22(int pin)
|
|
|
|
{
|
|
|
|
dht = new DHT(pin,DHT22,11); //default:11
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sensor_DHT22::init() //Things to be done during setup()
|
|
|
|
{
|
|
|
|
Serial.println("initializing dht");
|
|
|
|
dht->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Also called during setup()
|
|
|
|
void Sensor_DHT22::setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
|
|
|
{
|
|
|
|
dataDHT22_temperature.minchange=minchange;
|
|
|
|
dataDHT22_temperature.senddelaymax=senddelaymax;
|
|
|
|
dataDHT22_temperature.readdelay=readdelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Also called during setup()
|
|
|
|
void Sensor_DHT22::setSettings_Humidity(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
|
|
|
{
|
|
|
|
dataDHT22_humidity.minchange=minchange;
|
|
|
|
dataDHT22_humidity.senddelaymax=senddelaymax;
|
|
|
|
dataDHT22_humidity.readdelay=readdelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Called during setup
|
|
|
|
void Sensor_DHT22::advertise(HomieNode& p_sensorNode)
|
|
|
|
{
|
|
|
|
sensorNode = &p_sensorNode;
|
|
|
|
#ifndef SENSOR_BMP180
|
|
|
|
sensorNode->advertise("temperature");
|
|
|
|
#else
|
|
|
|
sensorNode->advertise("temperature_dht");
|
|
|
|
#endif
|
|
|
|
sensorNode->advertise("humidity");
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:51:55 +00:00
|
|
|
void Sensor_DHT22::sensorloop()
|
2021-10-31 13:07:09 +00:00
|
|
|
{
|
2021-11-02 17:51:55 +00:00
|
|
|
loop_DHT22_temperature();
|
|
|
|
loop_DHT22_humidity();
|
2021-10-31 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Sensor_DHT22::loop_DHT22_temperature()
|
|
|
|
{
|
|
|
|
sensordata &d=dataDHT22_temperature;
|
|
|
|
bool _changed=false;
|
|
|
|
|
|
|
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
|
|
d.value = dht->readTemperature();
|
|
|
|
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
|
|
|
_changed=true;
|
|
|
|
}
|
|
|
|
d.lastreadtime=millis();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
|
|
|
Serial.print("Sending DHT22_temperature. reason=");
|
|
|
|
if (_changed) Serial.println("change"); else Serial.println("time");
|
|
|
|
|
|
|
|
if (!(isnan(d.value) == 1)){ //success
|
|
|
|
|
|
|
|
#ifndef SENSOR_BMP180
|
|
|
|
sensorNode->setProperty("temperature").send(String(d.value));
|
2021-11-02 17:51:55 +00:00
|
|
|
Homie.getLogger() << "temperature " << ": " << d.value << endl;
|
2021-10-31 13:07:09 +00:00
|
|
|
#else
|
|
|
|
sensorNode->setProperty("temperature_dht").send(String(d.value));
|
|
|
|
Homie.getLogger() << "temperature_dht " << ": " << d.value << endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
d.lastsentvalue=d.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
d.lastsent=millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sensor_DHT22::loop_DHT22_humidity()
|
|
|
|
{
|
|
|
|
sensordata &d=dataDHT22_humidity;
|
|
|
|
bool _changed=false;
|
|
|
|
|
|
|
|
|
|
|
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
|
|
d.value = dht->readHumidity();
|
|
|
|
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
|
|
|
_changed=true;
|
|
|
|
}
|
|
|
|
d.lastreadtime=millis();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
|
|
|
Serial.print("Sending DHT22_humidity. reason=");
|
|
|
|
if (_changed) Serial.println("change"); else Serial.println("time");
|
|
|
|
|
|
|
|
if (!(isnan(d.value) == 1)){ //success
|
|
|
|
Homie.getLogger() << "humidity " << ": " << d.value << endl;
|
|
|
|
sensorNode->setProperty("humidity").send(String(d.value));
|
|
|
|
d.lastsentvalue=d.value;
|
|
|
|
}
|
|
|
|
d.lastsent=millis();
|
|
|
|
}
|
2021-11-02 17:51:55 +00:00
|
|
|
}
|
|
|
|
|