Update URL and expand JSON field names to match Python model

This commit is contained in:
grove5_admin committed 2026-04-27 22:47:01 -07:00
1 parent 4a933c304b
commit b18ffd6861
1 file changed
+32 -31
+32 -31
View File
@@ -1,7 +1,7 @@
/**
* cultivar_env.cpp
* Grove5 — Open source software, honest hardware.
* https://grovefive.com
* https://grove5.com
*/
#include "cultivar_env.h"
@@ -50,7 +50,7 @@ bool CultivarEnv::begin(TwoWire &wire, uint8_t sda, uint8_t scl, uint8_t bme_add
bme_ok = _initBME690();
scd4x_ok = _initSCD4x();
return bme_ok; // SCD43 is optional
return bme_ok;
}
// ── _initBME690 ───────────────────────────────────────────────────────────
@@ -73,7 +73,6 @@ bool CultivarEnv::_initBME690() {
_dev.variant_id);
}
// Default sensor config
_conf.os_temp = BME69X_OS_2X;
_conf.os_pres = BME69X_OS_16X;
_conf.os_hum = BME69X_OS_1X;
@@ -98,9 +97,8 @@ bool CultivarEnv::_forcedRead(struct bme69x_data &data) {
int8_t r = bme69x_set_op_mode(BME69X_FORCED_MODE, &_dev);
if (r != BME69X_OK) return false;
// Wait for T/P/H measurement + heater duration
uint32_t meas_dur = bme69x_get_meas_dur(BME69X_FORCED_MODE, &_conf, &_dev);
uint32_t heat_dur = (uint32_t)_heatr.heatr_dur * 1000; // ms -> us
uint32_t heat_dur = (uint32_t)_heatr.heatr_dur * 1000;
_dev.delay_us(meas_dur + heat_dur + 5000, _dev.intf_ptr);
uint8_t n_data = 0;
@@ -112,7 +110,6 @@ bool CultivarEnv::_forcedRead(struct bme69x_data &data) {
bool CultivarEnv::_readAmbient(CultivarAmbient &out) {
out.valid = false;
// High oversampling, heater off
_conf.os_temp = BME69X_OS_8X;
_conf.os_pres = BME69X_OS_16X;
_conf.os_hum = BME69X_OS_4X;
@@ -126,7 +123,6 @@ bool CultivarEnv::_readAmbient(CultivarAmbient &out) {
r = bme69x_set_heatr_conf(BME69X_FORCED_MODE, &_heatr, &_dev);
if (r != BME69X_OK) return false;
// Settle — let residual heater heat dissipate
delay(CULTIVAR_AMBIENT_SETTLE_MS);
struct bme69x_data data;
@@ -134,7 +130,7 @@ bool CultivarEnv::_readAmbient(CultivarAmbient &out) {
out.temperature = data.temperature;
out.humidity = data.humidity;
out.pressure = data.pressure / 100.0f; // Pa → hPa
out.pressure = data.pressure / 100.0f;
out.valid = true;
return true;
}
@@ -174,7 +170,7 @@ void CultivarEnv::_computeHealth(CultivarSensorHealth &health,
} else {
health.temp_delta = 0.0f;
health.humidity_delta = 0.0f;
health.temp_ok = true; // can't check — not a failure
health.temp_ok = true;
health.humidity_ok = true;
}
}
@@ -183,7 +179,6 @@ void CultivarEnv::_computeHealth(CultivarSensorHealth &health,
bool CultivarEnv::_runSweep(CultivarGasPoint *out, uint8_t &count) {
count = 0;
// Restore standard oversampling for gas measurements
_conf.os_temp = BME69X_OS_2X;
_conf.os_pres = BME69X_OS_4X;
_conf.os_hum = BME69X_OS_1X;
@@ -217,7 +212,7 @@ bool CultivarEnv::_runSweep(CultivarGasPoint *out, uint8_t &count) {
out[i].valid = gas_valid;
count++;
delay(50); // brief cool-down between profiles
delay(50);
}
return count > 0;
@@ -233,7 +228,6 @@ bool CultivarEnv::collect(CultivarEnvPayload &payload) {
_computeHealth(payload.health, payload.ambient, payload.co2);
_runSweep(payload.sweep, payload.sweep_count);
// Update baseline sample counter
_baseline.samples_collected++;
payload.baseline = _baseline;
@@ -246,6 +240,8 @@ bool CultivarEnv::pollCO2(CultivarCO2 &out) {
}
// ── toJSON ────────────────────────────────────────────────────────────────
// Full field names match cultivar_types.py STM32Payload model exactly.
// No translation layer needed on the daemon side.
size_t CultivarEnv::toJSON(const CultivarEnvPayload &p, char *buf, size_t len) {
size_t n = 0;
@@ -253,48 +249,58 @@ 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, "\"ts\":%lu,", p.timestamp_ms);
// Ambient
// Ambient — full field names
n += snprintf(buf + n, len - n, "\"ambient\":{");
if (p.ambient.valid) {
n += snprintf(buf + n, len - n,
"\"temp\":%.2f,\"hum\":%.2f,\"pres\":%.2f,\"ok\":true",
"\"temp_c\":%.2f,\"humidity_pct\":%.2f,\"pressure_hpa\":%.2f,\"valid\":true",
p.ambient.temperature, p.ambient.humidity, p.ambient.pressure);
} else {
n += snprintf(buf + n, len - n, "\"ok\":false");
n += snprintf(buf + n, len - n, "\"valid\":false");
}
n += snprintf(buf + n, len - n, "},");
// CO2
n += snprintf(buf + n, len - n, "\"co2\":{");
// CO2 — full field names, null if not present
if (p.co2.valid) {
n += snprintf(buf + n, len - n,
"\"ppm\":%u,\"temp\":%.2f,\"hum\":%.2f,\"ok\":true",
"\"co2\":{\"ppm\":%u,\"temp_c\":%.2f,\"humidity_pct\":%.2f,\"valid\":true},",
p.co2.co2_ppm, p.co2.temperature, p.co2.humidity);
} else {
n += snprintf(buf + n, len - n, "\"ok\":false");
n += snprintf(buf + n, len - n, "\"co2\":null,");
}
n += snprintf(buf + n, len - n, "},");
// Health
// Health — full field names
n += snprintf(buf + n, len - n,
"\"health\":{\"scd43\":%s,\"tdelta\":%.2f,\"hdelta\":%.2f,\"tok\":%s,\"hok\":%s},",
"\"health\":{"
"\"scd43_present\":%s,"
"\"temp_delta\":%.2f,"
"\"humidity_delta\":%.2f,"
"\"temp_ok\":%s,"
"\"humidity_ok\":%s},",
p.health.scd43_present ? "true" : "false",
p.health.temp_delta,
p.health.humidity_delta,
p.health.temp_ok ? "true" : "false",
p.health.humidity_ok ? "true" : "false");
// Baseline
// Baseline — full field names
n += snprintf(buf + n, len - n,
"\"baseline\":{\"established\":%s,\"samples\":%u},",
"\"baseline\":{\"established\":%s,\"n_samples\":%u},",
p.baseline.established ? "true" : "false",
p.baseline.samples_collected);
// Gas sweep
// Gas sweep — full field names
n += snprintf(buf + n, len - n, "\"sweep\":[");
for (uint8_t i = 0; i < p.sweep_count; i++) {
n += snprintf(buf + n, len - n,
"%s{\"t\":%u,\"dur\":%u,\"zone\":%u,\"r\":%.0f,\"stable\":%s,\"ok\":%s}",
"%s{"
"\"heater_temp_c\":%u,"
"\"heater_dur_ms\":%u,"
"\"zone\":%u,"
"\"resistance_raw\":%.0f,"
"\"heater_stable\":%s,"
"\"valid\":%s"
"}",
i > 0 ? "," : "",
p.sweep[i].heater_temp_c,
p.sweep[i].heater_dur_ms,
@@ -312,7 +318,6 @@ size_t CultivarEnv::toJSON(const CultivarEnvPayload &p, char *buf, size_t len) {
void CultivarEnv::printPayload(const CultivarEnvPayload &p) {
Serial.printf("\n══ Cultivar ENV payload [t=%lums] ══\n", p.timestamp_ms);
// Ambient
Serial.println("── Ambient (BME690, heater off) ──────");
if (p.ambient.valid) {
Serial.printf(" Temperature: %.2f °C\n", p.ambient.temperature);
@@ -322,7 +327,6 @@ void CultivarEnv::printPayload(const CultivarEnvPayload &p) {
Serial.println(" [invalid]");
}
// CO2
Serial.println("── CO2 (SCD43) ───────────────────────");
if (p.health.scd43_present) {
if (p.co2.valid) {
@@ -336,7 +340,6 @@ void CultivarEnv::printPayload(const CultivarEnvPayload &p) {
Serial.println(" [not populated]");
}
// Health
Serial.println("── Sensor Health ─────────────────────");
if (p.health.scd43_present && p.ambient.valid && p.co2.valid) {
Serial.printf(" Temp delta: %.2f °C %s\n",
@@ -349,12 +352,10 @@ void CultivarEnv::printPayload(const CultivarEnvPayload &p) {
Serial.println(" [single sensor mode]");
}
// Baseline
Serial.printf("── Baseline [samples=%u, established=%s]\n",
p.baseline.samples_collected,
p.baseline.established ? "yes" : "no");
// Gas sweep
Serial.println("── Gas Sweep (BME690) ────────────────");
Serial.printf(" %-6s %-6s %-5s %-16s %-7s %s\n",
"Temp°C", "Dur ms", "Zone", "Resistance Ω", "Stable", "Valid");