From 895dbfdddd4b5d403383260a221440bde7f46c4b Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 13 Oct 2026 01:56:29 +0200 Subject: [PATCH] =?UTF-8?q?sensor:=20telemetrie=20z=20Chytr=C3=A9=20budky?= =?UTF-8?q?=20po=20dr=C3=A1t=C4=9B=20(BudkaLinkSensorManager)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Budka posílá každých ~30 s po jednosměrném UARTu řádek `CB1 id=… t= h= p= v= i= w= wh= mot= up=…*XX` (NMEA XOR součet). BudkaLinkSensorManager je EnvironmentSensorManager + Serial1 RX: řádek se špatným součtem zahodí celý, hodnoty starší než 2 min do telemetrie nedává. Všechno z budky jde na jeden LPP kanál za čidly na desce; výkon jako analog input (LPP_POWER má rozlišení 1 W). TX UARTu je LED_GREEN — nic se nevysílá a klidová jednička ji drží zhasnutou. Zapíná se jen -D BUDKA_LINK_RX_PIN=; bez něj se nic nemění. Nový env Xiao_nrf52_sensor (upstream pro XIAO nRF sensor neměl). Co-Authored-By: Claude Opus 5.5 --- .../sensors/BudkaLinkSensorManager.cpp | 119 ++++++++++++++++++ src/helpers/sensors/BudkaLinkSensorManager.h | 57 +++++++++ variants/xiao_nrf52/platformio.ini | 13 ++ variants/xiao_nrf52/target.cpp | 4 + variants/xiao_nrf52/target.h | 7 ++ 5 files changed, 200 insertions(+) create mode 100644 src/helpers/sensors/BudkaLinkSensorManager.cpp create mode 100644 src/helpers/sensors/BudkaLinkSensorManager.h diff --git a/src/helpers/sensors/BudkaLinkSensorManager.cpp b/src/helpers/sensors/BudkaLinkSensorManager.cpp new file mode 100644 index 00000000..1ba6b968 --- /dev/null +++ b/src/helpers/sensors/BudkaLinkSensorManager.cpp @@ -0,0 +1,119 @@ +// Compiled only into envs that wire a budka in (-D BUDKA_LINK_RX_PIN=); +// every other sensor env globs helpers/sensors too and must not see it. +#ifdef BUDKA_LINK_RX_PIN + +#include "BudkaLinkSensorManager.h" + +#include +#include + +#ifndef BUDKA_LINK_TX_PIN + #define BUDKA_LINK_TX_PIN LED_GREEN +#endif + +bool BudkaLinkSensorManager::begin() { + bool ok = EnvironmentSensorManager::begin(); + Serial1.setPins(BUDKA_LINK_RX_PIN, BUDKA_LINK_TX_PIN); + Serial1.begin(BUDKA_LINK_BAUD); + MESH_DEBUG_PRINTLN("budka link: RX on pin %d at %d baud", BUDKA_LINK_RX_PIN, BUDKA_LINK_BAUD); + return ok; +} + +bool BudkaLinkSensorManager::fresh(const Val& x) const { + return x.at != 0 && (millis() - x.at) < BUDKA_LINK_STALE_MS; +} + +void BudkaLinkSensorManager::loop() { + EnvironmentSensorManager::loop(); + while (Serial1.available()) { + char c = (char)Serial1.read(); + if (c == '\n' || c == '\r') { + if (len > 0) { + line[len] = 0; + handleLine(); + } + len = 0; + } else if (len < sizeof(line) - 1) { + line[len++] = c; + } else { + len = 0; // overlong = garbage on the wire; resync on the next newline + } + } +} + +static int hexval(char c) { + 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; +} + +void BudkaLinkSensorManager::handleLine() { + if (strncmp(line, "CB1 ", 4) != 0) return; + char* star = strrchr(line, '*'); + if (!star || strlen(star) != 3) return; + int hi = hexval(star[1]), lo = hexval(star[2]); + if (hi < 0 || lo < 0) return; + uint8_t cs = 0; + for (char* q = line + 1; q < star; q++) cs ^= (uint8_t)*q; + if (cs != (uint8_t)((hi << 4) | lo)) { + MESH_DEBUG_PRINTLN("budka link: checksum mismatch, line dropped"); + return; + } + *star = 0; + + unsigned long now = millis(); + if (now == 0) now = 1; // 0 means "never" + for (char* tok = strtok(line + 4, " "); tok; tok = strtok(NULL, " ")) { + char* eq = strchr(tok, '='); + if (!eq) continue; + *eq = 0; + const char* k = tok; + const char* v = eq + 1; + Val* dst = NULL; + if (!strcmp(k, "t")) dst = &t; + else if (!strcmp(k, "h")) dst = &h; + else if (!strcmp(k, "p")) dst = &p; + else if (!strcmp(k, "v")) dst = &volt; + else if (!strcmp(k, "i")) dst = & + else if (!strcmp(k, "w")) dst = &watt; + else if (!strcmp(k, "wh")) dst = &wh; + else if (!strcmp(k, "mot")) { + mot = strtoul(v, NULL, 10); + mot_valid = true; + mot_at = now; + } + if (dst) { + dst->v = strtof(v, NULL); + dst->at = now; + } + } + last_line_ms = now; +} + +bool BudkaLinkSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) { + EnvironmentSensorManager::querySensors(requester_permissions, telemetry); + if (!(requester_permissions & TELEM_PERM_ENVIRONMENT)) return true; + + // Everything from the budka shares one channel: the first one after the + // on-board sensors. CayenneLPP allows one value per (channel, type). + uint8_t ch = next_available_channel; + bool any = false; + if (fresh(t)) { telemetry.addTemperature(ch, t.v); any = true; } + if (fresh(h)) { telemetry.addRelativeHumidity(ch, h.v); any = true; } + if (fresh(p)) { telemetry.addBarometricPressure(ch, p.v); any = true; } + if (fresh(volt)) { telemetry.addVoltage(ch, volt.v); any = true; } + if (fresh(amp)) { telemetry.addCurrent(ch, amp.v); any = true; } + // LPP_POWER is whole watts, unsigned — useless for a ~1 W box. Analog input + // carries 0.01 resolution. + if (fresh(watt)) { telemetry.addAnalogInput(ch, watt.v); any = true; } + if (fresh(wh)) { telemetry.addEnergy(ch, wh.v / 1000.0f); any = true; } // LPP energy is kWh + if (mot_valid && (millis() - mot_at) < BUDKA_LINK_STALE_MS) { + telemetry.addGenericSensor(ch, (float)mot); + any = true; + } + if (any) next_available_channel++; + return true; +} + +#endif // BUDKA_LINK_RX_PIN diff --git a/src/helpers/sensors/BudkaLinkSensorManager.h b/src/helpers/sensors/BudkaLinkSensorManager.h new file mode 100644 index 00000000..e86ac809 --- /dev/null +++ b/src/helpers/sensors/BudkaLinkSensorManager.h @@ -0,0 +1,57 @@ +#pragma once + +// Telemetry from a Chytra Budka (ESP32-S3 bird box) over a one-way UART line. +// +// The budka sends one NMEA-style line every ~30 s (firmware/main/mc_link.h in +// the chytra-budka repo): +// +// CB1 id=fbfe18 t=21.4 h=63 p=1013.2 v=5.04 i=0.221 w=1.11 wh=310.75 mot=3 up=1234*5A\r\n +// +// `*XX` is the XOR of every byte between the leading 'C' and the '*'. A line +// with a bad checksum or an unknown version is dropped whole. Keys the budka +// could not read are simply absent; a value not refreshed for +// BUDKA_LINK_STALE_MS is left out of the telemetry instead of being reported +// stale. +// +// Wiring: budka "mc_tx" pad -> BUDKA_LINK_RX_PIN, plus GND. The UART needs a TX +// pin too, but nothing is ever sent; BUDKA_LINK_TX_PIN defaults to the XIAO's +// LED_GREEN, which idles HIGH (= LED off) like an unused TX line does. + +#include "EnvironmentSensorManager.h" + +#ifndef BUDKA_LINK_BAUD + #define BUDKA_LINK_BAUD 115200 +#endif +#ifndef BUDKA_LINK_STALE_MS + #define BUDKA_LINK_STALE_MS (120UL * 1000UL) +#endif + +class BudkaLinkSensorManager : public EnvironmentSensorManager { +public: + bool begin() override; + bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override; + void loop() override; + + // Last motion count and the time it last increased (0 = never), so the + // app can raise an alert on new motion. + bool hasMotion() const { return mot_valid; } + uint32_t motionCount() const { return mot; } + unsigned long lastLineMillis() const { return last_line_ms; } + +private: + struct Val { + float v = 0; + unsigned long at = 0; // millis() of the last update, 0 = never + }; + Val t, h, p, volt, amp, watt, wh; + uint32_t mot = 0; + bool mot_valid = false; + unsigned long mot_at = 0; + unsigned long last_line_ms = 0; + + char line[200]; + size_t len = 0; + + void handleLine(); + bool fresh(const Val& x) const; +}; diff --git a/variants/xiao_nrf52/platformio.ini b/variants/xiao_nrf52/platformio.ini index 1076bd8d..b552f5e8 100644 --- a/variants/xiao_nrf52/platformio.ini +++ b/variants/xiao_nrf52/platformio.ini @@ -98,6 +98,19 @@ build_flags = build_src_filter = ${Xiao_nrf52.build_src_filter} +<../examples/simple_repeater/*.cpp> +[env:Xiao_nrf52_sensor] +extends = Xiao_nrf52 +build_flags = + ${Xiao_nrf52.build_flags} + -D ADVERT_NAME='"Xiao_nrf52 Sensor"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' +; -D BUDKA_LINK_RX_PIN=0 ; telemetry line from a Chytra Budka on D0 +; -D MESH_DEBUG=1 +build_src_filter = ${Xiao_nrf52.build_src_filter} + +<../examples/simple_sensor> + [env:Xiao_nrf52_room_server] extends = Xiao_nrf52 build_flags = diff --git a/variants/xiao_nrf52/target.cpp b/variants/xiao_nrf52/target.cpp index def1dbbe..70c2eae0 100644 --- a/variants/xiao_nrf52/target.cpp +++ b/variants/xiao_nrf52/target.cpp @@ -16,7 +16,11 @@ WRAPPER_CLASS radio_driver(radio, board); VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); +#ifdef BUDKA_LINK_RX_PIN +BudkaLinkSensorManager sensors; // on-board sensors + a Chytra Budka on Serial1 +#else EnvironmentSensorManager sensors; +#endif bool radio_init() { rtc_clock.begin(Wire); diff --git a/variants/xiao_nrf52/target.h b/variants/xiao_nrf52/target.h index 20093904..01bd2c95 100644 --- a/variants/xiao_nrf52/target.h +++ b/variants/xiao_nrf52/target.h @@ -8,6 +8,9 @@ #include #include #include +#ifdef BUDKA_LINK_RX_PIN + #include +#endif #ifdef DISPLAY_CLASS #include @@ -19,7 +22,11 @@ extern XiaoNrf52Board board; extern WRAPPER_CLASS radio_driver; extern AutoDiscoverRTCClock rtc_clock; +#ifdef BUDKA_LINK_RX_PIN +extern BudkaLinkSensorManager sensors; +#else extern EnvironmentSensorManager sensors; +#endif bool radio_init(); mesh::LocalIdentity radio_new_identity();