2020-02-15 11:57:23 +00:00
|
|
|
/*
|
|
|
|
xsns_06_dht.ino - DHTxx, AM23xx and SI7021 temperature and humidity sensor support for Tasmota
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends
|
2020-02-15 11:57:23 +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_DHT
|
|
|
|
/*********************************************************************************************\
|
2020-11-29 20:08:08 +00:00
|
|
|
* DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021 - Temperature and Humidity
|
2020-02-15 11:57:23 +00:00
|
|
|
*
|
|
|
|
* Reading temperature or humidity takes about 250 milliseconds!
|
|
|
|
* Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
|
|
|
*
|
2021-06-27 12:25:26 +01:00
|
|
|
* Changelog
|
|
|
|
* 20210524 - https://github.com/arendst/Tasmota/issues/12180
|
|
|
|
* 20200621 - https://github.com/arendst/Tasmota/pull/7468#issuecomment-647067015
|
2021-06-27 13:20:00 +01:00
|
|
|
* 20200313 - https://github.com/arendst/Tasmota/issues/7717#issuecomment-585833243
|
2020-02-15 11:57:23 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_06 6
|
|
|
|
|
|
|
|
#define DHT_MAX_SENSORS 4
|
|
|
|
#define DHT_MAX_RETRY 8
|
|
|
|
|
|
|
|
uint8_t dht_data[5];
|
|
|
|
uint8_t dht_sensors = 0;
|
|
|
|
uint8_t dht_pin_out = 0; // Shelly GPIO00 output only
|
|
|
|
bool dht_active = true; // DHT configured
|
|
|
|
bool dht_dual_mode = false; // Single pin mode
|
|
|
|
|
|
|
|
struct DHTSTRUCT {
|
2021-02-14 12:06:19 +00:00
|
|
|
int8_t pin;
|
2020-02-15 11:57:23 +00:00
|
|
|
uint8_t type;
|
|
|
|
uint8_t lastresult;
|
|
|
|
char stype[12];
|
|
|
|
float t = NAN;
|
|
|
|
float h = NAN;
|
|
|
|
} Dht[DHT_MAX_SENSORS];
|
|
|
|
|
2021-06-27 13:20:00 +01:00
|
|
|
bool DhtWaitState(uint32_t sensor, uint32_t level) {
|
|
|
|
unsigned long timeout = micros() + 100;
|
2020-02-15 11:57:23 +00:00
|
|
|
while (digitalRead(Dht[sensor].pin) != level) {
|
|
|
|
if (TimeReachedUsec(timeout)) {
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " %s " D_PULSE),
|
2020-02-15 11:57:23 +00:00
|
|
|
(level) ? D_START_SIGNAL_HIGH : D_START_SIGNAL_LOW);
|
2021-06-27 13:20:00 +01:00
|
|
|
return false;
|
2020-02-15 11:57:23 +00:00
|
|
|
}
|
|
|
|
delayMicroseconds(1);
|
|
|
|
}
|
2021-06-27 13:20:00 +01:00
|
|
|
return true;
|
2020-02-15 11:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 12:25:26 +01:00
|
|
|
bool DhtRead(uint32_t sensor) {
|
2020-02-15 11:57:23 +00:00
|
|
|
dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0;
|
|
|
|
|
|
|
|
if (!dht_dual_mode) {
|
|
|
|
pinMode(Dht[sensor].pin, OUTPUT);
|
|
|
|
digitalWrite(Dht[sensor].pin, LOW);
|
|
|
|
} else {
|
|
|
|
digitalWrite(dht_pin_out, LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Dht[sensor].type) {
|
|
|
|
case GPIO_DHT11: // DHT11
|
|
|
|
delay(19); // minimum 18ms
|
|
|
|
break;
|
|
|
|
case GPIO_DHT22: // DHT21, DHT22, AM2301, AM2302, AM2321
|
2020-06-22 10:14:12 +01:00
|
|
|
// delay(2); // minimum 1ms
|
2021-06-27 13:20:00 +01:00
|
|
|
delayMicroseconds(2000); // 20200621: See https://github.com/arendst/Tasmota/pull/7468#issuecomment-647067015
|
2020-02-15 11:57:23 +00:00
|
|
|
break;
|
|
|
|
case GPIO_SI7021: // iTead SI7021
|
|
|
|
delayMicroseconds(500);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dht_dual_mode) {
|
|
|
|
pinMode(Dht[sensor].pin, INPUT_PULLUP);
|
|
|
|
} else {
|
|
|
|
digitalWrite(dht_pin_out, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Dht[sensor].type) {
|
|
|
|
case GPIO_DHT11: // DHT11
|
|
|
|
case GPIO_DHT22: // DHT21, DHT22, AM2301, AM2302, AM2321
|
|
|
|
delayMicroseconds(50);
|
|
|
|
break;
|
|
|
|
case GPIO_SI7021: // iTead SI7021
|
2021-06-27 12:25:26 +01:00
|
|
|
delayMicroseconds(30); // See: https://github.com/letscontrolit/ESPEasy/issues/1798 and 20210524: https://github.com/arendst/Tasmota/issues/12180
|
2020-02-15 11:57:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-23 20:26:58 +00:00
|
|
|
uint32_t i = 0;
|
|
|
|
noInterrupts();
|
2021-06-27 13:20:00 +01:00
|
|
|
if (DhtWaitState(sensor, 0) && DhtWaitState(sensor, 1) && DhtWaitState(sensor, 0)) {
|
2020-02-23 20:26:58 +00:00
|
|
|
for (i = 0; i < 40; i++) {
|
|
|
|
if (!DhtWaitState(sensor, 1)) { break; }
|
2021-06-27 13:20:00 +01:00
|
|
|
delayMicroseconds(32); // Was 30
|
|
|
|
if (digitalRead(Dht[sensor].pin)) {
|
|
|
|
dht_data[i / 8] |= (1 << (7 - i % 8));
|
2021-06-27 12:25:26 +01:00
|
|
|
}
|
2021-06-27 13:20:00 +01:00
|
|
|
if (!DhtWaitState(sensor, 0)) { break; }
|
2020-02-23 20:26:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
interrupts();
|
2021-12-26 13:00:07 +00:00
|
|
|
|
|
|
|
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("DHT: Read %5_H"), dht_data);
|
|
|
|
|
2020-02-23 20:26:58 +00:00
|
|
|
if (i < 40) { return false; }
|
2020-02-15 11:57:23 +00:00
|
|
|
|
|
|
|
uint8_t checksum = (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF;
|
|
|
|
if (dht_data[4] != checksum) {
|
|
|
|
char hex_char[15];
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_CHECKSUM_FAILURE " %s =? %02X"),
|
2020-02-15 11:57:23 +00:00
|
|
|
ToHex_P(dht_data, 5, hex_char, sizeof(hex_char), ' '), checksum);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float temperature = NAN;
|
|
|
|
float humidity = NAN;
|
|
|
|
switch (Dht[sensor].type) {
|
2021-02-14 11:03:49 +00:00
|
|
|
case GPIO_DHT11: // DHT11
|
2020-02-15 11:57:23 +00:00
|
|
|
humidity = dht_data[0];
|
2021-06-27 13:20:00 +01:00
|
|
|
// 20200313: DHT11 (Adafruit):
|
2021-02-14 11:03:49 +00:00
|
|
|
temperature = dht_data[2];
|
|
|
|
if (dht_data[3] & 0x80) {
|
|
|
|
temperature = -1 - temperature;
|
|
|
|
}
|
|
|
|
temperature += (dht_data[3] & 0x0f) * 0.1f;
|
|
|
|
/*
|
|
|
|
// DHT12 (Adafruit):
|
|
|
|
temperature = dht_data[2];
|
|
|
|
temperature += (dht_data[3] & 0x0f) * 0.1f;
|
|
|
|
if (dht_data[2] & 0x80) {
|
|
|
|
temperature *= -1;
|
|
|
|
}
|
|
|
|
*/
|
2020-02-15 11:57:23 +00:00
|
|
|
break;
|
2021-02-14 11:03:49 +00:00
|
|
|
case GPIO_DHT22: // DHT21, DHT22, AM2301, AM2302, AM2321
|
|
|
|
case GPIO_SI7021: // iTead SI7021
|
2020-02-15 11:57:23 +00:00
|
|
|
humidity = ((dht_data[0] << 8) | dht_data[1]) * 0.1;
|
2021-02-14 11:03:49 +00:00
|
|
|
// DHT21/22 (Adafruit):
|
|
|
|
temperature = ((int16_t)(dht_data[2] & 0x7F) << 8 ) | dht_data[3];
|
|
|
|
temperature *= 0.1f;
|
2020-02-15 11:57:23 +00:00
|
|
|
if (dht_data[2] & 0x80) {
|
|
|
|
temperature *= -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-02-16 11:33:18 +00:00
|
|
|
if (isnan(temperature) || isnan(humidity)) {
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT "Invalid NAN reading"));
|
2020-02-15 11:57:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (humidity > 100) { humidity = 100.0; }
|
|
|
|
if (humidity < 0) { humidity = 0.1; }
|
|
|
|
Dht[sensor].h = ConvertHumidity(humidity);
|
|
|
|
Dht[sensor].t = ConvertTemp(temperature);
|
|
|
|
Dht[sensor].lastresult = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2021-06-27 12:25:26 +01:00
|
|
|
bool DhtPinState() {
|
2020-06-24 14:36:54 +01:00
|
|
|
if ((XdrvMailbox.index >= AGPIO(GPIO_DHT11)) && (XdrvMailbox.index <= AGPIO(GPIO_SI7021))) {
|
2020-02-15 11:57:23 +00:00
|
|
|
if (dht_sensors < DHT_MAX_SENSORS) {
|
|
|
|
Dht[dht_sensors].pin = XdrvMailbox.payload;
|
2020-06-24 14:36:54 +01:00
|
|
|
Dht[dht_sensors].type = BGPIO(XdrvMailbox.index);
|
2020-02-15 11:57:23 +00:00
|
|
|
dht_sensors++;
|
2020-06-24 14:36:54 +01:00
|
|
|
XdrvMailbox.index = AGPIO(GPIO_DHT11);
|
2020-02-15 11:57:23 +00:00
|
|
|
} else {
|
|
|
|
XdrvMailbox.index = 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-27 12:25:26 +01:00
|
|
|
void DhtInit(void) {
|
2020-02-15 11:57:23 +00:00
|
|
|
if (dht_sensors) {
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_DHT11_OUT)) {
|
|
|
|
dht_pin_out = Pin(GPIO_DHT11_OUT);
|
2020-02-15 11:57:23 +00:00
|
|
|
dht_dual_mode = true; // Dual pins mode as used by Shelly
|
|
|
|
dht_sensors = 1; // We only support one sensor in pseudo mode
|
|
|
|
pinMode(dht_pin_out, OUTPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < dht_sensors; i++) {
|
|
|
|
pinMode(Dht[i].pin, INPUT_PULLUP);
|
2020-02-16 11:33:18 +00:00
|
|
|
Dht[i].lastresult = DHT_MAX_RETRY; // Start with NAN
|
2020-02-15 11:57:23 +00:00
|
|
|
GetTextIndexed(Dht[i].stype, sizeof(Dht[i].stype), Dht[i].type, kSensorNames);
|
|
|
|
if (dht_sensors > 1) {
|
|
|
|
snprintf_P(Dht[i].stype, sizeof(Dht[i].stype), PSTR("%s%c%02d"), Dht[i].stype, IndexSeparator(), Dht[i].pin);
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 16:10:06 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT "(v5) " D_SENSORS_FOUND " %d"), dht_sensors);
|
2020-02-15 11:57:23 +00:00
|
|
|
} else {
|
|
|
|
dht_active = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 12:25:26 +01:00
|
|
|
void DhtEverySecond(void) {
|
2020-10-28 16:32:07 +00:00
|
|
|
if (TasmotaGlobal.uptime &1) { // Every 2 seconds
|
2020-02-16 11:33:18 +00:00
|
|
|
for (uint32_t sensor = 0; sensor < dht_sensors; sensor++) {
|
2020-02-15 11:57:23 +00:00
|
|
|
// DHT11 and AM2301 25mS per sensor, SI7021 5mS per sensor
|
2020-02-16 11:33:18 +00:00
|
|
|
if (!DhtRead(sensor)) {
|
|
|
|
Dht[sensor].lastresult++;
|
|
|
|
if (Dht[sensor].lastresult > DHT_MAX_RETRY) { // Reset after 8 misses
|
|
|
|
Dht[sensor].t = NAN;
|
|
|
|
Dht[sensor].h = NAN;
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 11:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 12:25:26 +01:00
|
|
|
void DhtShow(bool json) {
|
2020-03-16 15:52:22 +00:00
|
|
|
for (uint32_t i = 0; i < dht_sensors; i++) {
|
2020-10-29 12:37:09 +00:00
|
|
|
TempHumDewShow(json, ((0 == TasmotaGlobal.tele_period) && (0 == i)), Dht[i].stype, Dht[i].t, Dht[i].h);
|
2020-03-16 15:52:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-15 11:57:23 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2021-06-27 12:25:26 +01:00
|
|
|
bool Xsns06(uint8_t function) {
|
2020-02-15 11:57:23 +00:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (dht_active) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
DhtEverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
DhtShow(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
DhtShow(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
case FUNC_INIT:
|
|
|
|
DhtInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PIN_STATE:
|
|
|
|
result = DhtPinState();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DHT
|