From 85ff834e875ce9ca8d3e2e72214684dada05a723 Mon Sep 17 00:00:00 2001 From: Zefek Date: Sat, 9 May 2026 16:15:47 +0200 Subject: [PATCH 1/4] MQTTESP8266 V6 --- .github/workflows/build.yml | 11 +++-------- .github/workflows/deployment.yml | 15 ++++----------- .gitmodules | 4 ++++ LSSensor.ino | 22 ++++++++++++++++++++++ MQTTESP8266 | 1 + sketch.yaml | 8 ++++++++ 6 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 .gitmodules create mode 160000 MQTTESP8266 create mode 100644 sketch.yaml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 17fb98b..a1d859c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,6 +13,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v6 + with: + submodules: true - name: Install Arduino CLI uses: arduino/setup-arduino-cli@81d310742121c928ea9c8bbd407b4217b432ae02 # v2.0.0 @@ -22,13 +24,6 @@ jobs: arduino-cli config init arduino-cli core update-index - - name: Install required board core - run: arduino-cli core install arduino:avr@1.8.7 # napÅ™. pro Arduino Uno - - - name: Install custom library - run: | - arduino-cli lib install "MQTTESP8266@5.1.2" - - name: Set configs run: | cp config_default.h config.h @@ -36,4 +31,4 @@ jobs: - name: Compile Arduino project run: | - arduino-cli compile --fqbn arduino:avr:uno ./ + arduino-cli compile --profile LSSensor_Uno ./ diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 77ef325..2b4bc2c 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -12,20 +12,13 @@ jobs: - name: Checkout repository uses: actions/checkout@v6 + with: + submodules: true - name: Initialize Arduino CLI run: | & "$Env:RUNNER_TOOL_CACHE\arduino-cli\arduino-cli.exe" --config-file config.yaml core update-index - - name: Install required board core - run: | - & "$Env:RUNNER_TOOL_CACHE\arduino-cli\arduino-cli.exe" --config-file config.yaml core install arduino:avr@1.8.7 - - - name: Install custom Arduino libraries (Windows) - shell: powershell - run: | - & "$Env:RUNNER_TOOL_CACHE\arduino-cli\arduino-cli.exe" --config-file config.yaml lib install "MQTTESP8266@5.1.2" - - name: Set configs (Windows) shell: powershell env: @@ -72,7 +65,7 @@ jobs: - name: Compile Arduino project run: | - & "$Env:RUNNER_TOOL_CACHE\arduino-cli\arduino-cli.exe" --config-file config.yaml compile --fqbn arduino:avr:uno ./ --output-dir build + & "$Env:RUNNER_TOOL_CACHE\arduino-cli\arduino-cli.exe" --config-file config.yaml compile --profile LSSensor_Uno ./ --output-dir build - name: Upload firmware to Arduino env: @@ -82,4 +75,4 @@ jobs: Write-Error "Invalid serial port format: $env:SERIAL_PORT" exit 1 } - & "$Env:RUNNER_TOOL_CACHE\arduino-cli\arduino-cli.exe" --config-file config.yaml upload --fqbn arduino:avr:uno --port $env:SERIAL_PORT --input-dir build + & "$Env:RUNNER_TOOL_CACHE\arduino-cli\arduino-cli.exe" --config-file config.yaml upload --profile LSSensor_Uno --port $env:SERIAL_PORT --input-dir build diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f2db511 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "MQTTESP8266"] + path = MQTTESP8266 + url = https://github.com/Zefek/MQTTESP8266.git + branch = 6.0 diff --git a/LSSensor.ino b/LSSensor.ino index 92f0395..fd13c35 100644 --- a/LSSensor.ino +++ b/LSSensor.ino @@ -9,6 +9,8 @@ #define SENDINTERVAL 5 * 60 * 1000 //5 minut void MQTTMessageReceive(char* topic, uint8_t* payload, uint16_t length) { } +void OnBusy(uint8_t count); +void DataTimeout(); MQTTConnectData mqttConnectData = { MQTTHost, 1883, "WattMeter", MQTTUsername, MQTTPassword, "", 0, false, "", false, 0x0F }; SoftwareSerial serial(4, 5); @@ -21,6 +23,7 @@ int wattMetter2Counter = 0; unsigned long lastSendToMQTT = 0; unsigned long lastTime = 0; +bool closeRequired = false; void WattMetter1Received() { unsigned long time = millis(); @@ -66,6 +69,18 @@ bool Connect() return false; } +void DataTimeout() +{ + closeRequired = true; +} +void OnBusy(uint8_t count) +{ + if(count > 10) + { + closeRequired = true; + } +} + void setup() { // put your setup code here, to run once: pinMode(2, INPUT); @@ -73,6 +88,8 @@ void setup() { Serial.begin(57600); serial.begin(57600); espDrv.Init(16); + espDrv.OnBusy = OnBusy; + espDrv.DataTimeout = DataTimeout; espDrv.Connect(WifiSSID, WifiPassword); attachInterrupt(digitalPinToInterrupt(2), WattMetter1Received, RISING); attachInterrupt(digitalPinToInterrupt(3), WattMetter2Received, RISING); @@ -82,6 +99,11 @@ void setup() { void loop() { wdt_reset(); + if(closeRequired) + { + espDrv.Close(); + closeRequired = false; + } mqttClient.Loop(); unsigned long currentMillis = millis(); if(currentMillis - lastSendToMQTT >= 300000) diff --git a/MQTTESP8266 b/MQTTESP8266 new file mode 160000 index 0000000..0fc63a7 --- /dev/null +++ b/MQTTESP8266 @@ -0,0 +1 @@ +Subproject commit 0fc63a725df0f208de012f7cbcc4f4243d4a72ad diff --git a/sketch.yaml b/sketch.yaml new file mode 100644 index 0000000..c4c7be5 --- /dev/null +++ b/sketch.yaml @@ -0,0 +1,8 @@ +profiles: + LSSensor_Uno: + fqbn: arduino:avr:uno + platforms: + - platform: arduino:avr (1.8.7) + libraries: + - dir: MQTTESP8266 +default_profile: LSSensor_Uno From de375b5669b56ea9d29d89bfa2bf0ec104a2451d Mon Sep 17 00:00:00 2001 From: Zefek Date: Sat, 9 May 2026 16:18:19 +0200 Subject: [PATCH 2/4] Makra --- LSSensor.ino | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/LSSensor.ino b/LSSensor.ino index fd13c35..17f8e7d 100644 --- a/LSSensor.ino +++ b/LSSensor.ino @@ -6,6 +6,7 @@ #include #define LSSensorPIN1 2 +#define LSSensorPIN2 3 #define SENDINTERVAL 5 * 60 * 1000 //5 minut void MQTTMessageReceive(char* topic, uint8_t* payload, uint16_t length) { } @@ -83,16 +84,16 @@ void OnBusy(uint8_t count) void setup() { // put your setup code here, to run once: - pinMode(2, INPUT); - pinMode(3, INPUT); + pinMode(LSSensorPIN1, INPUT); + pinMode(LSSensorPIN2, INPUT); Serial.begin(57600); serial.begin(57600); espDrv.Init(16); espDrv.OnBusy = OnBusy; espDrv.DataTimeout = DataTimeout; espDrv.Connect(WifiSSID, WifiPassword); - attachInterrupt(digitalPinToInterrupt(2), WattMetter1Received, RISING); - attachInterrupt(digitalPinToInterrupt(3), WattMetter2Received, RISING); + attachInterrupt(digitalPinToInterrupt(LSSensorPIN1), WattMetter1Received, RISING); + attachInterrupt(digitalPinToInterrupt(LSSensorPIN2), WattMetter2Received, RISING); Serial.println("Setup OK"); wdt_enable(WDTO_8S); } @@ -108,8 +109,8 @@ void loop() { unsigned long currentMillis = millis(); if(currentMillis - lastSendToMQTT >= 300000) { - detachInterrupt(digitalPinToInterrupt(2)); - detachInterrupt(digitalPinToInterrupt(3)); + detachInterrupt(digitalPinToInterrupt(LSSensorPIN1)); + detachInterrupt(digitalPinToInterrupt(LSSensorPIN2)); if(Connect()) { sprintf(data, "{\"V\":%d,\"S\":%d}", 0, 0); @@ -122,7 +123,7 @@ void loop() { wattMetter2Counter = 0; } lastSendToMQTT = currentMillis; - attachInterrupt(digitalPinToInterrupt(2), WattMetter1Received, RISING); - attachInterrupt(digitalPinToInterrupt(3), WattMetter2Received, RISING); + attachInterrupt(digitalPinToInterrupt(LSSensorPIN1), WattMetter1Received, RISING); + attachInterrupt(digitalPinToInterrupt(LSSensorPIN2), WattMetter2Received, RISING); } } From 4f3ecc60457002be3d36df6ffdbbed6605017731 Mon Sep 17 00:00:00 2001 From: Zefek <32429493+Zefek@users.noreply.github.com> Date: Sat, 9 May 2026 16:35:15 +0200 Subject: [PATCH 3/4] Diagnostika (#8) --- LSSensor.ino | 44 ++++++++++++++++++++++++++++++++++++++++---- config_default.h | 1 + 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/LSSensor.ino b/LSSensor.ino index 17f8e7d..5859641 100644 --- a/LSSensor.ino +++ b/LSSensor.ino @@ -18,13 +18,31 @@ SoftwareSerial serial(4, 5); EspDrv espDrv(&serial); MQTTClient mqttClient(&espDrv, MQTTMessageReceive); char data[32]; - int wattMetter1Counter = 0; int wattMetter2Counter = 0; unsigned long lastSendToMQTT = 0; - unsigned long lastTime = 0; bool closeRequired = false; + +#pragma pack(push, 1) +struct DiagData { + uint32_t uptime; + uint16_t freeRam; + uint16_t wifiReconn; + uint16_t mqttFailCount; + uint8_t resetReason; + uint16_t loopMaxMs; +}; +#pragma pack(pop) + +DiagData currentDiagData; + +extern int __heap_start, *__brkval; +int freeRam() { + int v; + return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval); +} + void WattMetter1Received() { unsigned long time = millis(); @@ -53,6 +71,10 @@ bool Connect() if(wifiStatus == WL_DISCONNECTED || wifiStatus == WL_IDLE_STATUS) { wifiConnected = espDrv.Connect(WifiSSID, WifiPassword); + if(currentDiagData.wifiReconn < 65535) + { + currentDiagData.wifiReconn++; + } } if(wifiConnected) { @@ -60,7 +82,9 @@ bool Connect() bool isConnected = mqttClient.IsConnected(); if(!isConnected) { - return mqttClient.Connect(mqttConnectData); + bool ok = mqttClient.Connect(mqttConnectData); + if(!ok && currentDiagData.mqttFailCount < 65535) currentDiagData.mqttFailCount++; + return ok; } else { @@ -83,6 +107,8 @@ void OnBusy(uint8_t count) } void setup() { + currentDiagData.resetReason = MCUSR; + MCUSR = 0; // put your setup code here, to run once: pinMode(LSSensorPIN1, INPUT); pinMode(LSSensorPIN2, INPUT); @@ -99,6 +125,7 @@ void setup() { } void loop() { + unsigned long currentMillis = millis(); wdt_reset(); if(closeRequired) { @@ -106,7 +133,6 @@ void loop() { closeRequired = false; } mqttClient.Loop(); - unsigned long currentMillis = millis(); if(currentMillis - lastSendToMQTT >= 300000) { detachInterrupt(digitalPinToInterrupt(LSSensorPIN1)); @@ -121,9 +147,19 @@ void loop() { wattMetter1Counter = 0; wattMetter2Counter = 0; + + currentDiagData.uptime = currentMillis / 60000UL; + currentDiagData.freeRam = freeRam(); + mqttClient.Publish(LSSENSOR_DIAG, (const uint8_t*)¤tDiagData, sizeof(DiagData), false); + currentDiagData.loopMaxMs = 0; } lastSendToMQTT = currentMillis; attachInterrupt(digitalPinToInterrupt(LSSensorPIN1), WattMetter1Received, RISING); attachInterrupt(digitalPinToInterrupt(LSSensorPIN2), WattMetter2Received, RISING); } + unsigned long iterDur = millis() - currentMillis; + if(iterDur > currentDiagData.loopMaxMs) + { + currentDiagData.loopMaxMs = (iterDur > 65535UL) ? 65535 : (uint16_t)iterDur; + } } diff --git a/config_default.h b/config_default.h index 48c4703..e7e6f4f 100644 --- a/config_default.h +++ b/config_default.h @@ -1 +1,2 @@ #define ELCONSUMPTION "ELCONSUMPTION" +#define LSSENSOR_DIAG "LSSENSOR_DIAG" From 5c723adf16c4698a1bae0fe916cbfdd054faee37 Mon Sep 17 00:00:00 2001 From: Zefek <32429493+Zefek@users.noreply.github.com> Date: Sat, 9 May 2026 22:56:15 +0200 Subject: [PATCH 4/4] Reconnect MQTT client before resetting counters --- LSSensor.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LSSensor.ino b/LSSensor.ino index 5859641..1909a42 100644 --- a/LSSensor.ino +++ b/LSSensor.ino @@ -143,7 +143,6 @@ void loop() { mqttClient.Publish(ELCONSUMPTION, data); sprintf(data, "{\"V\":%d,\"S\":%d}", wattMetter1Counter, wattMetter2Counter); mqttClient.Publish(ELCONSUMPTION, data); - mqttClient.Disconnect(); wattMetter1Counter = 0; wattMetter2Counter = 0; @@ -152,6 +151,7 @@ void loop() { currentDiagData.freeRam = freeRam(); mqttClient.Publish(LSSENSOR_DIAG, (const uint8_t*)¤tDiagData, sizeof(DiagData), false); currentDiagData.loopMaxMs = 0; + mqttClient.Disconnect(); } lastSendToMQTT = currentMillis; attachInterrupt(digitalPinToInterrupt(LSSensorPIN1), WattMetter1Received, RISING);