always emit ambient fields in JSON and keep TCP connection alive for multiple payloads

This commit is contained in:
grove5_admin committed 2026-04-29 22:50:18 -07:00
1 parent ed39be0412
commit 6fcc4a0d80
2 files changed
+29 -20

No files matched your search

+4 -9
View File
@@ -253,16 +253,11 @@ size_t CultivarEnv::toJSON(const CultivarEnvPayload &p, char *buf, size_t len) {
n += snprintf(buf + n, len - n, "\"chamber_id\":\"%s\",", _chamber_id); n += snprintf(buf + n, len - n, "\"chamber_id\":\"%s\",", _chamber_id);
n += snprintf(buf + n, len - n, "\"ts\":%lu,", p.timestamp_ms); n += snprintf(buf + n, len - n, "\"ts\":%lu,", p.timestamp_ms);
// Ambient — full field names // Ambient — always emit all fields; daemon model requires temp_c/humidity_pct/pressure_hpa
n += snprintf(buf + n, len - n, "\"ambient\":{");
if (p.ambient.valid) {
n += snprintf(buf + n, len - n, n += snprintf(buf + n, len - n,
"\"temp_c\":%.2f,\"humidity_pct\":%.2f,\"pressure_hpa\":%.2f,\"valid\":true", "\"ambient\":{\"temp_c\":%.2f,\"humidity_pct\":%.2f,\"pressure_hpa\":%.2f,\"valid\":%s},",
p.ambient.temperature, p.ambient.humidity, p.ambient.pressure); p.ambient.temperature, p.ambient.humidity, p.ambient.pressure,
} else { p.ambient.valid ? "true" : "false");
n += snprintf(buf + n, len - n, "\"valid\":false");
}
n += snprintf(buf + n, len - n, "},");
// CO2 — full field names, null if not present // CO2 — full field names, null if not present
if (p.co2.valid) { if (p.co2.valid) {
+25 -11
View File
@@ -36,8 +36,10 @@ void setup() {
server.begin(); server.begin();
Monitor.println("[cultivar] TCP server ready on :5678"); Monitor.println("[cultivar] TCP server ready on :5678");
Wire1.begin(); // explicit begin before setClock
Wire1.setClock(100000); // 100 kHz — safer than default 400 kHz on longer runs
if (!env.begin(Wire1, SDA, SCL)) { if (!env.begin(Wire1, SDA, SCL)) {
Monitor.println("[WARN] BME690 init failed — check wiring"); Monitor.println("[WARN] BME690 init failed — check wiring/address/I2C bus");
} }
env.scanI2C(); env.scanI2C();
@@ -52,12 +54,6 @@ void setup() {
} }
void loop() { void loop() {
// Always poll CO2 regardless of client state
if (env.scd4x_ok) {
CultivarCO2 co2;
env.pollCO2(co2);
}
BridgeTCPClient<JSON_BUF_SIZE> client = server.accept(); BridgeTCPClient<JSON_BUF_SIZE> client = server.accept();
if (!client.connected()) { if (!client.connected()) {
@@ -65,6 +61,20 @@ void loop() {
return; return;
} }
Monitor.println("[cultivar] daemon connected");
// Keep the connection alive across multiple collection cycles.
// The daemon uses readline() in a persistent loop — it expects
// multiple newline-delimited payloads on a single connection.
// We never call client.stop() here; the daemon owns the lifecycle.
// If the daemon disconnects, client.connected() goes false and we
// fall back to server.accept() on the next outer loop iteration.
while (client.connected()) {
if (env.scd4x_ok) {
CultivarCO2 co2;
env.pollCO2(co2);
}
if (millis() - lastCollect >= COLLECT_INTERVAL_MS) { if (millis() - lastCollect >= COLLECT_INTERVAL_MS) {
lastCollect = millis(); lastCollect = millis();
@@ -72,13 +82,17 @@ void loop() {
if (env.collect(payload)) { if (env.collect(payload)) {
env.toJSON(payload, jsonBuf, sizeof(jsonBuf)); env.toJSON(payload, jsonBuf, sizeof(jsonBuf));
client.println(jsonBuf); client.println(jsonBuf);
Monitor.println("[cultivar] payload sent"); Monitor.print("[cultivar] payload sent — ");
Monitor.print("T="); Monitor.print(payload.ambient.temperature, 1);
Monitor.print("C RH="); Monitor.print(payload.ambient.humidity, 1);
Monitor.println("%");
} else { } else {
Monitor.println("[WARN] collect failed"); Monitor.println("[WARN] collect failed — BME690 read returned false");
} }
client.stop();
} }
delay(100); delay(100);
}
Monitor.println("[cultivar] daemon disconnected — will re-accept");
} }