2019-12-17 20:31:01 +00:00
|
|
|
/*
|
|
|
|
xsns_58_dht12.ino - DHT12 I2C temperature and humidity sensor support for Tasmota
|
|
|
|
|
2019-12-31 13:23:34 +00:00
|
|
|
Copyright (C) 2020 Stefan Oskamp and Theo Arends
|
2019-12-17 20:31:01 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_DHT12
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* DHT12 - Temperature and Humidity
|
|
|
|
*
|
|
|
|
* I2C Address: 0x5C
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_58 58
|
|
|
|
#define XI2C_41 41 // See I2CDEVICES.md
|
|
|
|
|
2019-12-21 11:43:07 +00:00
|
|
|
#define DHT12_ADDR 0x5C
|
2019-12-17 20:31:01 +00:00
|
|
|
|
2019-12-21 11:43:07 +00:00
|
|
|
struct DHT12 {
|
|
|
|
float temperature = NAN;
|
|
|
|
float humidity = NAN;
|
|
|
|
uint8_t valid = 0;
|
|
|
|
uint8_t count = 0;
|
|
|
|
char name[6] = "DHT12";
|
|
|
|
} Dht12;
|
2019-12-17 20:31:01 +00:00
|
|
|
|
|
|
|
bool Dht12Read(void)
|
|
|
|
{
|
2019-12-21 11:43:07 +00:00
|
|
|
if (Dht12.valid) { Dht12.valid--; }
|
|
|
|
|
|
|
|
Wire.beginTransmission(DHT12_ADDR);
|
|
|
|
Wire.write(0);
|
|
|
|
if (Wire.endTransmission() != 0) { return false; }
|
|
|
|
|
|
|
|
delay(50);
|
|
|
|
|
|
|
|
Wire.requestFrom(DHT12_ADDR, 5);
|
|
|
|
delay(5);
|
|
|
|
uint8_t humidity = Wire.read();
|
|
|
|
uint8_t humidityTenth = Wire.read();
|
|
|
|
uint8_t temp = Wire.read();
|
|
|
|
uint8_t tempTenth = Wire.read();
|
|
|
|
uint8_t checksum = Wire.read();
|
|
|
|
|
|
|
|
Dht12.humidity = ConvertHumidity( (float) humidity + (float) humidityTenth/(float) 10.0 );
|
|
|
|
Dht12.temperature = ConvertTemp( (float) temp + (float) tempTenth/(float) 10.0 );
|
|
|
|
|
|
|
|
if (isnan(Dht12.temperature) || isnan(Dht12.humidity)) { return false; }
|
|
|
|
|
|
|
|
Dht12.valid = SENSOR_MAX_MISS;
|
|
|
|
return true;
|
2019-12-17 20:31:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 11:43:07 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2019-12-17 20:31:01 +00:00
|
|
|
void Dht12Detect(void)
|
|
|
|
{
|
2019-12-21 11:43:07 +00:00
|
|
|
if (I2cActive(DHT12_ADDR)) { return; }
|
|
|
|
|
|
|
|
if (Dht12Read()) {
|
|
|
|
I2cSetActiveFound(DHT12_ADDR, Dht12.name);
|
|
|
|
Dht12.count = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dht12EverySecond(void)
|
|
|
|
{
|
|
|
|
if (uptime &1) {
|
|
|
|
// DHT12: 55mS
|
|
|
|
if (!Dht12Read()) {
|
|
|
|
AddLogMissed(Dht12.name, Dht12.valid);
|
2019-12-17 20:31:01 +00:00
|
|
|
}
|
2019-12-21 11:43:07 +00:00
|
|
|
}
|
2019-12-17 20:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dht12Show(bool json)
|
|
|
|
{
|
2019-12-21 11:43:07 +00:00
|
|
|
if (Dht12.valid) {
|
2019-12-17 20:31:01 +00:00
|
|
|
char temperature[33];
|
2019-12-21 11:43:07 +00:00
|
|
|
dtostrfd(Dht12.temperature, Settings.flag2.temperature_resolution, temperature);
|
2019-12-17 20:31:01 +00:00
|
|
|
char humidity[33];
|
2019-12-21 11:43:07 +00:00
|
|
|
dtostrfd(Dht12.humidity, Settings.flag2.humidity_resolution, humidity);
|
2019-12-17 20:31:01 +00:00
|
|
|
|
2019-12-21 11:43:07 +00:00
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(JSON_SNS_TEMPHUM, Dht12.name, temperature, humidity);
|
2019-12-17 20:31:01 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2019-12-21 11:43:07 +00:00
|
|
|
if ((0 == tele_period)) {
|
2019-12-17 20:31:01 +00:00
|
|
|
DomoticzTempHumSensor(temperature, humidity);
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_KNX
|
2019-12-21 11:43:07 +00:00
|
|
|
if (0 == tele_period) {
|
|
|
|
KnxSensor(KNX_TEMPERATURE, Dht12.temperature);
|
|
|
|
KnxSensor(KNX_HUMIDITY, Dht12.humidity);
|
2019-12-17 20:31:01 +00:00
|
|
|
}
|
|
|
|
#endif // USE_KNX
|
|
|
|
#ifdef USE_WEBSERVER
|
2019-12-21 11:43:07 +00:00
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_TEMP, Dht12.name, temperature, TempUnit());
|
|
|
|
WSContentSend_PD(HTTP_SNS_HUM, Dht12.name, humidity);
|
2019-12-17 20:31:01 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Xsns58(uint8_t function)
|
|
|
|
{
|
|
|
|
if (!I2cEnabled(XI2C_41)) { return false; }
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
Dht12Detect();
|
|
|
|
}
|
2019-12-21 11:43:07 +00:00
|
|
|
else if (Dht12.count) {
|
2019-12-17 20:31:01 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
2019-12-21 11:43:07 +00:00
|
|
|
Dht12EverySecond();
|
2019-12-17 20:31:01 +00:00
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Dht12Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Dht12Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DHT12
|
|
|
|
#endif // USE_I2C
|