From d199cfcc394618883740c01939fe09b056294d8c Mon Sep 17 00:00:00 2001 From: Stefan Oskamp <57906326+stefan-oskamp@users.noreply.github.com> Date: Tue, 17 Dec 2019 21:25:51 +0100 Subject: [PATCH 1/6] Added DHT12 Refer to issue #7229 --- I2CDEVICES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/I2CDEVICES.md b/I2CDEVICES.md index 73f4a9c6b..6d0de4e1a 100644 --- a/I2CDEVICES.md +++ b/I2CDEVICES.md @@ -61,4 +61,5 @@ Index | Define | Driver | Device | Address(es) | Description 38 | USE_DISPLAY_ILI9488 | xdsp_08 | FT6236 | 0x38 | Touch panel controller 39 | USE_DISPLAY_RA8876 | xdsp_10 | FT5316 | 0x38 | Touch panel controller 40 | USE_TSL2591 | xsns_57 | TLS2591 | 0x29 | Light intensity sensor + 41 | USE_DHT12 | xsns_58 | DHT12 | 0x5C | Temperature and humidity sensor From 9a7dfe7178449caa21f00838ccb608047d198f5a Mon Sep 17 00:00:00 2001 From: Stefan Oskamp <57906326+stefan-oskamp@users.noreply.github.com> Date: Tue, 17 Dec 2019 21:31:01 +0100 Subject: [PATCH 2/6] Driver for I2C sensor DHT12 Refer to issue #7229 --- tasmota/xsns_58_dht12.ino | 140 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 tasmota/xsns_58_dht12.ino diff --git a/tasmota/xsns_58_dht12.ino b/tasmota/xsns_58_dht12.ino new file mode 100644 index 000000000..1c60329b8 --- /dev/null +++ b/tasmota/xsns_58_dht12.ino @@ -0,0 +1,140 @@ +/* + xsns_58_dht12.ino - DHT12 I2C temperature and humidity sensor support for Tasmota + + Copyright (C) 2019 Stefan Oskamp and Theo Arends + + 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 . +*/ + +#ifdef USE_I2C +#ifdef USE_DHT12 +/*********************************************************************************************\ + * DHT12 - Temperature and Humidity + * + * I2C Address: 0x5C +\*********************************************************************************************/ + +#define XSNS_58 58 +#define XI2C_41 41 // See I2CDEVICES.md + +#define DHT12_ADDR (uint8_t) 0x5C + +char dht12_name[] = "DHT12-0x00"; +uint8_t dht12_count = 0; +float dht12_temperature = NAN; +float dht12_humidity = NAN; + +bool Dht12Read(void) +{ + Wire.beginTransmission(DHT12_ADDR); + Wire.write(0); + if (Wire.endTransmission() != 0) { + return false; + } + delay(50); + Wire.requestFrom(DHT12_ADDR, (uint8_t) 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 = (float) humidity + (float) humidityTenth/(float) 10.0; + dht12_temperature = (float) temp + (float) tempTenth/(float) 10.0; + return (!isnan(dht12_temperature) && !isnan(dht12_humidity)); +} + +void Dht12Detect(void) +{ + if (Dht12Read()) { + snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, dht12_name, DHT12_ADDR); + AddLog(LOG_LEVEL_DEBUG); + snprintf_P(dht12_name, sizeof(dht12_name), PSTR("%s%c0x%02X"), "DHT12", IndexSeparator(), DHT12_ADDR); + dht12_count = 1; + } +} + +void Dht12Show(bool json) +{ + if (dht12_count > 0) + { + char temperature[33]; + dtostrfd(dht12_temperature, Settings.flag2.temperature_resolution, temperature); + char humidity[33]; + dtostrfd(dht12_humidity, Settings.flag2.humidity_resolution, humidity); + + if (json) + { + ResponseAppend_P(JSON_SNS_TEMPHUM, dht12_name, temperature, humidity); +#ifdef USE_DOMOTICZ + if ((0 == tele_period)) + { + DomoticzTempHumSensor(temperature, humidity); + } +#endif // USE_DOMOTICZ + +#ifdef USE_KNX + if (0 == tele_period) + { + KnxSensor(KNX_TEMPERATURE, dht12_temperature); + KnxSensor(KNX_HUMIDITY, dht12_humidity); + } +#endif // USE_KNX + +#ifdef USE_WEBSERVER + } + else + { + WSContentSend_PD(HTTP_SNS_TEMP, dht12_name, temperature, TempUnit()); + WSContentSend_PD(HTTP_SNS_HUM, dht12_name, humidity); +#endif // USE_WEBSERVER + } + } +} + + + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +bool Xsns58(uint8_t function) +{ + if (!I2cEnabled(XI2C_41)) { return false; } + + bool result = false; + + if (FUNC_INIT == function) { + Dht12Detect(); + } + else if (dht12_count > 0) { + switch (function) { + case FUNC_EVERY_SECOND: + Dht12Read(); + 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 From 3c835f0ee71fd0365e98304c8d5edcc7ee751d19 Mon Sep 17 00:00:00 2001 From: Stefan Oskamp <57906326+stefan-oskamp@users.noreply.github.com> Date: Tue, 17 Dec 2019 21:32:55 +0100 Subject: [PATCH 3/6] Added USE_DHT12 Refer to issue #7229 --- tasmota/tasmota_post.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tasmota/tasmota_post.h b/tasmota/tasmota_post.h index 75f35cd92..108726c77 100644 --- a/tasmota/tasmota_post.h +++ b/tasmota/tasmota_post.h @@ -135,6 +135,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #define USE_SGP30 // Add I2C code for SGP30 sensor (+1k1 code) //#define USE_SI1145 // Add I2C code for SI1145/46/47 sensor (+1k code) #define USE_LM75AD // Add I2C code for LM75AD sensor (+0k5 code) +#define USE_DHT12 // Add I2C code for DHT12 temperature and humidity sensor (+0kx code) //#define USE_APDS9960 // Add I2C code for APDS9960 Proximity Sensor. Disables SHT and VEML6070 (+4k7 code) //#define USE_MCP230xx // Enable MCP23008/MCP23017 - Must define I2C Address in #define USE_MCP230xx_ADDR below - range 0x20 - 0x27 (+4k7 code) // #define USE_MCP230xx_ADDR 0x20 // Enable MCP23008/MCP23017 I2C Address to use (Must be within range 0x20 through 0x27 - set according to your wired setup) From c2d60b2ab772f109cd2b606df7eef56e029afdc7 Mon Sep 17 00:00:00 2001 From: Stefan Oskamp <57906326+stefan-oskamp@users.noreply.github.com> Date: Tue, 17 Dec 2019 22:19:49 +0100 Subject: [PATCH 4/6] Added entry for I2C sensor DHT12 --- tasmota/my_user_config.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 04318d9c8..d5645a9af 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -418,6 +418,7 @@ // #define USE_PAJ7620 // [I2cDriver34] Enable PAJ7620 gesture sensor (I2C address 0x73) (+2.5k code) // #define USE_PCF8574 // [I2cDriver2] Enable PCF8574 I/O Expander (I2C addresses 0x20 - 0x26 and 0x39 - 0x3F) (+1k9 code) // #define USE_HIH6 // [I2cDriver36] Enable Honeywell HIH Humidity and Temperature sensor (I2C address 0x27) (+0k6) +// #define USE_DHT12 // [I2cDriver41] Enable DHT12 humidity and temperature sensor (I2C address 0x5C) // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0 From eb4b3d306b4dfe9ae56e94539c1e88dcb837b310 Mon Sep 17 00:00:00 2001 From: Stefan Oskamp <57906326+stefan-oskamp@users.noreply.github.com> Date: Tue, 17 Dec 2019 22:29:16 +0100 Subject: [PATCH 5/6] Added code size for USE_DHT12 --- tasmota/my_user_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index d5645a9af..73a965d30 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -418,7 +418,7 @@ // #define USE_PAJ7620 // [I2cDriver34] Enable PAJ7620 gesture sensor (I2C address 0x73) (+2.5k code) // #define USE_PCF8574 // [I2cDriver2] Enable PCF8574 I/O Expander (I2C addresses 0x20 - 0x26 and 0x39 - 0x3F) (+1k9 code) // #define USE_HIH6 // [I2cDriver36] Enable Honeywell HIH Humidity and Temperature sensor (I2C address 0x27) (+0k6) -// #define USE_DHT12 // [I2cDriver41] Enable DHT12 humidity and temperature sensor (I2C address 0x5C) +// #define USE_DHT12 // [I2cDriver41] Enable DHT12 humidity and temperature sensor (I2C address 0x5C) (+0k7 code) // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0 From e853cc65e597bd47e164ee0a8bfef8061e0d5d8d Mon Sep 17 00:00:00 2001 From: Stefan Oskamp <57906326+stefan-oskamp@users.noreply.github.com> Date: Tue, 17 Dec 2019 22:31:08 +0100 Subject: [PATCH 6/6] Added code size for USE_DHT12 --- tasmota/tasmota_post.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasmota/tasmota_post.h b/tasmota/tasmota_post.h index 108726c77..b095fdbf2 100644 --- a/tasmota/tasmota_post.h +++ b/tasmota/tasmota_post.h @@ -135,7 +135,7 @@ char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, c #define USE_SGP30 // Add I2C code for SGP30 sensor (+1k1 code) //#define USE_SI1145 // Add I2C code for SI1145/46/47 sensor (+1k code) #define USE_LM75AD // Add I2C code for LM75AD sensor (+0k5 code) -#define USE_DHT12 // Add I2C code for DHT12 temperature and humidity sensor (+0kx code) +#define USE_DHT12 // Add I2C code for DHT12 temperature and humidity sensor (+0k7 code) //#define USE_APDS9960 // Add I2C code for APDS9960 Proximity Sensor. Disables SHT and VEML6070 (+4k7 code) //#define USE_MCP230xx // Enable MCP23008/MCP23017 - Must define I2C Address in #define USE_MCP230xx_ADDR below - range 0x20 - 0x27 (+4k7 code) // #define USE_MCP230xx_ADDR 0x20 // Enable MCP23008/MCP23017 I2C Address to use (Must be within range 0x20 through 0x27 - set according to your wired setup)