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
+37 -28

No files matched your search

+5 -10
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\":{"); n += snprintf(buf + n, len - n,
if (p.ambient.valid) { "\"ambient\":{\"temp_c\":%.2f,\"humidity_pct\":%.2f,\"pressure_hpa\":%.2f,\"valid\":%s},",
n += snprintf(buf + n, len - n, p.ambient.temperature, p.ambient.humidity, p.ambient.pressure,
"\"temp_c\":%.2f,\"humidity_pct\":%.2f,\"pressure_hpa\":%.2f,\"valid\":true", p.ambient.valid ? "true" : "false");
p.ambient.temperature, p.ambient.humidity, p.ambient.pressure);
} else {
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) {
+32 -18
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,20 +61,38 @@ void loop() {
return; return;
} }
if (millis() - lastCollect >= COLLECT_INTERVAL_MS) { Monitor.println("[cultivar] daemon connected");
lastCollect = millis();
CultivarEnvPayload payload; // Keep the connection alive across multiple collection cycles.
if (env.collect(payload)) { // The daemon uses readline() in a persistent loop — it expects
env.toJSON(payload, jsonBuf, sizeof(jsonBuf)); // multiple newline-delimited payloads on a single connection.
client.println(jsonBuf); // We never call client.stop() here; the daemon owns the lifecycle.
Monitor.println("[cultivar] payload sent"); // If the daemon disconnects, client.connected() goes false and we
} else { // fall back to server.accept() on the next outer loop iteration.
Monitor.println("[WARN] collect failed"); while (client.connected()) {
if (env.scd4x_ok) {
CultivarCO2 co2;
env.pollCO2(co2);
} }
client.stop(); if (millis() - lastCollect >= COLLECT_INTERVAL_MS) {
lastCollect = millis();
CultivarEnvPayload payload;
if (env.collect(payload)) {
env.toJSON(payload, jsonBuf, sizeof(jsonBuf));
client.println(jsonBuf);
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 {
Monitor.println("[WARN] collect failed — BME690 read returned false");
}
}
delay(100);
} }
delay(100); Monitor.println("[cultivar] daemon disconnected — will re-accept");
} }