Skip to content

Commit 3232fb7

Browse files
committed
v2.2.0, TLS support, home assistant discovery
- added TLS to encrypt MQTT connection (without server fingerprinting) - increased CPU speed from 80 to 160 MHz for TLS - added option for Home Assistant MQTT sensor auto-discovery - set MQTT keep-alive timeout to match publish interval - version 2.2.0
1 parent 4bbcc07 commit 3232fb7

13 files changed

+303
-33
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ alt="settings of the wifi power meter" width="250">&nbsp;&nbsp;&nbsp;<img align=
132132
src="assets/expert_settings.png" alt="expert settings to tune the detection of
133133
the red marker" width="250"></p><br>
134134

135-
I'm using MQTT (enabled under `Settings`) to pass the meter readings to [home
136-
assistant](https://www.home-assistant.io) but the Wifi Power Meter also offers
137-
support for RESTfult HTTP requests. Readings are available as JSON under
135+
I'm using MQTT (enabled under `Settings`) to pass the meter readings to [Home
136+
Assistant](https://www.home-assistant.io) using its [discovery
137+
function](https://www.home-assistant.io/docs/mqtt/discovery/) but the Wifi Power Meter
138+
also offers support for RESTful HTTP requests. Readings are available as JSON under
138139
`http://<IP>/readings`. See `restful.html` as an example.
139140

140141
## Debug readings with InfluxDB and Grafana

assets/expert_settings.png

-643 Bytes
Loading

assets/main_page.png

-38.5 KB
Loading

assets/main_settings.png

-43.8 KB
Loading

include/config.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@
4242
#define MQTT_BROKER_PORT 1883
4343
#define MQTT_BASE_TOPIC "__mqtt_topic__"
4444
#define MQTT_PUBLISH_INTERVAL_SEC 60
45+
4546
// uncomment to enable MQTT authentication
4647
//#define MQTT_USERNAME "admin"
4748
//#define MQTT_PASSWORD "secret"
4849

50+
// uncomment to enable encrypted MQTT connection
51+
// MQTT must have TLS enabled on port 8883
52+
//#define MQTT_USE_TLS
53+
54+
// uncomment to enable Home Assistant MQTT auto discovery
55+
//#define MQTT_HA_AUTO_DISCOVERY
56+
4957
// the following settings should be changed with care
5058
// better use web ui (expert settings) for fine-tuning
5159
#define READINGS_BUFFER_SEC 90
@@ -69,7 +77,23 @@
6977
// to make Arduino IDE happy
7078
// version number is set in platformio.ini
7179
#ifndef FIRMWARE_VERSION
72-
#define FIRMWARE_VERSION 213
80+
#define FIRMWARE_VERSION 220
81+
#endif
82+
83+
// set default port for MQTT over TLS
84+
#ifdef MQTT_USE_TLS
85+
#undef MQTT_BROKER_PORT
86+
#define MQTT_BROKER_PORT 8883
87+
#endif
88+
89+
// enable MQTT and JSON if HA discovery is set
90+
#ifdef MQTT_HA_AUTO_DISCOVERY
91+
#ifndef MQTT_ENABLE
92+
#define MQTT_ENABLE
93+
#endif
94+
#ifndef MQTT_PUBLISH_JSON
95+
#define MQTT_PUBLISH_JSON
96+
#endif
7397
#endif
7498

7599
#endif

include/index_de.h

+24-2
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ function toggleMQTTAuth() {
433433
}
434434
}
435435

