// 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, "mot")) { char* end; unsigned long m = strtoul(v, &end, 10); if (end != v) { mot = m; mot_valid = true; mot_at = now; } } else { // Meter fields: v i w wh, k = 1..BUDKA_LINK_METERS. size_t kl = strlen(k); int idx = (kl >= 2) ? k[kl - 1] - '1' : -1; if (idx >= 0 && idx < BUDKA_LINK_METERS) { if (kl == 2 && k[0] == 'v') dst = &volt[idx]; else if (kl == 2 && k[0] == 'i') dst = &[idx]; else if (kl == 2 && k[0] == 'w') dst = &watt[idx]; else if (kl == 3 && k[0] == 'w' && k[1] == 'h') dst = &wh[idx]; } } if (dst) { char* end; float f = strtof(v, &end); if (end != v) { // "v1=" with no number is not a reading dst->v = f; 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; // The box's own sensors share one channel (CayenneLPP allows one value per // (channel, type)); meter k gets the k-th channel after it. Channels are // kept even when a meter is momentarily missing, so a meter's numbers never // move to another channel between two requests. uint8_t ch = next_available_channel; if (fresh(t)) telemetry.addTemperature(ch, t.v); if (fresh(h)) telemetry.addRelativeHumidity(ch, h.v); if (fresh(p)) telemetry.addBarometricPressure(ch, p.v); if (mot_valid && (millis() - mot_at) < BUDKA_LINK_STALE_MS) telemetry.addGenericSensor(ch, (float)mot); for (int k = 0; k < BUDKA_LINK_METERS; k++) { uint8_t mch = ch + 1 + k; if (fresh(volt[k])) telemetry.addVoltage(mch, volt[k].v); if (fresh(amp[k])) telemetry.addCurrent(mch, amp[k].v); // LPP_POWER is whole watts, unsigned — useless for a ~1 W load. Analog // input carries 0.01 resolution. if (fresh(watt[k])) telemetry.addAnalogInput(mch, watt[k].v); if (fresh(wh[k])) telemetry.addEnergy(mch, wh[k].v / 1000.0f); // LPP energy is kWh } if (last_line_ms != 0) next_available_channel = ch + 1 + BUDKA_LINK_METERS; return true; } #endif // BUDKA_LINK_RX_PIN