Add bridge

This commit is contained in:
grove5_admin committed 2026-04-29 13:46:24 -07:00
1 parent b18ffd6861
commit 3569811411
1 file changed
+91
@@ -0,0 +1,91 @@
/**
* cultivar_env_bridge.ino
* Grove5 Cultivar — ENV sensor bridge for Arduino UNO Q
*
* STM32U585 collects BME690 + SCD43 data and streams JSON
* payloads over a TCP bridge server to the Cultivar daemon
* running on the QRB2210 Debian side.
*
* Transport: BridgeTCPServer on localhost:5678
* Protocol: Newline-delimited JSON (one payload per line)
*
* Grove5 — Open source software, honest hardware.
* https://grove5.com
*/
#include <Wire.h>
#include <Arduino_RouterBridge.h>
#include "cultivar_env.h"
#define COLLECT_INTERVAL_MS (30UL * 1000UL) // 30s dev; use 300000 production
#define TCP_PORT 5678
#define JSON_BUF_SIZE 1024
IPAddress localhost(127, 0, 0, 1);
BridgeTCPServer<JSON_BUF_SIZE> server(Bridge, localhost, TCP_PORT);
CultivarEnv env;
static char jsonBuf[JSON_BUF_SIZE];
void setup() {
Monitor.begin();
Monitor.println("\n[cultivar] ENV bridge starting...");
if (!Bridge.begin()) {
Monitor.println("[ERROR] Bridge init failed");
while (true) {}
}
server.begin();
Monitor.println("[cultivar] TCP bridge ready on :5678");
// SDA=0, SCL=0 on Zephyr = device tree configured pins (correct)
if (!env.begin(Wire, SDA, SCL)) {
Monitor.println("[WARN] BME690 init failed — check wiring");
}
env.scanI2C();
Monitor.print("[cultivar] BME690=");
Monitor.print(env.bme_ok ? "OK" : "FAIL");
Monitor.print(" SCD43=");
Monitor.println(env.scd4x_ok ? "OK" : "absent");
}
void loop() {
static unsigned long lastCollect = 0;
static bool firstRun = true;
BridgeTCPClient<JSON_BUF_SIZE> client = server.accept();
if (!client.connected()) {
delay(1000);
return;
}
unsigned long waitMs = firstRun
? (env.scd4x_ok ? 35000UL : 5000UL)
: COLLECT_INTERVAL_MS;
if (millis() - lastCollect >= waitMs) {
lastCollect = millis();
firstRun = false;
CultivarEnvPayload payload;
if (env.collect(payload)) {
env.toJSON(payload, jsonBuf, sizeof(jsonBuf));
client.println(jsonBuf);
Monitor.println("[cultivar] payload sent");
} else {
Monitor.println("[WARN] collect failed");
}
}
if (env.scd4x_ok) {
CultivarCO2 co2;
env.pollCO2(co2);
}
delay(100);
}