436+
function toggleMQTTSecure() {
437+
if (document.getElementById("checkbox_mqtt_secure").checked == true) {
438+
document.getElementById("input_mqttport").value = 8883;
439+
} else {
440+
document.getElementById("input_mqttport").value = 1883;
441+
}
442+
}
443+
436444
function togglePower() {
437445
if (document.getElementById("checkbox_power").checked == true) {
438446
document.getElementById("power").style.display = "block";
@@ -449,9 +457,21 @@ function togglePowerAvg() {
449457
}
450458
}
451459

460+
function toggleJSON() {
461+
if (document.getElementById("checkbox_mqtt_json").checked == false) {
462+
document.getElementById("checkbox_mqtt_ha_discovery").checked = false;
463+
}
464+
}
465+
466+
function toggleHADiscovery() {
467+
if (document.getElementById("checkbox_mqtt_ha_discovery").checked == true) {
468+
document.getElementById("checkbox_mqtt_json").checked = true;
469+
}
470+
}
471+
452472
</script>
453473
</head>
454-
<body onload="configSaved(); togglePower(); toggleMQTT(); toggleMQTTAuth(); togglePowerAvg();">
474+
<body onload="configSaved(); togglePower(); toggleMQTT(); toggleMQTTAuth(); toggleMQTTSecure(); togglePowerAvg(); toggleJSON(); toggleHADiscovery();">
455475
<div style="text-align:left;display:inline-block;min-width:340px;">
456476
<div style="text-align:center;">
457477
<h2 id="heading">Einstellungen</h2>
@@ -493,7 +513,8 @@ function togglePowerAvg() {
493513
<input id="input_mqttport" name="mqttport" size="16" maxlength="5" value="__MQTT_PORT__" onkeyup="digitsOnly(this);"></p>
494514
<p><b>Topic f&uuml;r Nachrichten</b><br />
495515
<input id="input_mqttbasetopic" name="mqttbasetopic" size="16" maxlength="64" value="__MQTT_BASE_TOPIC__" onkeyup="ASCIIOnly(this);"></p>
496-
<p><input id="checkbox_mqtt_json" name="mqtt_json" type="checkbox" __MQTT_JSON__><b>Daten als JSON publizieren</b></p>
516+
<p><input id="checkbox_mqtt_json" name="mqtt_json" onclick="toggleJSON();" type="checkbox" __MQTT_JSON__><b>Daten als JSON publizieren</b></p>
517+
<p><input id="checkbox_mqtt_ha_discovery" name="mqtt_ha_discovery" onclick="toggleHADiscovery();" type="checkbox" __MQTT_HA_DISCOVERY__><b>Home Assistant Discovery</b></p>
497518
<p><b>Nachrichteninterval (Sek.)</b><br />
498519
<input name="mqttinterval" value="__MQTT_INTERVAL__" maxlength="4" onkeyup="digitsOnly(this);"></p>
499520
<p><input id="checkbox_mqttauth" name="mqttauth" onclick="toggleMQTTAuth();" type="checkbox" __MQTT_AUTH__><b>Authentifizierung aktivieren</b></p>
@@ -503,6 +524,7 @@ function togglePowerAvg() {
503524
<p><b>Passwort</b><br />
504525
<input id="input_mqttpassword" type="password" name="mqttpassword" size="16" maxlength="32" value="__MQTT_PASSWORD__"></p>
505526
</span>
527+
<p><input id="checkbox_mqtt_secure" name="mqtt_secure" type="checkbox" onclick="toggleMQTTSecure();" __MQTT_SECURE__><b>Verschlüsselte Verbindung</b></p>
506528
</span>
507529
</fieldset>
508530
<br />

include/index_en.h

+25-3
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ function toggleMQTTAuth() {
433433
}
434434
}
435435

436+
function toggleMQTTSecure() {
437+
if (document.getElementById("checkbox_mqtt_secure").checked == true) {
438+
document.getElementById("input_mqttport").value = 8883;
439+
} else {
440+
document.getElementById("input_mqttport").value = 1883;
441+
}
442+
}
443+
436444
function togglePower() {
437445
if (document.getElementById("checkbox_power").checked == true) {
438446
document.getElementById("power").style.display = "block";
@@ -449,9 +457,21 @@ function togglePowerAvg() {
449457
}
450458
}
451459

460+
function toggleJSON() {
461+
if (document.getElementById("checkbox_mqtt_json").checked == false) {
462+
document.getElementById("checkbox_mqtt_ha_discovery").checked = false;
463+
}
464+
}
465+
466+
function toggleHADiscovery() {
467+
if (document.getElementById("checkbox_mqtt_ha_discovery").checked == true) {
468+
document.getElementById("checkbox_mqtt_json").checked = true;
469+
}
470+
}
471+
452472
</script>
453473
</head>
454-
<body onload="configSaved(); togglePower(); toggleMQTT(); toggleMQTTAuth(); togglePowerAvg();">
474+
<body onload="configSaved(); togglePower(); toggleMQTT(); toggleMQTTAuth(); toggleMQTTSecure(); togglePowerAvg(); toggleJSON(); toggleHADiscovery();">
455475
<div style="text-align:left;display:inline-block;min-width:340px;">
456476
<div style="text-align:center;">
457477
<h2 id="heading">Settings</h2>
@@ -493,7 +513,8 @@ function togglePowerAvg() {
493513
<input id="input_mqttport" name="mqttport" size="16" maxlength="5" value="__MQTT_PORT__" onkeyup="digitsOnly(this);"></p>
494514
<p><b>Message topic</b><br />
495515
<input id="input_mqttbasetopic" name="mqttbasetopic" size="16" maxlength="64" value="__MQTT_BASE_TOPIC__" onkeyup="ASCIIOnly(this);"></p>
496-
<p><input id="checkbox_mqtt_json" name="mqtt_json" type="checkbox" __MQTT_JSON__><b>Publish data as JSON</b></p>
516+
<p><input id="checkbox_mqtt_json" name="mqtt_json" onclick="toggleJSON();" type="checkbox" __MQTT_JSON__><b>Publish data as JSON</b></p>
517+
<p><input id="checkbox_mqtt_ha_discovery" name="mqtt_ha_discovery" onclick="toggleHADiscovery();" type="checkbox" __MQTT_HA_DISCOVERY__><b>Home Assistant Discovery</b></p>
497518
<p><b>Publishing interval (sec.)</b><br />
498519
<input name="mqttinterval" value="__MQTT_INTERVAL__" maxlength="4" onkeyup="digitsOnly(this);"></p>
499520
<p><input id="checkbox_mqttauth" name="mqttauth" onclick="toggleMQTTAuth();" type="checkbox" __MQTT_AUTH__><b>Enable authentication</b></p>
@@ -503,7 +524,8 @@ function togglePowerAvg() {
503524
<p><b>Password</b><br />
504525
<input id="input_mqttpassword" type="password" name="mqttpassword" size="16" maxlength="32" value="__MQTT_PASSWORD__"></p>
505526
</span>
506-
</span>
527+
<p><input id="checkbox_mqtt_secure" name="mqtt_secure" type="checkbox" onclick="toggleMQTTSecure();" __MQTT_SECURE__><b>Enable TLS</b></p>
528+
</span>
507529
</fieldset>
508530
<br />
509531

include/mqtt.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717
#include <Arduino.h>
1818
#include <PubSubClient.h>
1919
#include <ArduinoJson.h>
20+
#include <WiFiClient.h>
21+
#include <WiFiClientSecure.h>
2022

21-
#define MQTT_CLIENT_ID "PowerMeter_%x"
23+
#define MQTT_CLIENT_ID "WifiPowerMeter_%x"
2224
#define MQTT_SUBTOPIC_CNT "counter"
2325
#define MQTT_SUBTOPIC_CONS "consumption"
2426
#define MQTT_SUBTOPIC_PWR "power"
2527
#define MQTT_SUBTOPIC_RUNT "runtime"
2628
#define MQTT_SUBTOPIC_RSSI "rssi"
2729
#define MQTT_SUBTOPIC_HEAP "freeheap"
28-
29-
extern PubSubClient mqtt;
30+
#define MQTT_TOPIC_DISCOVER "homeassistant/sensor/wifipowermeter-"
3031

3132
void mqttPublish();
33+
void mqttDisconnect();
3234

3335
#endif

include/nvs.h

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ typedef struct {
4242
char mqttUsername[33];
4343
char mqttPassword[33];
4444
bool mqttJSON;
45+
bool enableHADiscovery;
46+
bool mqttSecure;
4547
bool enableInflux;
4648
char systemID[17];
4749
uint8_t magic;

platformio.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ default_envs = d1_mini
1212
description = Firmware for ESP8266 power meter
1313

1414
[common]
15-
firmware_version = 213
15+
firmware_version = 220
1616
upload_speed = 460800
1717
monitor_speed = 115200
1818
port = /dev/tty.wchusbserial1410
@@ -31,7 +31,7 @@ build_flags =
3131
platform = espressif8266
3232
board = d1_mini
3333
framework = arduino
34-
board_build.f_cpu = 80000000L
34+
board_build.f_cpu = 160000000L
3535
build_flags = ${common.build_flags}
3636
lib_deps = ${common.lib_deps_all}
3737
upload_speed = ${common.upload_speed}

0 commit comments

Comments
 (0)