always emit ambient fields in JSON and keep TCP connection alive for multiple payloads
This commit is contained in:
1 parent
ed39be0412
commit
6fcc4a0d80
2 files changed
+37
-28
No files matched your search
@@ -36,8 +36,10 @@ void setup() {
|
||||
server.begin();
|
||||
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)) {
|
||||
Monitor.println("[WARN] BME690 init failed — check wiring");
|
||||
Monitor.println("[WARN] BME690 init failed — check wiring/address/I2C bus");
|
||||
}
|
||||
|
||||
env.scanI2C();
|
||||
@@ -52,12 +54,6 @@ void setup() {
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (!client.connected()) {
|
||||
@@ -65,20 +61,38 @@ void loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (millis() - lastCollect >= COLLECT_INTERVAL_MS) {
|
||||
lastCollect = millis();
|
||||
Monitor.println("[cultivar] daemon connected");
|
||||
|
||||
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");
|
||||
// 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);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
Reference in new issue
Block a user