From 83518a9c38e3c2d31ebf27a4419485fc6089e342 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 27 Jun 2021 13:25:26 +0200 Subject: [PATCH] Update timing DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021 (#12488) --- CHANGELOG.md | 1 + RELEASENOTES.md | 1 + tasmota/xsns_06_dht.ino | 86 ++++++++++++----------------------------- 3 files changed, 27 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 171c62ad3..5c17c1704 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file. - Speed up initial GUI console refresh - Simplified configuration for ir-full and removal of tasmota-ircustom - Add Esp32 Partition Manager as a Berry component +- Update timing DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021 (#12488) ### Fixed - ESP32 Webcam add boundary marker before sending mjpeg image (#12376) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 76791941a..25806ee41 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -104,6 +104,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo - Force ESP32 defines USE_UFILESYS, GUI_TRASH_FILE and #define GUI_EDIT_FILE - Speed up initial GUI console refresh - Simplified configuration for ir-full and removal of tasmota-ircustom [#12428](https://github.com/arendst/Tasmota/issues/12428) +- Update timing DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021 [#12488](https://github.com/arendst/Tasmota/issues/12488) ### Fixed - DDS238-2 wrong reactive power value [#12283](https://github.com/arendst/Tasmota/issues/12283) diff --git a/tasmota/xsns_06_dht.ino b/tasmota/xsns_06_dht.ino index ff71417e3..46a250526 100644 --- a/tasmota/xsns_06_dht.ino +++ b/tasmota/xsns_06_dht.ino @@ -24,7 +24,10 @@ * Reading temperature or humidity takes about 250 milliseconds! * Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) * - * This version is based on ESPEasy _P005_DHT.ino 20191201 + * Changelog + * 20210627 - https://github.com/arendst/Tasmota/pull/12488 + * 20210524 - https://github.com/arendst/Tasmota/issues/12180 + * 20200621 - https://github.com/arendst/Tasmota/pull/7468#issuecomment-647067015 \*********************************************************************************************/ #define XSNS_06 6 @@ -47,25 +50,23 @@ struct DHTSTRUCT { float h = NAN; } Dht[DHT_MAX_SENSORS]; -// changed from bool to unsigned long returning the time to wait until state changes -unsigned long DhtWaitState(uint32_t sensor, uint32_t level) -{ - unsigned long timeout = micros() + 100; - unsigned long startms = micros(); +// 20210627: Changed from bool to uint32_t returning the time to wait until state changes +uint32_t DhtWaitState(uint32_t sensor, uint32_t level) { + uint32_t startms = micros(); + uint32_t timeout = startms + 100; while (digitalRead(Dht[sensor].pin) != level) { if (TimeReachedUsec(timeout)) { AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " %s " D_PULSE), (level) ? D_START_SIGNAL_HIGH : D_START_SIGNAL_LOW); - return 0; // return 0 (==false for compatibility) if timeout reached + return 0; // 20210627: Return 0 (==false for compatibility) if timeout reached } delayMicroseconds(1); } - return micros()-startms; + return micros() - startms; } -bool DhtRead(uint32_t sensor) -{ +bool DhtRead(uint32_t sensor) { dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0; if (!dht_dual_mode) { @@ -81,8 +82,8 @@ bool DhtRead(uint32_t sensor) break; case GPIO_DHT22: // DHT21, DHT22, AM2301, AM2302, AM2321 // delay(2); // minimum 1ms -// delayMicroseconds(2000); // See https://github.com/arendst/Tasmota/pull/7468#issuecomment-647067015 - delayMicroseconds(1100); // changed to Arduino DHT library value +// delayMicroseconds(2000); // 20200621: See https://github.com/arendst/Tasmota/pull/7468#issuecomment-647067015 + delayMicroseconds(1100); // 20210627: Changed to Arduino DHT library value break; case GPIO_SI7021: // iTead SI7021 delayMicroseconds(500); @@ -101,48 +102,20 @@ bool DhtRead(uint32_t sensor) delayMicroseconds(50); break; case GPIO_SI7021: // iTead SI7021 - delayMicroseconds(30); // See: https://github.com/letscontrolit/ESPEasy/issues/1798 and https://github.com/arendst/Tasmota/issues/12180 + delayMicroseconds(30); // See: https://github.com/letscontrolit/ESPEasy/issues/1798 and 20210524: https://github.com/arendst/Tasmota/issues/12180 break; } -/* - bool error = false; - noInterrupts(); - if (DhtWaitState(sensor, 0) && DhtWaitState(sensor, 1) && DhtWaitState(sensor, 0)) { - for (uint32_t i = 0; i < 5; i++) { - int data = 0; - for (uint32_t j = 0; j < 8; j++) { - if (!DhtWaitState(sensor, 1)) { - error = true; - break; - } - delayMicroseconds(35); // Was 30 - if (digitalRead(Dht[sensor].pin)) { - data |= (1 << (7 - j)); - } - if (!DhtWaitState(sensor, 0)) { - error = true; - break; - } - } - if (error) { break; } - dht_data[i] = data; - } - } else { - error = true; - } - interrupts(); - if (error) { return false; } -*/ - uint32_t i = 0; noInterrupts(); - if (DhtWaitState(sensor, 0)) { // only wait for one state change to 0 + if (DhtWaitState(sensor, 0)) { // Only wait for one state change to 0 for (i = 0; i < 40; i++) { if (!DhtWaitState(sensor, 1)) { break; } - int wt = DhtWaitState(sensor,0); // determine time to change state - if(wt==0) break; - if(wt>50) dht_data[i / 8] |= (1 << (7 - i % 8)); // if longer than 50 usec then 1, else 0 + uint32_t wt = DhtWaitState(sensor, 0); // Determine time to change state + if (0 == wt) { break; } + if (wt > 50) { + dht_data[i / 8] |= (1 << (7 - i % 8)); // If longer than 50 usec then 1, else 0 + } } } interrupts(); @@ -161,10 +134,6 @@ bool DhtRead(uint32_t sensor) switch (Dht[sensor].type) { case GPIO_DHT11: // DHT11 humidity = dht_data[0]; -/* - // DHT11 no negative temp: - temperature = dht_data[2] + ((float)dht_data[3] * 0.1f); // Issue #3164 -*/ // DHT11 (Adafruit): temperature = dht_data[2]; if (dht_data[3] & 0x80) { @@ -207,8 +176,7 @@ bool DhtRead(uint32_t sensor) /********************************************************************************************/ -bool DhtPinState() -{ +bool DhtPinState() { if ((XdrvMailbox.index >= AGPIO(GPIO_DHT11)) && (XdrvMailbox.index <= AGPIO(GPIO_SI7021))) { if (dht_sensors < DHT_MAX_SENSORS) { Dht[dht_sensors].pin = XdrvMailbox.payload; @@ -223,8 +191,7 @@ bool DhtPinState() return false; } -void DhtInit(void) -{ +void DhtInit(void) { if (dht_sensors) { if (PinUsed(GPIO_DHT11_OUT)) { dht_pin_out = Pin(GPIO_DHT11_OUT); @@ -247,8 +214,7 @@ void DhtInit(void) } } -void DhtEverySecond(void) -{ +void DhtEverySecond(void) { if (TasmotaGlobal.uptime &1) { // Every 2 seconds for (uint32_t sensor = 0; sensor < dht_sensors; sensor++) { // DHT11 and AM2301 25mS per sensor, SI7021 5mS per sensor @@ -263,8 +229,7 @@ void DhtEverySecond(void) } } -void DhtShow(bool json) -{ +void DhtShow(bool json) { for (uint32_t i = 0; i < dht_sensors; i++) { TempHumDewShow(json, ((0 == TasmotaGlobal.tele_period) && (0 == i)), Dht[i].stype, Dht[i].t, Dht[i].h); } @@ -274,8 +239,7 @@ void DhtShow(bool json) * Interface \*********************************************************************************************/ -bool Xsns06(uint8_t function) -{ +bool Xsns06(uint8_t function) { bool result = false; if (dht_active) {