From 782b10128bdfc6f37a5eaa0deceb69cfd5d5fc2c Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sun, 11 Dec 2022 14:46:46 +0100 Subject: [PATCH 1/6] Berry autoconf remove crlf --- .../src/embedded/autoconf_module.be | 2 +- .../src/solidify/solidified_autoconf_module.h | 91 ++++++++++--------- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/lib/libesp32/berry_tasmota/src/embedded/autoconf_module.be b/lib/libesp32/berry_tasmota/src/embedded/autoconf_module.be index 3e279cf71..ded7d470c 100644 --- a/lib/libesp32/berry_tasmota/src/embedded/autoconf_module.be +++ b/lib/libesp32/berry_tasmota/src/embedded/autoconf_module.be @@ -316,7 +316,7 @@ autoconf_module.init = def (m) var line = f.readline() # read each line, can contain a terminal '\n', empty if end of file if size(line) == 0 break end # end of file - if line[-1] == "\n" line = line[0..-2] end # remove any trailing '\n' + if line[-1] == "\n" || line[-1] == "\r" line = line[0..-2] end # remove any trailing '\n' or '\r' if size(line) > 0 tasmota.cmd(line) # run the command end diff --git a/lib/libesp32/berry_tasmota/src/solidify/solidified_autoconf_module.h b/lib/libesp32/berry_tasmota/src/solidify/solidified_autoconf_module.h index bbe8644cc..5bca2124f 100644 --- a/lib/libesp32/berry_tasmota/src/solidify/solidified_autoconf_module.h +++ b/lib/libesp32/berry_tasmota/src/solidify/solidified_autoconf_module.h @@ -362,31 +362,32 @@ be_local_closure(Autoconf_run_bat, /* name */ 0, /* has sup protos */ NULL, /* no sub protos */ 1, /* has constants */ - ( &(const bvalue[10]) { /* constants */ + ( &(const bvalue[11]) { /* constants */ /* K0 */ be_nested_str(string), /* K1 */ be_nested_str(r), /* K2 */ be_nested_str(readline), /* K3 */ be_const_int(0), /* K4 */ be_nested_str(_X0A), - /* K5 */ be_nested_str(tasmota), - /* K6 */ be_nested_str(cmd), - /* K7 */ be_nested_str(close), - /* K8 */ be_nested_str(format), - /* K9 */ be_nested_str(CFG_X3A_X20could_X20not_X20run_X20_X25s_X20_X28_X25s_X20_X2D_X20_X25s_X29), + /* K5 */ be_nested_str(_X0D), + /* K6 */ be_nested_str(tasmota), + /* K7 */ be_nested_str(cmd), + /* K8 */ be_nested_str(close), + /* K9 */ be_nested_str(format), + /* K10 */ be_nested_str(CFG_X3A_X20could_X20not_X20run_X20_X25s_X20_X28_X25s_X20_X2D_X20_X25s_X29), }), &be_const_str_run_bat, &be_const_str_solidified, - ( &(const binstruction[54]) { /* code */ + ( &(const binstruction[58]) { /* code */ 0xA40A0000, // 0000 IMPORT R2 K0 0x4C0C0000, // 0001 LDNIL R3 - 0xA8020023, // 0002 EXBLK 0 #0027 + 0xA8020027, // 0002 EXBLK 0 #002B 0x60100011, // 0003 GETGBL R4 G17 0x5C140200, // 0004 MOVE R5 R1 0x58180001, // 0005 LDCONST R6 K1 0x7C100400, // 0006 CALL R4 2 0x5C0C0800, // 0007 MOVE R3 R4 0x50100200, // 0008 LDBOOL R4 1 0 - 0x78120018, // 0009 JMPF R4 #0023 + 0x7812001C, // 0009 JMPF R4 #0027 0x8C100702, // 000A GETMET R4 R3 K2 0x7C100200, // 000B CALL R4 1 0x6014000C, // 000C GETGBL R5 G12 @@ -394,43 +395,47 @@ be_local_closure(Autoconf_run_bat, /* name */ 0x7C140200, // 000E CALL R5 1 0x1C140B03, // 000F EQ R5 R5 K3 0x78160000, // 0010 JMPF R5 #0012 - 0x70020010, // 0011 JMP #0023 + 0x70020014, // 0011 JMP #0027 0x5415FFFE, // 0012 LDINT R5 -1 0x94140805, // 0013 GETIDX R5 R4 R5 0x1C140B04, // 0014 EQ R5 R5 K4 - 0x78160002, // 0015 JMPF R5 #0019 - 0x5415FFFD, // 0016 LDINT R5 -2 - 0x40160605, // 0017 CONNECT R5 K3 R5 - 0x94100805, // 0018 GETIDX R4 R4 R5 - 0x6014000C, // 0019 GETGBL R5 G12 - 0x5C180800, // 001A MOVE R6 R4 - 0x7C140200, // 001B CALL R5 1 - 0x24140B03, // 001C GT R5 R5 K3 - 0x78160003, // 001D JMPF R5 #0022 - 0xB8160A00, // 001E GETNGBL R5 K5 - 0x8C140B06, // 001F GETMET R5 R5 K6 - 0x5C1C0800, // 0020 MOVE R7 R4 - 0x7C140400, // 0021 CALL R5 2 - 0x7001FFE4, // 0022 JMP #0008 - 0x8C100707, // 0023 GETMET R4 R3 K7 - 0x7C100200, // 0024 CALL R4 1 - 0xA8040001, // 0025 EXBLK 1 1 - 0x7002000D, // 0026 JMP #0035 - 0xAC100002, // 0027 CATCH R4 0 2 - 0x7002000A, // 0028 JMP #0034 - 0x60180001, // 0029 GETGBL R6 G1 - 0x8C1C0508, // 002A GETMET R7 R2 K8 - 0x58240009, // 002B LDCONST R9 K9 - 0x5C280200, // 002C MOVE R10 R1 - 0x5C2C0800, // 002D MOVE R11 R4 - 0x5C300A00, // 002E MOVE R12 R5 - 0x7C1C0A00, // 002F CALL R7 5 - 0x7C180200, // 0030 CALL R6 1 - 0x8C180707, // 0031 GETMET R6 R3 K7 - 0x7C180200, // 0032 CALL R6 1 - 0x70020000, // 0033 JMP #0035 - 0xB0080000, // 0034 RAISE 2 R0 R0 - 0x80000000, // 0035 RET 0 + 0x74160003, // 0015 JMPT R5 #001A + 0x5415FFFE, // 0016 LDINT R5 -1 + 0x94140805, // 0017 GETIDX R5 R4 R5 + 0x1C140B05, // 0018 EQ R5 R5 K5 + 0x78160002, // 0019 JMPF R5 #001D + 0x5415FFFD, // 001A LDINT R5 -2 + 0x40160605, // 001B CONNECT R5 K3 R5 + 0x94100805, // 001C GETIDX R4 R4 R5 + 0x6014000C, // 001D GETGBL R5 G12 + 0x5C180800, // 001E MOVE R6 R4 + 0x7C140200, // 001F CALL R5 1 + 0x24140B03, // 0020 GT R5 R5 K3 + 0x78160003, // 0021 JMPF R5 #0026 + 0xB8160C00, // 0022 GETNGBL R5 K6 + 0x8C140B07, // 0023 GETMET R5 R5 K7 + 0x5C1C0800, // 0024 MOVE R7 R4 + 0x7C140400, // 0025 CALL R5 2 + 0x7001FFE0, // 0026 JMP #0008 + 0x8C100708, // 0027 GETMET R4 R3 K8 + 0x7C100200, // 0028 CALL R4 1 + 0xA8040001, // 0029 EXBLK 1 1 + 0x7002000D, // 002A JMP #0039 + 0xAC100002, // 002B CATCH R4 0 2 + 0x7002000A, // 002C JMP #0038 + 0x60180001, // 002D GETGBL R6 G1 + 0x8C1C0509, // 002E GETMET R7 R2 K9 + 0x5824000A, // 002F LDCONST R9 K10 + 0x5C280200, // 0030 MOVE R10 R1 + 0x5C2C0800, // 0031 MOVE R11 R4 + 0x5C300A00, // 0032 MOVE R12 R5 + 0x7C1C0A00, // 0033 CALL R7 5 + 0x7C180200, // 0034 CALL R6 1 + 0x8C180708, // 0035 GETMET R6 R3 K8 + 0x7C180200, // 0036 CALL R6 1 + 0x70020000, // 0037 JMP #0039 + 0xB0080000, // 0038 RAISE 2 R0 R0 + 0x80000000, // 0039 RET 0 }) ) ); From c9b838d3dcff2feb19b6848917a98c1be105ab1a Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sun, 11 Dec 2022 15:09:26 +0100 Subject: [PATCH 2/6] Remove all trailing \r and \n --- .../src/embedded/autoconf_module.be | 2 +- .../src/solidify/solidified_autoconf_module.h | 94 ++++++++++--------- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/lib/libesp32/berry_tasmota/src/embedded/autoconf_module.be b/lib/libesp32/berry_tasmota/src/embedded/autoconf_module.be index ded7d470c..80c3579ba 100644 --- a/lib/libesp32/berry_tasmota/src/embedded/autoconf_module.be +++ b/lib/libesp32/berry_tasmota/src/embedded/autoconf_module.be @@ -316,7 +316,7 @@ autoconf_module.init = def (m) var line = f.readline() # read each line, can contain a terminal '\n', empty if end of file if size(line) == 0 break end # end of file - if line[-1] == "\n" || line[-1] == "\r" line = line[0..-2] end # remove any trailing '\n' or '\r' + while (size(line) > 0 && (line[-1] == "\n" || line[-1] == "\r")) line = line[0..-2] end # remove any trailing '\n' or '\r' if size(line) > 0 tasmota.cmd(line) # run the command end diff --git a/lib/libesp32/berry_tasmota/src/solidify/solidified_autoconf_module.h b/lib/libesp32/berry_tasmota/src/solidify/solidified_autoconf_module.h index 5bca2124f..16d3089dd 100644 --- a/lib/libesp32/berry_tasmota/src/solidify/solidified_autoconf_module.h +++ b/lib/libesp32/berry_tasmota/src/solidify/solidified_autoconf_module.h @@ -377,17 +377,17 @@ be_local_closure(Autoconf_run_bat, /* name */ }), &be_const_str_run_bat, &be_const_str_solidified, - ( &(const binstruction[58]) { /* code */ + ( &(const binstruction[64]) { /* code */ 0xA40A0000, // 0000 IMPORT R2 K0 0x4C0C0000, // 0001 LDNIL R3 - 0xA8020027, // 0002 EXBLK 0 #002B + 0xA802002D, // 0002 EXBLK 0 #0031 0x60100011, // 0003 GETGBL R4 G17 0x5C140200, // 0004 MOVE R5 R1 0x58180001, // 0005 LDCONST R6 K1 0x7C100400, // 0006 CALL R4 2 0x5C0C0800, // 0007 MOVE R3 R4 0x50100200, // 0008 LDBOOL R4 1 0 - 0x7812001C, // 0009 JMPF R4 #0027 + 0x78120022, // 0009 JMPF R4 #002D 0x8C100702, // 000A GETMET R4 R3 K2 0x7C100200, // 000B CALL R4 1 0x6014000C, // 000C GETGBL R5 G12 @@ -395,47 +395,53 @@ be_local_closure(Autoconf_run_bat, /* name */ 0x7C140200, // 000E CALL R5 1 0x1C140B03, // 000F EQ R5 R5 K3 0x78160000, // 0010 JMPF R5 #0012 - 0x70020014, // 0011 JMP #0027 - 0x5415FFFE, // 0012 LDINT R5 -1 - 0x94140805, // 0013 GETIDX R5 R4 R5 - 0x1C140B04, // 0014 EQ R5 R5 K4 - 0x74160003, // 0015 JMPT R5 #001A - 0x5415FFFE, // 0016 LDINT R5 -1 - 0x94140805, // 0017 GETIDX R5 R4 R5 - 0x1C140B05, // 0018 EQ R5 R5 K5 - 0x78160002, // 0019 JMPF R5 #001D - 0x5415FFFD, // 001A LDINT R5 -2 - 0x40160605, // 001B CONNECT R5 K3 R5 - 0x94100805, // 001C GETIDX R4 R4 R5 - 0x6014000C, // 001D GETGBL R5 G12 - 0x5C180800, // 001E MOVE R6 R4 - 0x7C140200, // 001F CALL R5 1 - 0x24140B03, // 0020 GT R5 R5 K3 - 0x78160003, // 0021 JMPF R5 #0026 - 0xB8160C00, // 0022 GETNGBL R5 K6 - 0x8C140B07, // 0023 GETMET R5 R5 K7 - 0x5C1C0800, // 0024 MOVE R7 R4 - 0x7C140400, // 0025 CALL R5 2 - 0x7001FFE0, // 0026 JMP #0008 - 0x8C100708, // 0027 GETMET R4 R3 K8 - 0x7C100200, // 0028 CALL R4 1 - 0xA8040001, // 0029 EXBLK 1 1 - 0x7002000D, // 002A JMP #0039 - 0xAC100002, // 002B CATCH R4 0 2 - 0x7002000A, // 002C JMP #0038 - 0x60180001, // 002D GETGBL R6 G1 - 0x8C1C0509, // 002E GETMET R7 R2 K9 - 0x5824000A, // 002F LDCONST R9 K10 - 0x5C280200, // 0030 MOVE R10 R1 - 0x5C2C0800, // 0031 MOVE R11 R4 - 0x5C300A00, // 0032 MOVE R12 R5 - 0x7C1C0A00, // 0033 CALL R7 5 - 0x7C180200, // 0034 CALL R6 1 - 0x8C180708, // 0035 GETMET R6 R3 K8 - 0x7C180200, // 0036 CALL R6 1 - 0x70020000, // 0037 JMP #0039 - 0xB0080000, // 0038 RAISE 2 R0 R0 - 0x80000000, // 0039 RET 0 + 0x7002001A, // 0011 JMP #002D + 0x6014000C, // 0012 GETGBL R5 G12 + 0x5C180800, // 0013 MOVE R6 R4 + 0x7C140200, // 0014 CALL R5 1 + 0x24140B03, // 0015 GT R5 R5 K3 + 0x7816000B, // 0016 JMPF R5 #0023 + 0x5415FFFE, // 0017 LDINT R5 -1 + 0x94140805, // 0018 GETIDX R5 R4 R5 + 0x1C140B04, // 0019 EQ R5 R5 K4 + 0x74160003, // 001A JMPT R5 #001F + 0x5415FFFE, // 001B LDINT R5 -1 + 0x94140805, // 001C GETIDX R5 R4 R5 + 0x1C140B05, // 001D EQ R5 R5 K5 + 0x78160003, // 001E JMPF R5 #0023 + 0x5415FFFD, // 001F LDINT R5 -2 + 0x40160605, // 0020 CONNECT R5 K3 R5 + 0x94100805, // 0021 GETIDX R4 R4 R5 + 0x7001FFEE, // 0022 JMP #0012 + 0x6014000C, // 0023 GETGBL R5 G12 + 0x5C180800, // 0024 MOVE R6 R4 + 0x7C140200, // 0025 CALL R5 1 + 0x24140B03, // 0026 GT R5 R5 K3 + 0x78160003, // 0027 JMPF R5 #002C + 0xB8160C00, // 0028 GETNGBL R5 K6 + 0x8C140B07, // 0029 GETMET R5 R5 K7 + 0x5C1C0800, // 002A MOVE R7 R4 + 0x7C140400, // 002B CALL R5 2 + 0x7001FFDA, // 002C JMP #0008 + 0x8C100708, // 002D GETMET R4 R3 K8 + 0x7C100200, // 002E CALL R4 1 + 0xA8040001, // 002F EXBLK 1 1 + 0x7002000D, // 0030 JMP #003F + 0xAC100002, // 0031 CATCH R4 0 2 + 0x7002000A, // 0032 JMP #003E + 0x60180001, // 0033 GETGBL R6 G1 + 0x8C1C0509, // 0034 GETMET R7 R2 K9 + 0x5824000A, // 0035 LDCONST R9 K10 + 0x5C280200, // 0036 MOVE R10 R1 + 0x5C2C0800, // 0037 MOVE R11 R4 + 0x5C300A00, // 0038 MOVE R12 R5 + 0x7C1C0A00, // 0039 CALL R7 5 + 0x7C180200, // 003A CALL R6 1 + 0x8C180708, // 003B GETMET R6 R3 K8 + 0x7C180200, // 003C CALL R6 1 + 0x70020000, // 003D JMP #003F + 0xB0080000, // 003E RAISE 2 R0 R0 + 0x80000000, // 003F RET 0 }) ) ); From 1bdb542e504d345226a9bd5059db2608f7d8eb00 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sun, 11 Dec 2022 16:24:14 +0100 Subject: [PATCH 3/6] Fix IPv4 DNS resolution when IPv6 is enabled --- tasmota/tasmota_support/support_wifi.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tasmota/tasmota_support/support_wifi.ino b/tasmota/tasmota_support/support_wifi.ino index b72c309cd..81e4de039 100644 --- a/tasmota/tasmota_support/support_wifi.ino +++ b/tasmota/tasmota_support/support_wifi.ino @@ -893,6 +893,9 @@ bool WifiHostByName(const char* aHostname, IPAddress& aResult) { } #else // DnsClient can't do one-shot mDNS queries so use WiFi.hostByName() for *.local +#ifdef USE_IPV6 + aResult.setV4(); // force IPv4 result for now, until DNS is updated to IPv6 +#endif size_t hostname_len = strlen(aHostname); if (strstr_P(aHostname, PSTR(".local")) == &aHostname[hostname_len] - 6) { if (WiFi.hostByName(aHostname, aResult)) { From bde58105d03c50c9072a5a6d96c489b6891752ab Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sun, 11 Dec 2022 16:31:14 +0100 Subject: [PATCH 4/6] More universal way to force IPv4 --- tasmota/tasmota_support/support_wifi.ino | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tasmota/tasmota_support/support_wifi.ino b/tasmota/tasmota_support/support_wifi.ino index 81e4de039..5088ab81b 100644 --- a/tasmota/tasmota_support/support_wifi.ino +++ b/tasmota/tasmota_support/support_wifi.ino @@ -893,9 +893,7 @@ bool WifiHostByName(const char* aHostname, IPAddress& aResult) { } #else // DnsClient can't do one-shot mDNS queries so use WiFi.hostByName() for *.local -#ifdef USE_IPV6 - aResult.setV4(); // force IPv4 result for now, until DNS is updated to IPv6 -#endif + aResult = (uint32_t) 0x00000000L; // indirectly force to be IPv4, since the client touches the binary format later size_t hostname_len = strlen(aHostname); if (strstr_P(aHostname, PSTR(".local")) == &aHostname[hostname_len] - 6) { if (WiFi.hostByName(aHostname, aResult)) { From b78886b0f1ffafc7d25cfeeb4a1d9441f99617b1 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 11 Dec 2022 16:44:36 +0100 Subject: [PATCH 5/6] Add TasmotaModbus hardware fallback to GPIO13/15 --- lib/default/TasmotaSerial-3.6.0/src/TasmotaSerial.cpp | 1 + lib/lib_basic/TasmotaModbus-3.6.0/src/TasmotaModbus.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/default/TasmotaSerial-3.6.0/src/TasmotaSerial.cpp b/lib/default/TasmotaSerial-3.6.0/src/TasmotaSerial.cpp index 7e960b05c..f334073a5 100644 --- a/lib/default/TasmotaSerial-3.6.0/src/TasmotaSerial.cpp +++ b/lib/default/TasmotaSerial-3.6.0/src/TasmotaSerial.cpp @@ -463,6 +463,7 @@ size_t TasmotaSerial::write(uint8_t b) { size = 1; } if (m_tx_enable_valid) { + delay(1); digitalWrite(m_tx_enable_pin, LOW); } return size; diff --git a/lib/lib_basic/TasmotaModbus-3.6.0/src/TasmotaModbus.cpp b/lib/lib_basic/TasmotaModbus-3.6.0/src/TasmotaModbus.cpp index 25ea7caaf..4860e6b84 100644 --- a/lib/lib_basic/TasmotaModbus-3.6.0/src/TasmotaModbus.cpp +++ b/lib/lib_basic/TasmotaModbus-3.6.0/src/TasmotaModbus.cpp @@ -27,7 +27,7 @@ enum LoggingLevels {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_D //#define TASMOTAMODBUSDEBUG -TasmotaModbus::TasmotaModbus(int receive_pin, int transmit_pin, int tx_enable_pin) : TasmotaSerial(receive_pin, transmit_pin, 1) +TasmotaModbus::TasmotaModbus(int receive_pin, int transmit_pin, int tx_enable_pin) : TasmotaSerial(receive_pin, transmit_pin, 2) { setTransmitEnablePin(tx_enable_pin); mb_address = 0; From 630987302ab66c27c41e44f67defc92515cf1986 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 12 Dec 2022 10:57:21 +0100 Subject: [PATCH 6/6] Fix Analog MQ exception Fix Analog MQ exception (#17271) --- CHANGELOG.md | 1 + RELEASENOTES.md | 1 + .../tasmota_xsns_sensor/xsns_02_analog.ino | 81 ++++++++++--------- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 821f8dfde..089d76cf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file. - TasmotaSerial ``read(buffer, size)`` regression from v9.3.0 - RCSwitch exception 0/6 on some protocols (#17285) - ESP32 exception 28 when RtcNtpServer is enabled on restart (#17338) +- Analog MQ exception 28 on restart (#17271) ### Removed diff --git a/RELEASENOTES.md b/RELEASENOTES.md index f464887a9..8a0d47cee 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -164,6 +164,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo - ModbusBridge buffer overflow [#16979](https://github.com/arendst/Tasmota/issues/16979) - ModbusBridge baudrates over 76500 baud [#17106](https://github.com/arendst/Tasmota/issues/17106) - SenseAir S8 module detection [#17033](https://github.com/arendst/Tasmota/issues/17033) +- Analog MQ exception 28 on restart [#17271](https://github.com/arendst/Tasmota/issues/17271) - RCSwitch exception 0/6 on some protocols [#17285](https://github.com/arendst/Tasmota/issues/17285) - ESP32 exception 28 when RtcNtpServer is enabled on restart [#17338](https://github.com/arendst/Tasmota/issues/17338) diff --git a/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino b/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino index 8c6464aea..5abb6b56a 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino @@ -386,46 +386,55 @@ uint16_t AdcGetLux(uint32_t idx) { } void AddSampleMq(uint32_t idx){ - AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "Adding sample for mq-sensor")); +// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Adding sample for mq-sensor")); + int _adc = AdcRead(Adc[idx].pin, 2); // init af array at same value - if (Adc[idx].indexOfPointer==-1) - { - AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "Init samples for mq-sensor")); - for (int i = 0; i < ANALOG_MQ_SAMPLES; i ++) + if (Adc[idx].indexOfPointer==-1) { + +// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Init samples for mq-sensor")); + + for (int i = 0; i < ANALOG_MQ_SAMPLES; i ++) { Adc[idx].mq_samples[i] = _adc; - } - else + } + } else { Adc[idx].mq_samples[Adc[idx].indexOfPointer] = _adc; + } Adc[idx].indexOfPointer++; - if (Adc[idx].indexOfPointer==ANALOG_MQ_SAMPLES) + if (Adc[idx].indexOfPointer==ANALOG_MQ_SAMPLES) { Adc[idx].indexOfPointer=0; + } } float AdcGetMq(uint32_t idx) { - AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "Getting value for mq-sensor")); +// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Getting value for mq-sensor")); + float avg = 0.0; - float _RL = 10; //Value in KiloOhms - float _R0 = 10; - for (int i = 0; i < ANALOG_MQ_SAMPLES; i ++) + for (int i = 0; i < ANALOG_MQ_SAMPLES; i ++) { avg += Adc[idx].mq_samples[i]; + } float voltage = (avg / ANALOG_MQ_SAMPLES) * ANALOG_V33 / ANALOG_RANGE; - float _RS_Calc = ((ANALOG_V33 * _RL) / voltage) -_RL; //Get value of RS in a gas - if (_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted. - float _ratio = _RS_Calc / _R0; // Get ratio RS_gas/RS_air - float ppm= Adc[idx].param2/ANALOG_MQ_DECIMAL_MULTIPLIER*FastPrecisePow(_ratio, Adc[idx].param3/ANALOG_MQ_DECIMAL_MULTIPLIER); // <- Source excel analisis https://github.com/miguel5612/MQSensorsLib_Docs/tree/master/Internal_design_documents - if(ppm < 0) ppm = 0; //No negative values accepted or upper datasheet recomendation. - char ppm_chr[6]; - dtostrfd(ppm, 2, ppm_chr); - AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "Ppm read. ADC-RAW: %2_f, ppm: %s,"), &voltage, ppm_chr); + float _RL = 10; // Value in KiloOhms + float _RS_Calc = ((ANALOG_V33 * _RL) / voltage) -_RL; // Get value of RS in a gas + if (_RS_Calc < 0) { + _RS_Calc = 0; // No negative values accepted. + } + + float _R0 = 10; + float _ratio = _RS_Calc / _R0; // Get ratio RS_gas/RS_air + float ppm = Adc[idx].param2 / ANALOG_MQ_DECIMAL_MULTIPLIER * FastPrecisePow(_ratio, Adc[idx].param3 / ANALOG_MQ_DECIMAL_MULTIPLIER); // Source excel analisis https://github.com/miguel5612/MQSensorsLib_Docs/tree/master/Internal_design_documents + if (ppm < 0) { ppm = 0; } // No negative values accepted or upper datasheet recomendation. + if (ppm > 100000) { ppm = 100000; } + +// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Ppm read. ADC-RAW: %2_f, ppm: %2_f"), &voltage, &ppm); + return ppm; } float AdcGetPh(uint32_t idx) { int adc = AdcRead(Adc[idx].pin, 2); - float y1 = (float)Adc[idx].param1 / ANALOG_PH_DECIMAL_MULTIPLIER; int32_t x1 = Adc[idx].param2; float y2 = (float)Adc[idx].param3 / ANALOG_PH_DECIMAL_MULTIPLIER; @@ -434,12 +443,7 @@ float AdcGetPh(uint32_t idx) { float m = (y2 - y1) / (float)(x2 - x1); float ph = m * (float)(adc - x1) + y1; - - char phLow_chr[6]; - char phHigh_chr[6]; - dtostrfd(y1, 2, phLow_chr); - dtostrfd(y2, 2, phHigh_chr); - AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "Analog pH read. ADC-RAW: %d, cal-low(pH=ADC): %s=%d, cal-high(pH=ADC): %s=%d"), adc, phLow_chr, x1, phHigh_chr,x2); +// AddLog(LOG_LEVEL_DEBUG, PSTR("ADC: Analog pH read. ADC-RAW: %d, cal-low(pH=ADC): %2_f = %d, cal-high(pH=ADC): %2_f = %d"), adc, &y1, x1, &y2, x2); return ph; } @@ -666,10 +670,9 @@ void AdcShow(bool json) { } case ADC_PH: { float ph = AdcGetPh(idx); - char ph_chr[6]; + char ph_chr[FLOATSZ]; dtostrfd(ph, 2, ph_chr); - if (json) { AdcShowContinuation(&jsonflg); ResponseAppend_P(PSTR("\"pH%d\":%s"), idx + offset, ph_chr); @@ -682,11 +685,11 @@ void AdcShow(bool json) { } case ADC_MQ: { float mq = AdcGetMq(idx); - char mq_chr[6]; + char mq_chr[FLOATSZ]; dtostrfd(mq, 2, mq_chr); float mqnumber =Adc[idx].param1; - char mqnumber_chr[6]; + char mqnumber_chr[FLOATSZ]; dtostrfd(mqnumber, 0, mqnumber_chr); if (json) { @@ -754,8 +757,8 @@ void CmndAdcParam(void) { Adc[idx].param2 = strtol(ArgV(argument, 3), nullptr, 10); Adc[idx].param3 = phHigh * ANALOG_PH_DECIMAL_MULTIPLIER; Adc[idx].param4 = strtol(ArgV(argument, 5), nullptr, 10); - AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION "Analog pH probe calibrated. cal-low(pH=ADC) %2_f=%d, cal-high(pH=ADC) %2_f=%d"), - &phLow, Adc[idx].param2, &phHigh, Adc[idx].param4); + +// AddLog(LOG_LEVEL_INFO, PSTR("ADC: Analog pH probe calibrated. cal-low(pH=ADC) %2_f = %d, cal-high(pH=ADC) %2_f = %d"), &phLow, Adc[idx].param2, &phHigh, Adc[idx].param4); } if (ADC_CT_POWER == XdrvMailbox.payload) { if (((1 == Adc[idx].param1) & CT_FLAG_ENERGY_RESET) > 0) { @@ -796,11 +799,11 @@ void CmndAdcParam(void) { ratioMQCleanAir=15; } } - Adc[idx].param2 = (int)(a * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression - Adc[idx].param3 = (int)(b * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression - Adc[idx].param4 = (int)(ratioMQCleanAir * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression - AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION "Analog MQ reset: mq%d, a=%2_f, b=%2_f, ratioMQCleanAir=%2_f"), - Adc[idx].param1, &a, &b, &ratioMQCleanAir); + Adc[idx].param2 = (int)(a * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression + Adc[idx].param3 = (int)(b * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression + Adc[idx].param4 = (int)(ratioMQCleanAir * ANALOG_MQ_DECIMAL_MULTIPLIER); // Exponential regression + +// AddLog(LOG_LEVEL_INFO, PSTR("ADC: MQ reset mq%d, a = %2_f, b = %2_f, ratioMQCleanAir = %2_f"), Adc[idx].param1, &a, &b, &ratioMQCleanAir); } } else { // Set default values based on current adc type // AdcParam 2 @@ -829,7 +832,7 @@ void CmndAdcParam(void) { if (value % 10) { break; } value /= 10; } - char param3[33]; + char param3[FLOATSZ]; dtostrfd(((double)Adc[idx].param3)/10000, precision, param3); ResponseAppend_P(PSTR(",%s,%d"), param3, Adc[idx].param4); }