Skip to content

Commit

Permalink
v2.3.0, fixed issue #7 and #8
Browse files Browse the repository at this point in the history
- issue #7: removed old code fragment limiting reading interval from findRisingEdge()
- issue #8: increased size for JSON docs and MQTT connection buffers in mqtt.c
- added publishJSON() which carefully checks JSON serialization and MQTT return codes
- use addDeviceDescription() to add device info to HA discovery topics
- if HA discovery is disabled also delete corresponding configuration topic for HA
- always disconnect from MQTT server when saving main settings
- hide field in web ui to specify time for moving average if set to 0 in config.h
- reconnect to WiFi every 30 secs. and blink LED if WiFi offline and publish additional
  topic with WiFi reconnect counter
- added copyright header to include files with HTML templates
- make system id configurable in config.h
- version 2.3.0
  • Loading branch information
lrswss committed Nov 13, 2022
1 parent 1f323cd commit ad8e2df
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 116 deletions.
8 changes: 6 additions & 2 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@
// uncomment to enable Home Assistant MQTT auto discovery
//#define MQTT_HA_AUTO_DISCOVERY

// optional preset power meter's id (max. 16 characters)
// defaults to last 3 octets of ESP8266's MAC address
//#define SYSTEM_ID "12345678"

// the following settings should be changed with care
// better use web ui (expert settings) for fine-tuning
#define READINGS_BUFFER_SEC 90
#define READINGS_INTERVAL_MS 25
#define READINGS_SPREAD_MIN 4
#define READINGS_SPREAD_MIN 3
#define ABOVE_THRESHOLD_TRIGGER 3
#define PULSE_DEBOUNCE_MS 2000
#define BACKUP_CYCLE_MIN 60
Expand All @@ -77,7 +81,7 @@
// to make Arduino IDE happy
// version number is set in platformio.ini
#ifndef FIRMWARE_VERSION
#define FIRMWARE_VERSION 224
#define FIRMWARE_VERSION 230
#endif

// set default port for MQTT over TLS
Expand Down
13 changes: 13 additions & 0 deletions include/index_de.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/***************************************************************************
Copyright (c) 2019-2022 Lars Wessels
This file a part of the "ESP8266 Wifi Power Meter" source code.
https://github.com/lrswss/esp8266-wifi-power-meter
Licensed under the MIT License. You may not use this file except in
compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT
***************************************************************************/

const char HEADER_html[] PROGMEM = R"=====(
<!DOCTYPE html>
<html lang="de">
Expand Down
13 changes: 13 additions & 0 deletions include/index_en.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/***************************************************************************
Copyright (c) 2019-2022 Lars Wessels
This file a part of the "ESP8266 Wifi Power Meter" source code.
https://github.com/lrswss/esp8266-wifi-power-meter
Licensed under the MIT License. You may not use this file except in
compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT
***************************************************************************/

const char HEADER_html[] PROGMEM = R"=====(
<!DOCTYPE html>
<html lang="en">
Expand Down
1 change: 1 addition & 0 deletions include/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define MQTT_SUBTOPIC_RUNT "runtime"
#define MQTT_SUBTOPIC_RSSI "rssi"
#define MQTT_SUBTOPIC_HEAP "freeheap"
#define MQTT_SUBTOPIC_WIFI "wificounter"
#define MQTT_TOPIC_DISCOVER "homeassistant/sensor/wifipowermeter-"

void mqttPublish();
Expand Down
1 change: 1 addition & 0 deletions include/wlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <WiFiManager.h>

extern WiFiManager wm;
extern uint16_t wifiReconnectCounter;

#define WIFI_AP_SSID "WifiPowerMeter"
#define WIFI_MIN_RSSI 25
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ default_envs = d1_mini
description = Firmware for ESP8266 power meter

[common]
firmware_version = 223
firmware_version = 230
upload_speed = 460800
monitor_speed = 115200
port = /dev/tty.wchusbserial1410
Expand Down
13 changes: 4 additions & 9 deletions src/ferraris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,16 @@ static void calculateThreshold() {
// try to find a rising edge in previous analog readings
static bool findRisingEdge() {
uint16_t i, belowThresholdTrigger;
uint8_t s = 1;
uint16_t belowTh = 0;
uint16_t aboveTh = 0;

// reading interval min. 50ms
if (50 / settings.readingsIntervalMs > 1)
s = 50 / settings.readingsIntervalMs;

// min. number of pulses below threshold required to detect a rising edge
belowThresholdTrigger = (settings.pulseDebounceMs / 2) / settings.readingsIntervalMs;

i = ferraris.index > 0 ? ferraris.index : ferraris.size - 1;
while (i - s >= 0 && belowTh <= belowThresholdTrigger &&
while (i - 1 >= 0 && belowTh <= belowThresholdTrigger &&
aboveTh <= settings.aboveThresholdTrigger) {
i -= s;
i -= 1;
if (pulseReadings[i] < settings.pulseThreshold)
belowTh++;
else
Expand All @@ -120,9 +115,9 @@ static bool findRisingEdge() {

// round robin...
i = ferraris.size;
while (i - s > ferraris.index && belowTh <= belowThresholdTrigger &&
while (i - 1 > ferraris.index && belowTh <= belowThresholdTrigger &&
aboveTh <= settings.aboveThresholdTrigger) {
i -= s;
i -= 1;
if (pulseReadings[i] < settings.pulseThreshold)
belowTh++;
else
Expand Down
17 changes: 13 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ void loop() {
static uint32_t previousMeasurementMillis = 0;
static uint32_t prevLoopTimer = 0;
static uint32_t busyTime = 0;
static uint8_t wifiOfflineCounter = 0;

// scan ferraris disk for read marker
// scan ferraris disk for red marker
if (tsDiff(previousMeasurementMillis) > settings.readingsIntervalMs) {
previousMeasurementMillis = millis();
if (readFerraris()) {
Expand All @@ -81,14 +82,22 @@ void loop() {
busyTime += 1;

// regular MQTT publish interval (if enabled)
// try to reconnect to WiFi
if (settings.enableMQTT && !(busyTime % settings.mqttIntervalSecs))
mqttPublish();

// blink LED if WiFi is not available
if (!WiFi.isConnected())
// blink LED if WiFi is (still) not available
// try to reconnect every 30 seconds
if (!WiFi.isConnected()) {
toggleLED();
else
if (wifiOfflineCounter++ >= 30) {
wifiOfflineCounter = 0;
wifiReconnectCounter++;
WiFi.reconnect();
}
} else {
switchLED(false);
}

// frequently save counter readings and threshold to EEPROM
if (!(busyTime % (settings.backupCycleMin * 60)))
Expand Down
Loading

0 comments on commit ad8e2df

Please sign in to comment.