send raw and formatted
This commit is contained in:
1 parent
9717ef5549
commit
d97b4a545a
5 files changed
+381
-164
No files matched your search
@@ -1,12 +1,15 @@
|
||||
/**
|
||||
* cultivar_env_bridge.ino
|
||||
* Grove5 Cultivar — ENV sensor bridge for Arduino UNO Q
|
||||
* Grove5 Cultivar — ENV sensor bridge
|
||||
*
|
||||
* STM32U585 collects BME690 + SCD43 data and streams JSON
|
||||
* payloads over a TCP bridge server to the Cultivar daemon
|
||||
* running on the QRB2210 Debian side.
|
||||
* Arduino UNO Q (Zephyr/STM32U585):
|
||||
* Streams JSON over BridgeTCPServer to the Cultivar daemon
|
||||
* running on the QRB2210 Debian side (localhost:5678).
|
||||
*
|
||||
* ESP32-S3 (debug / validation):
|
||||
* Logs JSON payloads to Serial — no network required.
|
||||
* Use to verify sensor readings independently of STM32 I2C issues.
|
||||
*
|
||||
* Transport: BridgeTCPServer on localhost:5678
|
||||
* Protocol: Newline-delimited JSON (one payload per line)
|
||||
*
|
||||
* Grove5 — Open source software, honest hardware.
|
||||
@@ -14,50 +17,61 @@
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <Arduino_RouterBridge.h>
|
||||
#include "cultivar_env.h"
|
||||
|
||||
#define COLLECT_INTERVAL_MS (30UL * 1000UL) // 30s dev; use 300000 production
|
||||
#define SCD43_WARMUP_MS (35UL * 1000UL)
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
#include <Arduino_RouterBridge.h>
|
||||
#define DBG Monitor
|
||||
#else
|
||||
#define DBG Serial
|
||||
#endif
|
||||
|
||||
#define COLLECT_INTERVAL_MS (5UL * 60UL * 1000UL) // 30s dev; use 300000 production
|
||||
#define SCD43_WARMUP_MS (60UL * 1000UL)
|
||||
#define TCP_PORT 5678
|
||||
#define JSON_BUF_SIZE 2048
|
||||
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
IPAddress localhost(127, 0, 0, 1);
|
||||
BridgeTCPServer<JSON_BUF_SIZE> server(Bridge, localhost, TCP_PORT);
|
||||
#endif
|
||||
|
||||
CultivarEnv env;
|
||||
static char jsonBuf[JSON_BUF_SIZE];
|
||||
static unsigned long lastCollect = 0;
|
||||
|
||||
void setup() {
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
Bridge.begin();
|
||||
delay(500); // let arduino-router finish boot chatter before we print
|
||||
Monitor.println("[cultivar] ENV bridge starting...");
|
||||
|
||||
server.begin();
|
||||
Monitor.println("[cultivar] TCP server ready on :5678");
|
||||
DBG.println("[cultivar] TCP server ready on :5678");
|
||||
#else
|
||||
Serial.begin(115200);
|
||||
#endif
|
||||
|
||||
Wire1.begin();
|
||||
Wire1.setClock(100000);
|
||||
DBG.println("[cultivar] ENV bridge starting...");
|
||||
|
||||
// I2C scan via Monitor (visible on Linux side) before sensor init
|
||||
Monitor.println("\n[cultivar] I2C scan on Wire1:");
|
||||
Wire1.begin(); // needed for I2C scan + chip probe before env.begin()
|
||||
|
||||
// I2C scan — confirms bus and devices before sensor init
|
||||
DBG.println("\n[cultivar] I2C scan on Wire1:");
|
||||
int i2c_found = 0;
|
||||
for (uint8_t addr = 1; addr < 127; addr++) {
|
||||
Wire1.beginTransmission(addr);
|
||||
if (Wire1.endTransmission() == 0) {
|
||||
Monitor.print(" 0x");
|
||||
if (addr < 0x10) Monitor.print("0");
|
||||
Monitor.print(addr, HEX);
|
||||
if (addr == 0x76 || addr == 0x77) Monitor.print(" <- BME690");
|
||||
if (addr == 0x62) Monitor.print(" <- SCD43");
|
||||
if (addr == 0x6B) Monitor.print(" <- IMU");
|
||||
Monitor.println();
|
||||
DBG.print(" 0x");
|
||||
if (addr < 0x10) DBG.print("0");
|
||||
DBG.print(addr, HEX);
|
||||
if (addr == 0x76 || addr == 0x77) DBG.print(" <- BME690");
|
||||
if (addr == 0x62) DBG.print(" <- SCD43");
|
||||
if (addr == 0x6B) DBG.print(" <- IMU");
|
||||
DBG.println();
|
||||
i2c_found++;
|
||||
}
|
||||
}
|
||||
if (!i2c_found) Monitor.println(" No devices found — check wiring");
|
||||
Monitor.println("[cultivar] I2C scan done");
|
||||
if (!i2c_found) DBG.println(" No devices found — check wiring");
|
||||
DBG.println("[cultivar] I2C scan done");
|
||||
|
||||
// Raw chip ID probe — BME690 register 0xD0 should return 0x61
|
||||
Wire1.beginTransmission(0x76);
|
||||
@@ -66,47 +80,36 @@ void setup() {
|
||||
Wire1.requestFrom((uint8_t)0x76, (uint8_t)1);
|
||||
if (Wire1.available()) {
|
||||
uint8_t chip_id = Wire1.read();
|
||||
Monitor.print("[cultivar] BME690 chip_id=0x");
|
||||
Monitor.print(chip_id, HEX);
|
||||
Monitor.println(chip_id == 0x61 ? " (OK)" : " (WRONG — expected 0x61)");
|
||||
DBG.print("[cultivar] BME690 chip_id=0x");
|
||||
DBG.print(chip_id, HEX);
|
||||
DBG.println(chip_id == 0x61 ? " (OK)" : " (WRONG — expected 0x61)");
|
||||
} else {
|
||||
Monitor.println("[cultivar] BME690 chip_id read failed — no data");
|
||||
DBG.println("[cultivar] BME690 chip_id read failed — no data");
|
||||
}
|
||||
|
||||
if (!env.begin(Wire1, SDA, SCL)) {
|
||||
Monitor.print("[WARN] BME690 init failed — bme_status=");
|
||||
Monitor.println(env.bme_status);
|
||||
DBG.print("[WARN] BME690 init failed — bme_status=");
|
||||
DBG.println(env.bme_status);
|
||||
}
|
||||
// env.begin() calls Wire1.begin() internally on Zephyr, resetting the clock.
|
||||
// Re-apply 100 kHz so all sensor reads use the safe rate.
|
||||
Wire1.setClock(100000);
|
||||
|
||||
Monitor.print("[cultivar] BME690=");
|
||||
Monitor.print(env.bme_ok ? "OK" : "FAIL");
|
||||
Monitor.print(" SCD43=");
|
||||
Monitor.println(env.scd4x_ok ? "OK" : "absent");
|
||||
DBG.print("[cultivar] BME690=");
|
||||
DBG.print(env.bme_ok ? "OK" : "FAIL");
|
||||
DBG.print(" SCD43=");
|
||||
DBG.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() {
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
// UNO Q: block until daemon connects, then stream payloads until disconnect
|
||||
BridgeTCPClient<JSON_BUF_SIZE> client = server.accept();
|
||||
|
||||
if (!client.connected()) {
|
||||
delay(100);
|
||||
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.
|
||||
if (!client.connected()) { delay(100); return; }
|
||||
DBG.println("[cultivar] daemon connected");
|
||||
while (client.connected()) {
|
||||
#endif
|
||||
|
||||
if (env.scd4x_ok) {
|
||||
CultivarCO2 co2;
|
||||
env.pollCO2(co2);
|
||||
@@ -118,20 +121,24 @@ void loop() {
|
||||
CultivarEnvPayload payload;
|
||||
if (env.collect(payload)) {
|
||||
env.toJSON(payload, jsonBuf, sizeof(jsonBuf));
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
client.println(jsonBuf);
|
||||
Monitor.print(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("%");
|
||||
#endif
|
||||
DBG.println(jsonBuf);
|
||||
DBG.print("[cultivar] payload sent — ");
|
||||
DBG.print("T="); DBG.print(payload.ambient.temperature, 1);
|
||||
DBG.print("C RH="); DBG.print(payload.ambient.humidity, 1);
|
||||
DBG.println("%");
|
||||
} else {
|
||||
Monitor.println("[WARN] collect failed — BME690 read returned false");
|
||||
DBG.println("[WARN] collect failed — BME690 read returned false");
|
||||
}
|
||||
}
|
||||
|
||||
delay(100);
|
||||
}
|
||||
|
||||
Monitor.println("[cultivar] daemon disconnected — restarting server");
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
}
|
||||
DBG.println("[cultivar] daemon disconnected — restarting server");
|
||||
server.begin();
|
||||
#endif
|
||||
}
|
||||
Reference in new issue
Block a user