Functional V0.1
This commit is contained in:
1 parent
0f2dac32f1
commit
072985f65c
2 files changed
+17
-21
No files matched your search
Binary file not shown.
@@ -18,26 +18,24 @@
|
|||||||
#include "cultivar_env.h"
|
#include "cultivar_env.h"
|
||||||
|
|
||||||
#define COLLECT_INTERVAL_MS (30UL * 1000UL) // 30s dev; use 300000 production
|
#define COLLECT_INTERVAL_MS (30UL * 1000UL) // 30s dev; use 300000 production
|
||||||
|
#define SCD43_WARMUP_MS (35UL * 1000UL)
|
||||||
#define TCP_PORT 5678
|
#define TCP_PORT 5678
|
||||||
#define JSON_BUF_SIZE 1024
|
#define JSON_BUF_SIZE 2048
|
||||||
|
|
||||||
IPAddress localhost(127, 0, 0, 1);
|
IPAddress localhost(127, 0, 0, 1);
|
||||||
BridgeTCPServer<JSON_BUF_SIZE> server(Bridge, localhost, TCP_PORT);
|
BridgeTCPServer<JSON_BUF_SIZE> server(Bridge, localhost, TCP_PORT);
|
||||||
|
|
||||||
CultivarEnv env;
|
CultivarEnv env;
|
||||||
static char jsonBuf[JSON_BUF_SIZE];
|
static char jsonBuf[JSON_BUF_SIZE];
|
||||||
|
static unsigned long lastCollect = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Bridge.begin();
|
Bridge.begin();
|
||||||
|
Monitor.println("[cultivar] ENV bridge starting...");
|
||||||
Monitor.println("\n[cultivar] ENV bridge starting...");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
server.begin();
|
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)) {
|
if (!env.begin(Wire1, SDA, SCL)) {
|
||||||
Monitor.println("[WARN] BME690 init failed — check wiring");
|
Monitor.println("[WARN] BME690 init failed — check wiring");
|
||||||
}
|
}
|
||||||
@@ -48,26 +46,27 @@ void setup() {
|
|||||||
Monitor.print(env.bme_ok ? "OK" : "FAIL");
|
Monitor.print(env.bme_ok ? "OK" : "FAIL");
|
||||||
Monitor.print(" SCD43=");
|
Monitor.print(" SCD43=");
|
||||||
Monitor.println(env.scd4x_ok ? "OK" : "absent");
|
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() {
|
void loop() {
|
||||||
static unsigned long lastCollect = 0;
|
// Always poll CO2 regardless of client state
|
||||||
static bool firstRun = true;
|
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()) {
|
||||||
delay(1000);
|
delay(100);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long waitMs = firstRun
|
if (millis() - lastCollect >= COLLECT_INTERVAL_MS) {
|
||||||
? (env.scd4x_ok ? 35000UL : 5000UL)
|
|
||||||
: COLLECT_INTERVAL_MS;
|
|
||||||
|
|
||||||
if (millis() - lastCollect >= waitMs) {
|
|
||||||
lastCollect = millis();
|
lastCollect = millis();
|
||||||
firstRun = false;
|
|
||||||
|
|
||||||
CultivarEnvPayload payload;
|
CultivarEnvPayload payload;
|
||||||
if (env.collect(payload)) {
|
if (env.collect(payload)) {
|
||||||
@@ -77,12 +76,9 @@ void loop() {
|
|||||||
} else {
|
} else {
|
||||||
Monitor.println("[WARN] collect failed");
|
Monitor.println("[WARN] collect failed");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (env.scd4x_ok) {
|
client.stop();
|
||||||
CultivarCO2 co2;
|
|
||||||
env.pollCO2(co2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user