From fadc2cee534b0a898cea5d6b4194d2f58c639775 Mon Sep 17 00:00:00 2001 From: StefanStrobel <> Date: Mon, 12 Aug 2019 17:41:49 +0000 Subject: [PATCH] 98_Arducounter.pm: more bugfixes in the firmware for ESP8266 based counters git-svn-id: https://svn.fhem.de/fhem/trunk@19988 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/firmware/ArduCounter.hex | 26 +++++++------- fhem/contrib/arduino/ArduCounter3.30.ino | 44 +++++++++++++----------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/fhem/FHEM/firmware/ArduCounter.hex b/fhem/FHEM/firmware/ArduCounter.hex index f73af64ce..ee9c5f0b2 100755 --- a/fhem/FHEM/firmware/ArduCounter.hex +++ b/fhem/FHEM/firmware/ArduCounter.hex @@ -13,10 +13,10 @@ :1000C0006F6E66696720696E20454550524F4D003E :1000D0004D206E6F20636F6E66696720696E2045E4 :1000E0004550524F4D004572726F723A2000417573 -:1000F0006720313020323031392031343A31313AD1 -:1001000030350020636F6D70696C656420004E416E +:1000F0006720313220323031392031393A33343AC5 +:1001000030370020636F6D70696C656420004E416C :100110004E4F00206F6E200041726475436F756E04 -:100120007465722056332E333200200020002000E8 +:100120007465722056332E333300200020002000E7 :10013000490020005400206D696E20002070756C0D :100140006C757000202D002066616C6C696E670014 :1001500020726973696E6700500040002F007300C1 @@ -162,8 +162,8 @@ :100A1000C080D180D88ECF8A7111C7C0E98DFA8D80 :100A2000EC5DFA4F80819181A281B38169017A01E5 :100A3000C81AD90AEA0AFB0AD701C601C0900C01FC -:100A4000D0900D01E0900E01F0900F018C159D05E6 -:100A5000AE05BF0508F443C08091060190910701DF +:100A4000D0900D01E0900E01F0900F01C816D9066C +:100A5000EA06FB0608F043C0809106019091070169 :100A6000A0E0B0E088169906AA06BB06D8F08091EF :100A7000080190910901A0910A01B0910B01841520 :100A80009505A605B70570F48091100190911101AC @@ -172,8 +172,8 @@ :100AB000318342835383E98DFA8DEA5FFB4F2083B4 :100AC000318342835383789429013A01C984DA84BB :100AD000EB84FC844C185D086E087F0833C0C0901E -:100AE0001001D0901101E0901201F09013018C15CB -:100AF0009D05AE05BF0508F43EC1809106019091A9 +:100AE0001001D0901101E0901201F0901301C8168E +:100AF000D906EA06FB0608F03EC1809106019091F6 :100B00000701A0E0B0E088169906AA06BB0608F423 :100B100032C18091080190910901A0910A01B09120 :100B20000B0184159505A605B70508F024C1F894B6 @@ -696,10 +696,10 @@ :102B7000190680919706909198060E94E90D10928F :102B8000A00410929F0410929E04E8E1F6E01192D6 :102B9000119216E0E832F107D1F7809195069091F5 -:102BA000960697FD7BC10E9444106B017C018091C9 -:102BB000A1049091A204A091A304B091A404B70130 -:102BC000A601481B590B6A0B7B0B20910001309129 -:102BD0000101C901A0E0B0E084179507A607B70777 +:102BA000960697FD7BC10E9444106B017C01209129 +:102BB0000001309101018091A1049091A204A091A3 +:102BC000A304B091A404B701A601481B590B6A0BDA +:102BD0007B0BC901A0E0B0E084179507A607B707F3 :102BE00008F05CC190911706923001F128F4992306 :102BF00049F0913069F048C1943051F108F19530B5 :102C0000A9F142C160E082E00E94B40F81E00BC0F4 @@ -747,7 +747,7 @@ :102EA0008A3060F50E94441000916C0510916D0508 :102EB00020916E0530916F054B015C01801A910ADB :102EC000A20AB30AC0901001D0901101E090120143 -:102ED000F09013018C149D04AE04BF0408F0D6C01A +:102ED000F0901301C814D904EA04FB0408F4D6C026 :102EE000C0900C01D0900D01E0900E01F0900F0108 :102EF000E3E7F6E0A4E2B5E040E0C1C053E7A52E69 :102F000056E0B52E62E1E62E65E0F62E74E4C72E9B @@ -772,7 +772,7 @@ :10303000BA030F5F1F4FFFEFEF1AFF0A22E0C20E25 :10304000D11C0231110509F067CF2CCF0D911D91D4 :103050002D913C9113974B015C01801A910AA20AB1 -:10306000B30A8C149D04AE04BF0408F041E014962A +:10306000B30AC814D904EA04FB0408F441E0149636 :1030700027E9A22E26E0B22EAE16BF0629F0219136 :10308000319137FDF4CFE2CF442361F10E94441027 :103090004B015C0183E7E82E86E0F82E00E010E0AB diff --git a/fhem/contrib/arduino/ArduCounter3.30.ino b/fhem/contrib/arduino/ArduCounter3.30.ino index 0bf4680d1..f236a364d 100755 --- a/fhem/contrib/arduino/ArduCounter3.30.ino +++ b/fhem/contrib/arduino/ArduCounter3.30.ino @@ -92,6 +92,7 @@ 20.7.19 - nicer debug output for analog leves 21.7.19 - V3.30 replace delay during analog read with millis() logic, optimize waiting times for analog read 10.8.19 - V3.32 add ICACHE_RAM_ATTR for ISRs and remove remaining long casts (bug) when handling time + 12.8.19 - V3.33 fix handling of keepalive timeouts when millis wraps ToDo / Ideas: make analogInterval available in Fhem @@ -120,7 +121,7 @@ #include "pins_arduino.h" #include -const char versionStr[] PROGMEM = "ArduCounter V3.32"; +const char versionStr[] PROGMEM = "ArduCounter V3.33"; #define SERIAL_SPEED 38400 #define MAX_INPUT_NUM 8 @@ -156,6 +157,7 @@ WiFiClient Client1; // active TCP connection WiFiClient Client2; // secound TCP connection to send reject message boolean Client1Connected; // remember state of TCP connection boolean Client2Connected; // remember state of TCP connection +long rssi; // WiFi connection strength boolean tcpMode = false; uint8_t delayedTcpReports = 0; // how often did we already delay reporting because tcp disconnected @@ -338,7 +340,8 @@ uint16_t countMin = 2; // continue counting if count is less than t uint32_t lastReportCall; #ifdef ESP8266 -uint32_t expectK; +uint16_t keepAliveTimeout; +uint32_t lastKeepAlive; #endif /* index to the following arrays is the internal pin index number */ @@ -433,7 +436,7 @@ void initialize() { #endif restoreFromEEPROM(); #ifdef ESP8266 - expectK = now + 600000; // max 10 Minutes (to be checked on Fhem module side as well + lastKeepAlive = now; #endif } @@ -448,8 +451,7 @@ void initialize() { */ static void inline doCount(uint8_t pinIndex, uint8_t level, uint32_t now) { uint32_t len = now - lastChange[pinIndex]; - char act = ' '; - + char act = ' '; #ifdef pulseHistory histIndex++; if (histIndex >= MAX_HIST) histIndex = 0; @@ -780,7 +782,7 @@ void showPinCounter(short pinIndex, boolean showOnly, uint32_t now) { rejDiff = rejCount - lastRejCount[pinIndex]; if (!showOnly) { // real reporting sets the interval borders new - if((now - lastReport[pinIndex]) >= intervalMax) { + if((now - lastReport[pinIndex]) > intervalMax) { // intervalMax is over if ((countDiff >= countMin) && (timeDiff > intervalSml) && (intervalMin != intervalMax)) { // normal procedure @@ -795,7 +797,7 @@ void showPinCounter(short pinIndex, boolean showOnly, uint32_t now) { interrupts(); timeDiff = now - startT; // special handling - calculation ends now } - } else if (((now - lastReport[pinIndex]) >= intervalMin) + } else if (((now - lastReport[pinIndex]) > intervalMin) && (countDiff >= countMin) && (timeDiff > intervalSml)) { // minInterval has elapsed and other conditions are ok noInterrupts(); // vars could be modified in ISR as well @@ -862,12 +864,12 @@ void showPinCounter(short pinIndex, boolean showOnly, uint32_t now) { boolean reportDue() { uint32_t now = millis(); boolean doReport = false; // check if report needs to be called - if((now - lastReportCall) >= intervalMin) // works fine when millis wraps. + if((now - lastReportCall) > intervalMin) // works fine when millis wraps. doReport = true; // intervalMin is over else for (uint8_t pinIndex=0; pinIndex < MAX_PIN; pinIndex++) if (activePin[pinIndex] >= 0) - if((now - lastReport[pinIndex]) >= intervalMax) + if((now - lastReport[pinIndex]) > intervalMax) doReport = true; // active pin has not been reported for langer than intervalMax return doReport; } @@ -878,7 +880,7 @@ void report() { uint32_t now = millis(); #ifdef ESP8266 if (tcpMode && !Client1Connected && (delayedTcpReports < 3)) { - if(delayedTcpReports == 0 || ((long)(now - (lastDelayedTcpReports + (1 * 30 * 1000))) > 0)) { + if(delayedTcpReports == 0 || ((now - lastDelayedTcpReports) > 30000)) { Serial.print(F("D report called but tcp is disconnected - delaying (")); Serial.print(delayedTcpReports); Serial.print(F(")")); @@ -1104,19 +1106,19 @@ void intervalCmd(uint16_t *values, uint8_t size) { PrintErrorMsg(); Output->println(values[0]); return; } - intervalMin = (long)values[0] * 1000; + intervalMin = (uint32_t)values[0] * 1000; if (values[1] < 1 || values[1] > 3600) { PrintErrorMsg(); Output->println(values[1]); return; } - intervalMax = (long)values[1]* 1000; + intervalMax = (uint32_t)values[1]* 1000; if (values[2] > 3600) { PrintErrorMsg(); Output->println(values[2]); return; } - intervalSml = (long)values[2] * 1000; + intervalSml = (uint32_t)values[2] * 1000; if (values[3] > 100) { PrintErrorMsg(); Output->println(values[3]); @@ -1204,9 +1206,9 @@ void keepAliveCmd(uint16_t *values, uint8_t size) { if (values[0] == 1 && size > 0 && size < 3 && Client1.connected()) { tcpMode = true; if (size == 2) { - expectK = millis() + values[1] * 2500; + keepAliveTimeout = values[1]; // timeout in seconds (on ESP side we use it times 3) } else { - expectK = millis() + 600000; // 10 Minutes if nothing sent (should not happen) + keepAliveTimeout = 200; // *3*1000 gives 10 minutes if nothing sent (should not happen) } } #endif @@ -1569,7 +1571,7 @@ void handleConnections() { uint32_t now = millis(); if (Client1Connected) { - if((long)(now - expectK) >= 0) { + if((now - lastKeepAlive) > (keepAliveTimeout *3000)) { Serial.println(F("M no keepalive from Client - disconnecting")); Client1.stop(); } @@ -1602,7 +1604,7 @@ void handleConnections() { Serial.println(F(" accepted")); Client1Connected = true; Output = &Client1; - expectK = now + 600000; // max 10 Minutes (to be checked on Fhem module side as well + lastKeepAlive = now; helloCmd(); // say hello to client } } @@ -1664,14 +1666,16 @@ void readAnalog() { short AIndex = allowedPins[analogInPin]; if (AIndex >= 0 && activePin[AIndex] >= 0) { // analog Pin active? uint32_t now = millis(); - if (now - lastAnalogRead > analogReadInterval) { // time for next analog read? + uint16_t interval2 = analogReadInterval + 2; + uint16_t interval3 = analogReadInterval + 4; + if ((now - lastAnalogRead) > analogReadInterval) { // time for next analog read? switch (analogReadState) { case 0: // initial state digitalWrite(irOutPin, LOW); // switch IR LED is off analogReadState = 1; break; case 1: // wait before measuring - if (now - lastAnalogRead < analogReadInterval + 2) + if ((now - lastAnalogRead) < interval2) return; analogReadState = 2; break; @@ -1684,7 +1688,7 @@ void readAnalog() { analogReadState = 4; break; case 4: // wait again before measuring - if (now - lastAnalogRead < analogReadInterval + 4) + if ((now - lastAnalogRead) < interval3) return; analogReadState = 5; break;