Functional V0.1

This commit is contained in:
grove5_admin committed 2026-04-29 21:09:49 -07:00
1 parent 0f2dac32f1
commit 072985f65c
2 files changed
+17 -21

No files matched your search

+17 -21
View File
@@ -18,26 +18,24 @@
#include "cultivar_env.h"
#define COLLECT_INTERVAL_MS (30UL * 1000UL) // 30s dev; use 300000 production
#define SCD43_WARMUP_MS (35UL * 1000UL)
#define TCP_PORT 5678
#define JSON_BUF_SIZE 1024
#define JSON_BUF_SIZE 2048
IPAddress localhost(127, 0, 0, 1);
BridgeTCPServer<JSON_BUF_SIZE> server(Bridge, localhost, TCP_PORT);
CultivarEnv env;
static char jsonBuf[JSON_BUF_SIZE];
static unsigned long lastCollect = 0;
void setup() {
Bridge.begin();
Monitor.println("\n[cultivar] ENV bridge starting...");
Monitor.println("[cultivar] ENV bridge starting...");
server.begin();
Monitor.println("[cultivar] TCP bridge ready on :5678");
Monitor.println("[cultivar] TCP server ready on :5678");
// SDA=0, SCL=0 on Zephyr = device tree configured pins (correct)
if (!env.begin(Wire1, SDA, SCL)) {
Monitor.println("[WARN] BME690 init failed — check wiring");
}
@@ -48,26 +46,27 @@ void setup() {
Monitor.print(env.bme_ok ? "OK" : "FAIL");
Monitor.print(" SCD43=");
Monitor.println(env.scd4x_ok ? "OK" : "absent");
// Start warmup clock at boot, not at first client connect
lastCollect = millis() - COLLECT_INTERVAL_MS + (env.scd4x_ok ? SCD43_WARMUP_MS : 5000UL);
}
void loop() {
static unsigned long lastCollect = 0;
static bool firstRun = true;
// 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()) {
delay(1000);
delay(100);
return;
}
unsigned long waitMs = firstRun
? (env.scd4x_ok ? 35000UL : 5000UL)
: COLLECT_INTERVAL_MS;
if (millis() - lastCollect >= waitMs) {
if (millis() - lastCollect >= COLLECT_INTERVAL_MS) {
lastCollect = millis();
firstRun = false;
CultivarEnvPayload payload;
if (env.collect(payload)) {
@@ -77,12 +76,9 @@ void loop() {
} else {
Monitor.println("[WARN] collect failed");
}
}
if (env.scd4x_ok) {
CultivarCO2 co2;
env.pollCO2(co2);
client.stop();
}
delay(100);
}
}