feat: make WiFi power-save and HA discovery configurable
- WIFI_DISABLE_POWER_SAVE (default 1 = keep modem sleep off) - HA_DISCOVERY_ENABLED: switch auto-discovery off entirely; the payload strings are compiled out, saving ~4.6 KB of flash - HA_DISCOVERY_PREFIX, HA_DEVICE_ID, HA_UNIQUE_PREFIX, HA_DEVICE_NAME, HA_MODEL and HA_MANUFACTURER replace the hardcoded garage identifiers, so a second MPPT can run without entity/topic collisions - Defaults reproduce the previous topics, unique_ids and payloads exactly - Document the new options in README; all values remain #ifndef-guarded so existing config.h files keep compiling
This commit is contained in:
parent
a2fea10678
commit
156f43323c
17
README.md
17
README.md
|
|
@ -117,6 +117,23 @@ cp include/example.config.h include/config.h
|
|||
|
||||
`config.h` is gitignored — your key does not end up in a commit.
|
||||
|
||||
Optional settings in the same file:
|
||||
|
||||
```cpp
|
||||
// 1 = disable WiFi modem sleep (default, recommended)
|
||||
#define WIFI_DISABLE_POWER_SAVE 1
|
||||
|
||||
// 1 = publish Home Assistant discovery, 0 = data/status/diag only
|
||||
#define HA_DISCOVERY_ENABLED 1
|
||||
#define HA_DISCOVERY_PREFIX "homeassistant"
|
||||
#define HA_DEVICE_ID "victron_smartsolar_garage"
|
||||
#define HA_UNIQUE_PREFIX "victron_mppt"
|
||||
#define HA_DEVICE_NAME "Victron SmartSolar MPPT"
|
||||
```
|
||||
|
||||
If you run a second MPPT, give it its own `HA_DEVICE_ID` **and** `HA_UNIQUE_PREFIX`
|
||||
so its entities and discovery topics do not collide with the first one.
|
||||
|
||||
<details>
|
||||
<summary><b>Finding your MAC address and encryption key</b></summary>
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,10 @@
|
|||
#define WIFI_SSID "your_wifi_ssid"
|
||||
#define WIFI_PASSWORD "your_wifi_password"
|
||||
|
||||
// 1 = disable WiFi modem sleep (recommended: lower latency, fewer dropped
|
||||
// MQTT keepalives). 0 = allow modem sleep for lower power draw.
|
||||
#define WIFI_DISABLE_POWER_SAVE 1
|
||||
|
||||
// ── MQTT broker ───────────────────────────────────────────────────────────────
|
||||
#define MQTT_HOST "192.168.1.x" // IP address of your MQTT broker
|
||||
#define MQTT_PORT 1883
|
||||
|
|
@ -27,6 +31,22 @@
|
|||
// Find it in Victron Connect app → device → top-right menu → Product Info → Instant readout details
|
||||
#define VICTRON_ENC_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||
|
||||
// ── Home Assistant discovery ──────────────────────────────────────────────────
|
||||
// 1 = publish MQTT discovery configs, 0 = only publish data/status/diag.
|
||||
#define HA_DISCOVERY_ENABLED 1
|
||||
|
||||
// Discovery topic prefix used by Home Assistant (usually "homeassistant").
|
||||
#define HA_DISCOVERY_PREFIX "homeassistant"
|
||||
|
||||
// Device identity. Change both for a second MPPT so the entities do not collide.
|
||||
// HA_DEVICE_ID forms the discovery topic path and the device identifier.
|
||||
// HA_UNIQUE_PREFIX prefixes each entity's unique_id.
|
||||
#define HA_DEVICE_ID "victron_smartsolar_garage"
|
||||
#define HA_UNIQUE_PREFIX "victron_mppt"
|
||||
#define HA_DEVICE_NAME "Victron SmartSolar MPPT"
|
||||
#define HA_MODEL "SmartSolar MPPT 100/20"
|
||||
#define HA_MANUFACTURER "Victron Energy"
|
||||
|
||||
// ── Timing ────────────────────────────────────────────────────────────────────
|
||||
// How often to publish to MQTT (milliseconds)
|
||||
#define PUBLISH_INTERVAL_MS 10000
|
||||
|
|
|
|||
75
src/main.cpp
75
src/main.cpp
|
|
@ -39,6 +39,34 @@ static uint32_t s_lastDiscoveryAttemptMs = 0;
|
|||
#define MQTT_TOPIC_DIAG "solar/mppt/diag"
|
||||
#endif
|
||||
|
||||
// WiFi power-save default (1 = modem sleep disabled)
|
||||
#ifndef WIFI_DISABLE_POWER_SAVE
|
||||
#define WIFI_DISABLE_POWER_SAVE 1
|
||||
#endif
|
||||
|
||||
// Home Assistant discovery defaults — keeps older config.h files working
|
||||
#ifndef HA_DISCOVERY_ENABLED
|
||||
#define HA_DISCOVERY_ENABLED 1
|
||||
#endif
|
||||
#ifndef HA_DISCOVERY_PREFIX
|
||||
#define HA_DISCOVERY_PREFIX "homeassistant"
|
||||
#endif
|
||||
#ifndef HA_DEVICE_ID
|
||||
#define HA_DEVICE_ID "victron_smartsolar_garage"
|
||||
#endif
|
||||
#ifndef HA_UNIQUE_PREFIX
|
||||
#define HA_UNIQUE_PREFIX "victron_mppt"
|
||||
#endif
|
||||
#ifndef HA_DEVICE_NAME
|
||||
#define HA_DEVICE_NAME "Victron SmartSolar MPPT"
|
||||
#endif
|
||||
#ifndef HA_MODEL
|
||||
#define HA_MODEL "SmartSolar MPPT 100/20"
|
||||
#endif
|
||||
#ifndef HA_MANUFACTURER
|
||||
#define HA_MANUFACTURER "Victron Energy"
|
||||
#endif
|
||||
|
||||
#ifndef DIAG_INTERVAL_MS
|
||||
#define DIAG_INTERVAL_MS 60000UL
|
||||
#endif
|
||||
|
|
@ -178,6 +206,10 @@ static bool mqttEnsureConnected(uint32_t now) {
|
|||
|
||||
static bool publishDiscovery() {
|
||||
|
||||
#if !HA_DISCOVERY_ENABLED
|
||||
s_discoveryPublished = true;
|
||||
return true;
|
||||
#else
|
||||
uint32_t now = millis();
|
||||
if (s_lastDiscoveryAttemptMs != 0 &&
|
||||
now - s_lastDiscoveryAttemptMs < DISCOVERY_RETRY_MS) return false;
|
||||
|
|
@ -185,67 +217,69 @@ static bool publishDiscovery() {
|
|||
|
||||
// Common device suffix appended to every payload
|
||||
// identifiers must match across all sensors for HA to group them
|
||||
#define DEV ",\"device\":{\"identifiers\":[\"victron_smartsolar_garage\"],\"name\":\"Victron SmartSolar MPPT\",\"model\":\"SmartSolar MPPT 100/20\",\"manufacturer\":\"Victron Energy\"}}"
|
||||
#define DEV ",\"device\":{\"identifiers\":[\"" HA_DEVICE_ID "\"],\"name\":\"" HA_DEVICE_NAME "\",\"model\":\"" HA_MODEL "\",\"manufacturer\":\"" HA_MANUFACTURER "\"}}"
|
||||
#define AVAIL ",\"availability_topic\":\"" MQTT_TOPIC_STATUS "\",\"payload_available\":\"online\",\"payload_not_available\":\"offline\""
|
||||
#define HA_TOPIC(sensor) HA_DISCOVERY_PREFIX "/sensor/" HA_DEVICE_ID "/" sensor "/config"
|
||||
#define HA_UID(entity) HA_UNIQUE_PREFIX "_" entity
|
||||
|
||||
struct { const char* topic; const char* payload; } sensors[] = {
|
||||
{
|
||||
"homeassistant/sensor/victron_smartsolar_garage/battery_voltage/config",
|
||||
"{\"unique_id\":\"victron_mppt_bv\",\"name\":\"Battery Voltage\","
|
||||
HA_TOPIC("battery_voltage"),
|
||||
"{\"unique_id\":\"" HA_UID("bv") "\",\"name\":\"Battery Voltage\","
|
||||
"\"state_topic\":\"" MQTT_TOPIC_DATA "\","
|
||||
"\"value_template\":\"{{ value_json.battery_voltage | float }}\","
|
||||
"\"unit_of_measurement\":\"V\",\"device_class\":\"voltage\","
|
||||
"\"state_class\":\"measurement\"" AVAIL DEV
|
||||
},
|
||||
{
|
||||
"homeassistant/sensor/victron_smartsolar_garage/battery_current/config",
|
||||
"{\"unique_id\":\"victron_mppt_bc\",\"name\":\"Battery Current\","
|
||||
HA_TOPIC("battery_current"),
|
||||
"{\"unique_id\":\"" HA_UID("bc") "\",\"name\":\"Battery Current\","
|
||||
"\"state_topic\":\"" MQTT_TOPIC_DATA "\","
|
||||
"\"value_template\":\"{{ value_json.battery_current | float }}\","
|
||||
"\"unit_of_measurement\":\"A\",\"device_class\":\"current\","
|
||||
"\"state_class\":\"measurement\"" AVAIL DEV
|
||||
},
|
||||
{
|
||||
"homeassistant/sensor/victron_smartsolar_garage/pv_power/config",
|
||||
"{\"unique_id\":\"victron_mppt_pv\",\"name\":\"PV Power\","
|
||||
HA_TOPIC("pv_power"),
|
||||
"{\"unique_id\":\"" HA_UID("pv") "\",\"name\":\"PV Power\","
|
||||
"\"state_topic\":\"" MQTT_TOPIC_DATA "\","
|
||||
"\"value_template\":\"{{ value_json.pv_power | float }}\","
|
||||
"\"unit_of_measurement\":\"W\",\"device_class\":\"power\","
|
||||
"\"state_class\":\"measurement\"" AVAIL DEV
|
||||
},
|
||||
{
|
||||
"homeassistant/sensor/victron_smartsolar_garage/yield_today/config",
|
||||
"{\"unique_id\":\"victron_mppt_yt\",\"name\":\"Yield Today\","
|
||||
HA_TOPIC("yield_today"),
|
||||
"{\"unique_id\":\"" HA_UID("yt") "\",\"name\":\"Yield Today\","
|
||||
"\"state_topic\":\"" MQTT_TOPIC_DATA "\","
|
||||
"\"value_template\":\"{{ value_json.yield_today_wh | float }}\","
|
||||
"\"unit_of_measurement\":\"Wh\",\"device_class\":\"energy\","
|
||||
"\"state_class\":\"total_increasing\"" AVAIL DEV
|
||||
},
|
||||
{
|
||||
"homeassistant/sensor/victron_smartsolar_garage/charge_state/config",
|
||||
"{\"unique_id\":\"victron_mppt_cs\",\"name\":\"Charge State\","
|
||||
HA_TOPIC("charge_state"),
|
||||
"{\"unique_id\":\"" HA_UID("cs") "\",\"name\":\"Charge State\","
|
||||
"\"state_topic\":\"" MQTT_TOPIC_DATA "\","
|
||||
"\"value_template\":\"{{ value_json.charge_state }}\","
|
||||
"\"icon\":\"mdi:solar-power\"" AVAIL DEV
|
||||
},
|
||||
{
|
||||
"homeassistant/sensor/victron_smartsolar_garage/error_code/config",
|
||||
"{\"unique_id\":\"victron_mppt_ec\",\"name\":\"Error Code\","
|
||||
HA_TOPIC("error_code"),
|
||||
"{\"unique_id\":\"" HA_UID("ec") "\",\"name\":\"Error Code\","
|
||||
"\"state_topic\":\"" MQTT_TOPIC_DATA "\","
|
||||
"\"value_template\":\"{{ value_json.error_code | int }}\","
|
||||
"\"icon\":\"mdi:alert-circle-outline\"" AVAIL DEV
|
||||
},
|
||||
{
|
||||
"homeassistant/sensor/victron_smartsolar_garage/external_load/config",
|
||||
"{\"unique_id\":\"victron_mppt_el\",\"name\":\"External Load\","
|
||||
HA_TOPIC("external_load"),
|
||||
"{\"unique_id\":\"" HA_UID("el") "\",\"name\":\"External Load\","
|
||||
"\"state_topic\":\"" MQTT_TOPIC_DATA "\","
|
||||
"\"value_template\":\"{{ value_json.external_load_a | float }}\","
|
||||
"\"unit_of_measurement\":\"A\",\"device_class\":\"current\","
|
||||
"\"state_class\":\"measurement\"" AVAIL DEV
|
||||
},
|
||||
{
|
||||
"homeassistant/sensor/victron_smartsolar_garage/external_load_power/config",
|
||||
"{\"unique_id\":\"victron_mppt_elw\",\"name\":\"External Load Power\","
|
||||
HA_TOPIC("external_load_power"),
|
||||
"{\"unique_id\":\"" HA_UID("elw") "\",\"name\":\"External Load Power\","
|
||||
"\"state_topic\":\"" MQTT_TOPIC_DATA "\","
|
||||
"\"value_template\":\"{{ value_json.external_load_w | float }}\","
|
||||
"\"unit_of_measurement\":\"W\",\"device_class\":\"power\","
|
||||
|
|
@ -275,8 +309,11 @@ static bool publishDiscovery() {
|
|||
|
||||
#undef DEV
|
||||
#undef AVAIL
|
||||
#undef HA_TOPIC
|
||||
#undef HA_UID
|
||||
|
||||
return allOk;
|
||||
#endif // HA_DISCOVERY_ENABLED
|
||||
}
|
||||
|
||||
// ── Data Publisher ────────────────────────────────────────────────────────────
|
||||
|
|
@ -477,7 +514,11 @@ void setup() {
|
|||
WiFi.persistent(false);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setAutoReconnect(true);
|
||||
#if WIFI_DISABLE_POWER_SAVE
|
||||
WiFi.setSleep(false); // modem sleep adds latency and drops pings
|
||||
#else
|
||||
WiFi.setSleep(true);
|
||||
#endif
|
||||
WiFi.onEvent(onWiFiEvent);
|
||||
|
||||
bleInit();
|
||||
|
|
|
|||
Loading…
Reference in New Issue