sensor: telemetrie z Chytré budky po drátě (BudkaLinkSensorManager)

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=<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 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-10-13 01:56:29 +02:00
1 parent 5ac50acfca
commit 895dbfdddd
5 files changed
+200

No files matched your search

@@ -0,0 +1,119 @@
// Compiled only into envs that wire a budka in (-D BUDKA_LINK_RX_PIN=<pin>);
// every other sensor env globs helpers/sensors too and must not see it.
#ifdef BUDKA_LINK_RX_PIN
#include "BudkaLinkSensorManager.h"
#include <stdlib.h>
#include <string.h>
#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 = &amp;
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
@@ -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;
};
+13
View File
@@ -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 =
+4
View File
@@ -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);
+7
View File
@@ -8,6 +8,9 @@
#include <helpers/AutoDiscoverRTCClock.h>
#include <helpers/ArduinoHelpers.h>
#include <helpers/sensors/EnvironmentSensorManager.h>
#ifdef BUDKA_LINK_RX_PIN
#include <helpers/sensors/BudkaLinkSensorManager.h>
#endif
#ifdef DISPLAY_CLASS
#include <helpers/ui/NullDisplayDriver.h>
@@ -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();