commit 8de1704adcee014b8de4221d3fbead0c9d88f9ef Author: MrSossidge Date: Sun May 17 20:32:53 2026 +0100 Working Victron SmartSolar BLE to MQTT bridge with HA discovery diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9015bfe --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +include/config.h \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/include/example.config.h b/include/example.config.h new file mode 100644 index 0000000..1b93f83 --- /dev/null +++ b/include/example.config.h @@ -0,0 +1,33 @@ +#pragma once + +// ── WiFi ────────────────────────────────────────────────────────────────────── +#define WIFI_SSID "your_wifi_ssid" +#define WIFI_PASSWORD "your_wifi_password" + +// ── MQTT broker ─────────────────────────────────────────────────────────────── +#define MQTT_HOST "192.168.1.x" // IP address of your MQTT broker +#define MQTT_PORT 1883 +#define MQTT_CLIENT_ID "esp32-victron" +// Leave blank if your broker has no auth: +#define MQTT_USER "" +#define MQTT_PASSWORD "" + +// ── MQTT topics ─────────────────────────────────────────────────────────────── +#define MQTT_TOPIC_DATA "solar/mppt/data" +#define MQTT_TOPIC_STATUS "solar/mppt/status" + +// ── Victron SmartSolar MPPT ─────────────────────────────────────────────────── +// MAC address of your Victron device (lowercase, colon-separated) +// Find it in Victron Connect app → device → top-right menu → Product Info +#define VICTRON_MAC "xx:xx:xx:xx:xx:xx" + +// 32-character hex encryption key +// Find it in Victron Connect app → device → top-right menu → Product Info → Instant readout details +#define VICTRON_ENC_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +// ── Timing ──────────────────────────────────────────────────────────────────── +// How often to publish to MQTT (milliseconds) +#define PUBLISH_INTERVAL_MS 10000 + +// BLE scan window (seconds) +#define BLE_SCAN_DURATION_S 5 diff --git a/include/victron_ble.h b/include/victron_ble.h new file mode 100644 index 0000000..499ae6e --- /dev/null +++ b/include/victron_ble.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include +#include + +struct MpptData { + float batteryVoltage; // V (0.01V resolution) + float batteryCurrent; // A (0.1A resolution, positive = charging) + float pvPower; // W (1W resolution) + float yieldToday; // Wh (10Wh resolution) + float pvVoltage; // V (not in MPPT advert, always 0) + uint8_t chargeState; // 0=off 3=bulk 4=absorption 5=float etc + uint8_t errorCode; // 0 = no error + bool valid; // false if decryption/parse failed +}; + +const char* chargeStateName(uint8_t state); + +MpptData parseVictronMppt(const uint8_t* encKey, const uint8_t* data, size_t len); \ No newline at end of file diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..b9a5111 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,15 @@ +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino + +lib_deps = + h2zero/NimBLE-Arduino @ ^1.4.2 + knolleary/PubSubClient @ ^2.8.0 + bblanchon/ArduinoJson @ ^6.21.0 + +monitor_speed = 115200 +upload_speed = 921600 + +build_flags = + -DCORE_DEBUG_LEVEL=0 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..85565c3 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include "config.h" +#include "victron_ble.h" + +// ── Globals ────────────────────────────────────────────────────────────────── + +static uint8_t s_encKey[16]; + +// Shared between BLE core (1) and MQTT core (0) +static volatile bool s_newData = false; +static MpptData s_latest = {}; + +// MQTT + WiFi — only touched by core 0 task +static WiFiClient s_wifiClient; +static PubSubClient s_mqtt(s_wifiClient); +static bool s_discoveryPublished = false; + +// ── Helpers ────────────────────────────────────────────────────────────────── + +static bool hexToBin(const char* hex, uint8_t* out, size_t outLen) { + if (strlen(hex) != outLen * 2) return false; + for (size_t i = 0; i < outLen; i++) { + auto v = [](char c) -> int { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; + }; + int h = v(hex[i*2]), l = v(hex[i*2+1]); + if (h < 0 || l < 0) return false; + out[i] = (uint8_t)((h << 4) | l); + } + return true; +} + +// ── WiFi ───────────────────────────────────────────────────────────────────── + +static void wifiConnect() { + Serial.printf("[WiFi] Connecting to %s", WIFI_SSID); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + int attempts = 0; + while (WiFi.status() != WL_CONNECTED && attempts < 20) { + delay(500); + Serial.print("."); + attempts++; + } + if (WiFi.status() == WL_CONNECTED) { + Serial.printf("\n[WiFi] Connected, IP: %s\n", WiFi.localIP().toString().c_str()); + } else { + Serial.println("\n[WiFi] Failed, will retry"); + } +} + +// ── MQTT ───────────────────────────────────────────────────────────────────── + +static void mqttConnect() { + while (!s_mqtt.connected()) { + Serial.print("[MQTT] Connecting..."); + bool ok; + if (strlen(MQTT_USER) > 0) { + ok = s_mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD, + MQTT_TOPIC_STATUS, 0, true, "offline"); + } else { + ok = s_mqtt.connect(MQTT_CLIENT_ID, nullptr, nullptr, + MQTT_TOPIC_STATUS, 0, true, "offline"); + } + if (ok) { + Serial.println(" connected"); + s_mqtt.publish(MQTT_TOPIC_STATUS, "online", true); + s_discoveryPublished = false; + } else { + Serial.printf(" failed (rc=%d), retry in 5s\n", s_mqtt.state()); + delay(5000); + } + } +} + +// ── Home Assistant Discovery ────────────────────────────────────────────────── +// Payloads are hardcoded strings in flash — no runtime String building, +// no buffer size surprises. Each is well under 600 bytes total. + +static void 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 AVAIL ",\"availability_topic\":\"" MQTT_TOPIC_STATUS "\",\"payload_available\":\"online\",\"payload_not_available\":\"offline\"" + + struct { const char* topic; const char* payload; } sensors[] = { + { + "homeassistant/sensor/victron_smartsolar_garage/battery_voltage/config", + "{\"unique_id\":\"victron_mppt_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\"," + "\"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\"," + "\"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\"," + "\"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\"," + "\"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\"," + "\"state_topic\":\"" MQTT_TOPIC_DATA "\"," + "\"value_template\":\"{{ value_json.error_code | int }}\"," + "\"icon\":\"mdi:alert-circle-outline\"" AVAIL DEV + }, + }; + + for (auto& s : sensors) { + bool ok = s_mqtt.publish(s.topic, s.payload, true); + Serial.printf("[HA] %s: %s (%d bytes)\n", + ok ? "OK" : "FAIL", s.topic, strlen(s.payload)); + delay(50); + } + + s_discoveryPublished = true; + Serial.println("[HA] Discovery complete"); + + #undef DEV + #undef AVAIL +} + +// ── Data Publisher ──────────────────────────────────────────────────────────── + +static void publishData(const MpptData& d) { + StaticJsonDocument<256> doc; + doc["battery_voltage"] = serialized(String(d.batteryVoltage, 2)); + doc["battery_current"] = serialized(String(d.batteryCurrent, 2)); + doc["pv_power"] = serialized(String(d.pvPower, 1)); + doc["yield_today_wh"] = serialized(String(d.yieldToday, 0)); + doc["charge_state"] = chargeStateName(d.chargeState); + doc["charge_state_raw"] = d.chargeState; + doc["error_code"] = d.errorCode; + + char buf[256]; + serializeJson(doc, buf); + s_mqtt.publish(MQTT_TOPIC_DATA, buf, false); + Serial.printf("[MQTT] Published: %s\n", buf); +} + +// ── MQTT Task (runs on Core 0) ──────────────────────────────────────────────── + +void mqttTask(void* param) { + delay(1000); + + // Buffer must be set BEFORE connect — 620 bytes covers our largest payload + s_mqtt.setBufferSize(620); + s_mqtt.setServer(MQTT_HOST, MQTT_PORT); + s_mqtt.setKeepAlive(60); + + wifiConnect(); + mqttConnect(); + + for (;;) { + if (WiFi.status() != WL_CONNECTED) wifiConnect(); + if (!s_mqtt.connected()) mqttConnect(); + + s_mqtt.loop(); + + if (!s_discoveryPublished) publishDiscovery(); + + if (s_newData) { + s_newData = false; + publishData(s_latest); + } + + delay(100); + } +} + +// ── BLE Advertised Device Callback (fires on Core 1) ───────────────────────── + +class VictronAdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks { + void onResult(NimBLEAdvertisedDevice* device) override { + NimBLEAddress targetAddr(VICTRON_MAC); + if (device->getAddress() != targetAddr) return; + if (!device->haveManufacturerData()) return; + + std::string mfr = device->getManufacturerData(); + if (mfr.size() < 6 || (uint8_t)mfr[0] != 0xE1 || (uint8_t)mfr[1] != 0x02) return; + + const uint8_t* payload = (const uint8_t*)mfr.data() + 2; + size_t payloadLen = mfr.size() - 2; + + MpptData parsed = parseVictronMppt(s_encKey, payload, payloadLen); + if (parsed.valid) { + s_latest = parsed; + s_newData = true; + Serial.println("[BLE] Valid Victron packet decoded"); + } + } +}; + +// ── BLE Scanner (runs on Core 1 via loop()) ─────────────────────────────────── + +static NimBLEScan* s_scan = nullptr; + +static void bleInit() { + NimBLEDevice::init("esp32-victron"); + s_scan = NimBLEDevice::getScan(); + s_scan->setAdvertisedDeviceCallbacks(new VictronAdvertisedDeviceCallbacks(), true); + s_scan->setActiveScan(false); + s_scan->setInterval(100); + s_scan->setWindow(99); + Serial.println("[BLE] Scanner initialised"); +} + +// ── Setup & Loop ───────────────────────────────────────────────────────────── + +void setup() { + Serial.begin(115200); + delay(500); + Serial.println("\n[Boot] Victron ESP32 MQTT bridge starting"); + + if (!hexToBin(VICTRON_ENC_KEY, s_encKey, 16)) { + Serial.println("[ERROR] Bad encryption key — must be 32 hex chars"); + while (true) delay(1000); + } + Serial.println("[Boot] Config OK"); + + xTaskCreatePinnedToCore(mqttTask, "mqttTask", 8192, NULL, 1, NULL, 0); + bleInit(); +} + +void loop() { + Serial.println("[BLE] Scanning..."); + s_scan->start(BLE_SCAN_DURATION_S, false); + s_scan->clearResults(); + delay(PUBLISH_INTERVAL_MS); +} \ No newline at end of file diff --git a/src/victron_ble.cpp b/src/victron_ble.cpp new file mode 100644 index 0000000..d6b9934 --- /dev/null +++ b/src/victron_ble.cpp @@ -0,0 +1,150 @@ +#include "victron_ble.h" +#include +#include +#include +#include + +// --------------------------------------------------------------------------- +// Victron advertisement manufacturer data layout (after 2-byte company ID): +// +// Byte 0-1: prefix (uint16 LE) +// Byte 2-3: model_id (uint16 LE) +// Byte 4: readout_type +// Byte 5-6: IV / nonce (uint16 LE) +// Byte 7: key check byte (must equal encKey[0]) +// Byte 8+: AES-128-CTR encrypted payload +// +// AES-128-CTR: 128-bit little-endian counter, initial value = nonce (uint16) +// Matches Python: Counter.new(128, initial_value=iv, little_endian=True) +// --------------------------------------------------------------------------- + +static bool aes128ctrDecrypt(const uint8_t key[16], uint16_t nonce, + const uint8_t* in, uint8_t* out, size_t len) { + // 16-byte little-endian IV — nonce in bytes 0-1, rest zero + uint8_t iv[16] = {0}; + iv[0] = nonce & 0xFF; + iv[1] = (nonce >> 8) & 0xFF; + + mbedtls_aes_context ctx; + mbedtls_aes_init(&ctx); + if (mbedtls_aes_setkey_enc(&ctx, key, 128) != 0) { + mbedtls_aes_free(&ctx); + return false; + } + size_t nc_off = 0; + uint8_t stream_block[16] = {0}; + int ret = mbedtls_aes_crypt_ctr(&ctx, len, &nc_off, iv, stream_block, in, out); + mbedtls_aes_free(&ctx); + return ret == 0; +} + +// --------------------------------------------------------------------------- +// BitReader — mirrors Python BitReader exactly, reads fields LSB first +// --------------------------------------------------------------------------- + +struct BitReader { + const uint8_t* data; + size_t len; + size_t bitPos; + + BitReader(const uint8_t* d, size_t l) : data(d), len(l), bitPos(0) {} + + uint32_t readUnsigned(int bits) { + uint32_t result = 0; + for (int i = 0; i < bits; i++) { + size_t byteIdx = bitPos / 8; + size_t bitIdx = bitPos % 8; + if (byteIdx < len) { + result |= (uint32_t)((data[byteIdx] >> bitIdx) & 1) << i; + } + bitPos++; + } + return result; + } + + int32_t readSigned(int bits) { + uint32_t raw = readUnsigned(bits); + if (raw & (1u << (bits - 1))) { + raw |= ~((1u << bits) - 1); + } + return (int32_t)raw; + } +}; + +// --------------------------------------------------------------------------- +// SmartSolar MPPT decrypted payload (matches Python solar_charger.py exactly) +// +// 8 bits: charge_state +// 8 bits: charger_error +// 16 bits: battery_voltage (signed, /100 → V) +// 16 bits: battery_current (signed, /10 → A) +// 16 bits: yield_today (unsigned, x10 → Wh) +// 16 bits: solar_power (unsigned, 1W/bit) +// 9 bits: external_device_load (unsigned, /10 → A, not published) +// --------------------------------------------------------------------------- + +MpptData parseVictronMppt(const uint8_t* encKey, const uint8_t* data, size_t len) { + MpptData result = {}; + result.valid = false; + + // Need at least 9 bytes: 5 header + 2 nonce + 1 key check + 1 encrypted + if (len < 9) return result; + + uint16_t nonce = (uint16_t)data[5] | ((uint16_t)data[6] << 8); + uint8_t keyCheck = data[7]; + const uint8_t* encrypted = data + 8; + size_t encLen = len - 8; + + // Key check: first byte must equal encKey[0] + if (keyCheck != encKey[0]) { + Serial.printf("[BLE] Key check failed: got 0x%02X expected 0x%02X\n", + keyCheck, encKey[0]); + return result; + } + + uint8_t decrypted[32] = {0}; + if (!aes128ctrDecrypt(encKey, nonce, encrypted, decrypted, encLen)) { + Serial.println("[BLE] AES decryption failed"); + return result; + } + + BitReader reader(decrypted, encLen); + + uint32_t chargeState = reader.readUnsigned(8); + uint32_t chargerError = reader.readUnsigned(8); + int32_t batV_raw = reader.readSigned(16); + int32_t batI_raw = reader.readSigned(16); + uint32_t yieldToday = reader.readUnsigned(16); + uint32_t solarPower = reader.readUnsigned(16); + + result.chargeState = (uint8_t)chargeState; + result.errorCode = (uint8_t)chargerError; + result.batteryVoltage = (batV_raw != 0x7FFF) ? batV_raw / 100.0f : 0.0f; + result.batteryCurrent = (batI_raw != 0x7FFF) ? batI_raw / 10.0f : 0.0f; + result.yieldToday = (yieldToday != 0xFFFF) ? yieldToday * 10.0f : 0.0f; + result.pvPower = (solarPower != 0xFFFF) ? (float)solarPower : 0.0f; + result.pvVoltage = 0.0f; + result.valid = true; + + return result; +} + +const char* chargeStateName(uint8_t state) { + switch (state) { + case 0: return "off"; + case 2: return "fault"; + case 3: return "bulk"; + case 4: return "absorption"; + case 5: return "float"; + case 6: return "storage"; + case 7: return "equalize"; + case 245: return "starting"; + case 246: return "repeated_absorption"; + case 247: return "recondition"; + case 248: return "battery_safe"; + case 249: return "active"; + case 252: return "external_control"; + case 255: return "not_available"; + default: return "unknown"; + } +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html