Add ESP32 Sonoff sensor support

- Bump version to v12.0.2.3
- Add ESP32 SI7021, THS01 and MS01 support
- Fix THRxxD template
This commit is contained in:
Theo Arends 2022-07-06 16:58:27 +02:00
parent 9975524368
commit e6b4a77abd
12 changed files with 484 additions and 96 deletions

View File

@ -234,6 +234,7 @@ Note: `minimal` variant is not listed as it shouldn't be used outside of the [up
| USE_DISPLAY_RA8876 | - | - / - | - | - | - | x |
| USE_DISPLAY_ST7789 | - | - / - | - | - | - | x |
| USE_DISPLAY_TM1637 | - | - / - | - | - | - | x |
| USE_DISPLAY_TM1621_SONOFF | - | - / x | - | - | - | - |
| | | | | | | |
| USE_FT5206 | - | - / - | - | - | - | - |
| USE_FTC532 | - | - / - | - | - | - | - |

View File

@ -3,18 +3,23 @@ All notable changes to this project will be documented in this file.
## [Unreleased] - Development
## [12.0.2.3]
### Added
- Support for Sonoff POWR3xxD and THR3xxD (#15856)
### Changed
- Driver DHT v6 consolidation for both ESP8266 and ESP32 to support SI7021, THS01 and MS01 on ESP32 (#15856)
### Fixed
### Removed
## [12.0.2.2]
### Added
- Command ``GlobalTemp2 1..250`` to select Global Temperature source indexed from teleperiod occurance data (#15834)
- Command ``GlobalHum2 1..250`` to select Global Humidity source indexed from teleperiod occurance data (#15834)
- Command ``GlobalPress2 1..250`` to select Global Pressure source indexed from teleperiod occurance data (#15834)
### Changed
### Fixed
### Removed
## [12.0.2.1] 20220622
### Added
- Support for 5-channel light dimmer driver SM2335 used in SwitchBot Color Bulbs (#15839)

View File

@ -107,16 +107,18 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo
[Complete list](BUILDS.md) of available feature and sensors.
## Changelog v12.0.2.2
## Changelog v12.0.2.3
### Added
- Command ``GlobalTemp2 1..250`` to select Global Temperature source indexed from teleperiod occurance data [#15834](https://github.com/arendst/Tasmota/issues/15834)
- Command ``GlobalHum2 1..250`` to select Global Humidity source indexed from teleperiod occurance data [#15834](https://github.com/arendst/Tasmota/issues/15834)
- Command ``GlobalPress2 1..250`` to select Global Pressure source indexed from teleperiod occurance data [#15834](https://github.com/arendst/Tasmota/issues/15834)
- Support for 5-channel light dimmer driver SM2335 used in SwitchBot Color Bulbs [#15839](https://github.com/arendst/Tasmota/issues/15839)
- Support for Sonoff POWR3xxD and THR3xxD [#15856](https://github.com/arendst/Tasmota/issues/15856)
### Breaking Changed
### Changed
- Driver DHT v6 consolidation for both ESP8266 and ESP32 to support SI7021, THS01 and MS01 on ESP32 [#15856](https://github.com/arendst/Tasmota/issues/15856)
### Fixed
- ESP32 SendMail not working over ethernet [#15794](https://github.com/arendst/Tasmota/issues/15794)

View File

@ -697,6 +697,7 @@
//#define USE_A4988_STEPPER // Add support for A4988/DRV8825 stepper-motor-driver-circuit (+10k5 code)
//#define USE_THERMOSTAT // Add support for Thermostat
#define USE_ETHERNET // Add support for ethernet (+20k code)
#define USE_DISPLAY_TM1621_SONOFF // Add support for TM1621 display driver used by Sonoff POWR3xxD and THR3xxD
#ifndef USE_KNX
#define USE_KNX // Enable KNX IP Protocol Support (+23k code, +3k3 mem)

View File

@ -20,6 +20,6 @@
#ifndef _TASMOTA_VERSION_H_
#define _TASMOTA_VERSION_H_
const uint32_t VERSION = 0x0C000202; // 12.0.2.2
const uint32_t VERSION = 0x0C000203; // 12.0.2.3
#endif // _TASMOTA_VERSION_H_

View File

@ -1007,6 +1007,7 @@
#define USE_ESP32_SENSORS // Add support for ESP32 temperature and optional hall effect sensor
//#define USE_SONOFF_SPM // Add support for ESP32 based Sonoff Smart Stackable Power Meter (+11k code)
//#define USE_DISPLAY_TM1621_SONOFF // Add support for TM1621 dsiplay driver used by Sonoff POWR3xxD and THR3xxD
//#define USE_ETHERNET // Add support for ethernet (+20k code)
// #define USE_WT32_ETH01 // Add support for Wireless-Tag WT32-ETH01

View File

@ -818,7 +818,9 @@ void ResponseAppendFeatures(void)
#if defined(USE_LIGHT) && defined(USE_SM2335)
feature8 |= 0x40000000; // xlgt_09_sm2335.ino
#endif
// feature8 |= 0x80000000;
#ifdef USE_DISPLAY_TM1621_SONOFF
feature8 |= 0x80000000; // xdrv_87_tm1621_sonoff.ino
#endif
}
static uint32_t feature9 = 0x00000000;

View File

@ -12,7 +12,7 @@
*
* {"NAME":"Sonoff POWR316D","GPIO":[32,0,0,0,0,576,0,0,0,224,9280,0,3104,0,320,0,0,0,0,0,0,9184,9248,9216,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":1}
* {"NAME":"Sonoff POWR320D","GPIO":[32,0,224,0,225,576,0,0,0,0,9280,0,3104,0,320,0,0,0,0,0,0,9184,9248,9216,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":1}
* {"NAME":"Sonoff THR316D","GPIO":[32,0,0,0,225,9280,0,0,0,321,0,576,320,9184,9216,0,0,224,0,9248,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":1}
* {"NAME":"Sonoff THR316D","GPIO":[32,0,0,0,225,9280,0,0,0,321,0,576,320,9184,9216,0,0,224,0,9248,0,1,0,3840,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":1}
\*********************************************************************************************/
#define XDRV_87 87

View File

@ -64,6 +64,9 @@ void CseReceived(void) {
// F2 5A 02 F7 60 00 03 61 00 40 10 05 72 40 51 A6 58 63 10 1B E1 7F 4D 4E - F2 = Power cycle exceeds range - takes too long - No load
// 55 5A 02 F7 60 00 03 5A 00 40 10 04 8B 9F 51 A6 58 18 72 75 61 AC A1 30 - 55 = Ok, 61 = Power not valid (load below 5W)
// 55 5A 02 F7 60 00 03 AB 00 40 10 02 60 5D 51 A6 58 03 E9 EF 71 0B 7A 36 - 55 = Ok, 71 = Ok
// 55 5A 02 DB 40 00 03 25 00 3D 18 03 8E CD 4F 0A 60 2A 56 85 61 01 02 1A - OK voltage
// 55 5A 02 DB 40 07 17 1D 00 3D 18 03 8E CD 4F 0A 60 2B EF EA 61 01 02 2C - Wrong voltage
// Hd Id VCal---- Voltage- ICal---- Current- PCal---- Power--- Ad CF--- Ck
uint8_t header = Cse.rx_buffer[0];

View File

@ -18,7 +18,7 @@
*/
#ifdef ESP8266
#ifdef USE_DHT
#ifdef USE_DHT_V5
/*********************************************************************************************\
* DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021 - Temperature and Humidity
*
@ -50,7 +50,7 @@ struct DHTSTRUCT {
int16_t raw;
char stype[12];
int8_t pin;
uint8_t type;
uint16_t type;
uint8_t lastresult;
} Dht[DHT_MAX_SENSORS];
@ -111,7 +111,12 @@ bool DhtRead(uint32_t sensor) {
}
uint32_t i = 0;
#ifdef ESP32
{ portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
portENTER_CRITICAL(&mux);
#else
noInterrupts();
#endif
if (DhtWaitState(sensor, 0) && DhtWaitState(sensor, 1) && DhtWaitState(sensor, 0)) {
for (i = 0; i < 40; i++) {
if (!DhtWaitState(sensor, 1)) { break; }
@ -122,7 +127,11 @@ bool DhtRead(uint32_t sensor) {
if (!DhtWaitState(sensor, 0)) { break; }
}
}
#ifdef ESP32
portEXIT_CRITICAL(&mux); }
#else
interrupts();
#endif
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("DHT: Read %5_H"), dht_data);
@ -312,5 +321,5 @@ bool Xsns06(uint8_t function) {
return result;
}
#endif // USE_DHT
#endif // USE_DHT_V5
#endif // ESP8266

View File

@ -0,0 +1,364 @@
/*
xsns_06_dht.ino - DHTxx, AM23xx and SI7021 temperature and humidity sensor support for Tasmota
SPDX-FileCopyrightText: 2022 Theo Arends
SPDX-License-Identifier: GPL-3.0-only
*/
#ifdef USE_DHT
/*********************************************************************************************\
* DHT11, AM2301 (DHT21, DHT22, AM2302, AM2321), SI7021, THS01, MS01 - Temperature and Humidity
*
* Reading temperature or humidity takes about 250 milliseconds!
* Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
*
* Changelog
* 20220706 - v6
* - Consolidate Adafruit DHT library
* - Fix ESP32 interrupt control to solve intermittent results
* 20211229 - Change poll time from to 2 to 4 seconds for better results
* 20211226 - https://github.com/arendst/Tasmota/pull/14173
* 20210524 - https://github.com/arendst/Tasmota/issues/12180
* 20200621 - https://github.com/arendst/Tasmota/pull/7468#issuecomment-647067015
* 20200313 - https://github.com/arendst/Tasmota/issues/7717#issuecomment-585833243
\*********************************************************************************************/
#define XSNS_06 6
#define DHT_MAX_SENSORS 4
#define DHT_MAX_RETRY 8
uint32_t dht_maxcycles;
uint8_t dht_data[5];
uint8_t dht_sensors = 0;
uint8_t dht_pin;
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 {
float t = NAN;
float h = NAN;
int16_t raw;
char stype[12];
int8_t pin;
uint16_t type;
uint8_t lastresult;
} Dht[DHT_MAX_SENSORS];
// Expect the signal line to be at the specified level for a period of time and
// return a count of loop cycles spent at that level (this cycle count can be
// used to compare the relative time of two pulses). If more than a millisecond
// ellapses without the level changing then the call fails with a 0 response.
// This is adapted from Arduino's pulseInLong function
uint32_t DhtExpectPulse(bool level) {
uint32_t count = 0;
while (digitalRead(dht_pin) == level) {
if (count++ >= dht_maxcycles) {
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 UINT32_MAX; // Exceeded timeout, fail.
}
}
return count;
}
bool DhtRead(uint32_t sensor) {
dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0;
dht_pin = Dht[sensor].pin;
if (!dht_dual_mode) {
// Go into high impedence state to let pull-up raise data line level and
// start the reading process.
pinMode(dht_pin, INPUT_PULLUP);
delay(1);
// First set data line low for a period according to sensor type
pinMode(dht_pin, OUTPUT);
digitalWrite(dht_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
// delay(2); // minimum 1ms
delayMicroseconds(2000); // 20200621: See https://github.com/arendst/Tasmota/pull/7468#issuecomment-647067015
break;
case GPIO_SI7021: // iTead SI7021
// delayMicroseconds(500);
delayMicroseconds(400); // Higher results in Timeout waiting for start signal high pulse
break;
case GPIO_MS01: // Sonoff MS01
delayMicroseconds(450);
break;
}
uint32_t cycles[80];
uint32_t i = 0;
// End the start signal by setting data line high for 40 microseconds.
if (!dht_dual_mode) {
pinMode(dht_pin, INPUT_PULLUP);
} else {
digitalWrite(dht_pin_out, HIGH);
}
// Delay a moment to let sensor pull data line low.
switch (Dht[sensor].type) {
case GPIO_DHT11: // DHT11
case GPIO_DHT22: // DHT21, DHT22, AM2301, AM2302, AM2321
delayMicroseconds(50);
break;
case GPIO_SI7021: // iTead SI7021
case GPIO_MS01: // Sonoff MS01
delayMicroseconds(30); // See: https://github.com/letscontrolit/ESPEasy/issues/1798 and 20210524: https://github.com/arendst/Tasmota/issues/12180
break;
}
// Now start reading the data line to get the value from the DHT sensor.
// Turn off interrupts temporarily because the next sections
// are timing critical and we don't want any interruptions.
#ifdef ESP32
{portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
portENTER_CRITICAL(&mux);
#else
noInterrupts();
#endif
// First expect a low signal for ~80 microseconds followed by a high signal
// for ~80 microseconds again.
if ((DhtExpectPulse(LOW) != UINT32_MAX) && (DhtExpectPulse(HIGH) != UINT32_MAX)) {
// Now read the 40 bits sent by the sensor. Each bit is sent as a 50
// microsecond low pulse followed by a variable length high pulse. If the
// high pulse is ~28 microseconds then it's a 0 and if it's ~70 microseconds
// then it's a 1. We measure the cycle count of the initial 50us low pulse
// and use that to compare to the cycle count of the high pulse to determine
// if the bit is a 0 (high state cycle count < low state cycle count), or a
// 1 (high state cycle count > low state cycle count). Note that for speed
// all the pulses are read into a array and then examined in a later step.
for (i = 0; i < 80; i += 2) {
cycles[i] = DhtExpectPulse(LOW);
if (cycles[i] == UINT32_MAX) { break; }
cycles[i + 1] = DhtExpectPulse(HIGH);
if (cycles[1 + i] == UINT32_MAX) { break; }
}
}
#ifdef ESP32
portEXIT_CRITICAL(&mux);}
#else
interrupts();
#endif
if (i < 80) { return false; }
// Inspect pulses and determine which ones are 0 (high state cycle count < low
// state cycle count), or 1 (high state cycle count > low state cycle count).
for (int i = 0; i < 40; ++i) {
uint32_t lowCycles = cycles[2 * i];
uint32_t highCycles = cycles[2 * i + 1];
dht_data[i / 8] <<= 1;
// Now compare the low and high cycle times to see if the bit is a 0 or 1.
if (highCycles > lowCycles) {
// High cycles are greater than 50us low cycle count, must be a 1.
dht_data[i / 8] |= 1;
}
// Else high cycles are less than (or equal to, a weird case) the 50us low
// cycle count so this must be a zero. Nothing needs to be changed in the
// stored data.
}
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("DHT: Read %5_H"), dht_data);
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];
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_CHECKSUM_FAILURE " %s =? %02X"),
ToHex_P(dht_data, 5, hex_char, sizeof(hex_char), ' '), checksum);
return false;
}
float temperature = NAN;
float humidity = NAN;
switch (Dht[sensor].type) {
case GPIO_DHT11: // DHT11
humidity = dht_data[0];
// 20200313: DHT11 (Adafruit):
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;
}
*/
break;
case GPIO_DHT22: // DHT21, DHT22, AM2301, AM2302, AM2321
case GPIO_SI7021: { // iTead SI7021
humidity = ((dht_data[0] << 8) | dht_data[1]) * 0.1f;
// DHT21/22 (Adafruit):
int16_t temp16 = dht_data[2] << 8 | dht_data[3]; // case 1 : signed 16 bits
if ((dht_data[2] & 0xF0) == 0x80) { // case 2 : negative when high nibble = 0x80
temp16 = -(0xFFF & temp16);
}
temperature = 0.1f * temp16;
break;
}
case GPIO_MS01: { // Sonoff MS01
int16_t voltage = ((dht_data[0] << 8) | dht_data[1]);
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("DHT: MS01 %d"), voltage);
// Rough approximate of soil moisture % (based on values observed in the eWeLink app)
// Observed values are available here: https://gist.github.com/minovap/654cdcd8bc37bb0d2ff338f8d144a509
float x;
if (voltage < 15037) {
x = voltage - 15200;
humidity = - FastPrecisePowf(0.0024f * x, 3) - 0.0004f * x + 20.1f;
}
else if (voltage < 22300) {
humidity = - 0.00069f * voltage + 30.6f;
}
else {
x = voltage - 22800;
humidity = - FastPrecisePowf(0.00046f * x, 3) - 0.0004f * x + 15;
}
temperature = 0;
Dht[sensor].raw = voltage;
break;
}
}
if (isnan(temperature) || isnan(humidity)) {
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT "Invalid reading"));
return false;
}
if (humidity > 100) { humidity = 100.0f; }
if (humidity < 0) { humidity = 0.1f; }
Dht[sensor].h = ConvertHumidity(humidity);
Dht[sensor].t = ConvertTemp(temperature);
Dht[sensor].lastresult = 0;
return true;
}
/********************************************************************************************/
bool DhtPinState() {
if (((XdrvMailbox.index >= AGPIO(GPIO_DHT11)) && (XdrvMailbox.index <= AGPIO(GPIO_SI7021))) ||
(XdrvMailbox.index == AGPIO(GPIO_MS01))) {
if (dht_sensors < DHT_MAX_SENSORS) {
Dht[dht_sensors].pin = XdrvMailbox.payload;
Dht[dht_sensors].type = BGPIO(XdrvMailbox.index);
dht_sensors++;
XdrvMailbox.index = AGPIO(GPIO_DHT11);
} else {
XdrvMailbox.index = 0;
}
return true;
}
return false;
}
void DhtInit(void) {
if (dht_sensors) {
if (PinUsed(GPIO_DHT11_OUT)) {
dht_pin_out = Pin(GPIO_DHT11_OUT);
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);
Dht[i].lastresult = DHT_MAX_RETRY; // Start with NAN
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);
}
}
dht_maxcycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for reading pulses from DHT sensor.
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT "(v6) " D_SENSORS_FOUND " %d"), dht_sensors);
} else {
dht_active = false;
}
}
void DhtEverySecond(void) {
if (!(TasmotaGlobal.uptime %4)) { // Every 4 seconds
for (uint32_t sensor = 0; sensor < dht_sensors; sensor++) {
// DHT11 and AM2301 25mS per sensor, SI7021 5mS per sensor
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;
}
}
}
}
}
void DhtShow(bool json) {
for (uint32_t i = 0; i < dht_sensors; i++) {
if (GPIO_MS01 == Dht[i].type) {
if (json) {
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_HUMIDITY "\":%*_f,\"Raw\":%d}"),
Dht[i].stype, Settings->flag2.humidity_resolution, &Dht[i].h, Dht[i].raw);
#ifdef USE_WEBSERVER
} else {
char parameter[FLOATSZ];
dtostrfd(Dht[i].h, Settings->flag2.humidity_resolution, parameter);
WSContentSend_PD(HTTP_SNS_HUM, Dht[i].stype, parameter);
#endif // USE_WEBSERVER
}
} else {
TempHumDewShow(json, ((0 == TasmotaGlobal.tele_period) && (0 == i)), Dht[i].stype, Dht[i].t, Dht[i].h);
}
}
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xsns06(uint8_t function) {
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

View File

@ -280,7 +280,7 @@ a_features = [[
"USE_HDC2010","USE_LSC_MCSL","USE_SONOFF_SPM","USE_SHIFT595",
"USE_SDM230","USE_CM110x","USE_BL6523","USE_ADE7880",
"USE_PCF85363","USE_DS3502","USE_IMPROV","USE_FLOWRATEMETER",
"USE_BP5758D","USE_HYT","USE_SM2335",""
"USE_BP5758D","USE_HYT","USE_SM2335","USE_DISPLAY_TM1621_SONOFF"
],[
"","","","",
"","","","",
@ -317,7 +317,7 @@ else:
obj = json.load(fp)
def StartDecode():
print ("\n*** decode-status.py v11.1.0.3 by Theo Arends and Jacek Ziolkowski ***")
print ("\n*** decode-status.py v12.0.2.3 by Theo Arends and Jacek Ziolkowski ***")
# print("Decoding\n{}".format(obj))