388 lines
14 KiB
C
388 lines
14 KiB
C
// File: src/air_sensor_app.c
|
|
|
|
#include "air_sensor_app.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/queue.h" // bleibt drin (Header-Kompat / ggf. künftig wieder genutzt)
|
|
|
|
#include "esp_log.h"
|
|
#include "sensirion_i2c_hal.h"
|
|
#include "sensirion_i2c_esp32_config.h"
|
|
#include "hardware_config.h"
|
|
#if NANOPSX_SENSOR == NANOPSX_SENSOR_SEN66
|
|
#include "sen66_i2c.h"
|
|
#else
|
|
#include "sen5x_i2c.h"
|
|
#endif
|
|
|
|
#include "net_influx.h"
|
|
#include "storage.h"
|
|
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <math.h> // NAN, isfinite, lround
|
|
#include "vars.h"
|
|
#include "ui_model.h"
|
|
#include "esp_timer.h"
|
|
|
|
static const char* TAG = "air_sensor_app";
|
|
|
|
/* ====================== Konfiguration ====================== */
|
|
#define AIR_SENSOR_PROD_STACK 4096
|
|
#define AIR_SENSOR_PROD_PRIO 6 // Producer leicht höher priorisiert
|
|
|
|
// Influx worker runtime (läuft in net_influx.c)
|
|
#define INFLUX_QUEUE_LEN 32
|
|
#define INFLUX_TASK_STACK 8192
|
|
#define INFLUX_TASK_PRIO 5
|
|
|
|
#define INFLUX_INIT_RETRY_MS 1000
|
|
#define INFLUX_FAILS_REDISCOVER 5
|
|
#define INFLUX_BACKOFF_START_MS 2000
|
|
#define INFLUX_BACKOFF_MAX_MS 300000
|
|
#define INFLUX_HTTP_TIMEOUT_MS 3000
|
|
|
|
/* ====================== Typen & State ====================== */
|
|
typedef struct {
|
|
uint16_t pm1p0, pm2p5, pm4p0, pm10p0; // Rohwerte (x10)
|
|
float temp_c, rh_pct; // skaliert
|
|
int16_t voc_idx, nox_idx; // Rohwerte (x10)
|
|
} air_sensor_sample_t;
|
|
|
|
static bool g_inited = false;
|
|
static bool g_run = false;
|
|
static TaskHandle_t g_task_prod = NULL;
|
|
|
|
// App-Context
|
|
static char* g_device_tag = NULL; // z.B. "device=xyz"
|
|
|
|
/* ====================== Helfer ====================== */
|
|
#define CHECK_ESP_OR_RET(x) do { \
|
|
esp_err_t __e = (x); \
|
|
if (__e != ESP_OK) { \
|
|
ESP_LOGE(TAG, "%s failed: %s", #x, esp_err_to_name(__e)); \
|
|
return __e; \
|
|
} \
|
|
} while (0)
|
|
|
|
#define CHECK_SEN_OR_RET(x) do { \
|
|
int16_t __rc = (x); \
|
|
if (__rc != 0) { \
|
|
ESP_LOGE(TAG, "%s failed: %d", #x, (int)__rc); \
|
|
return ESP_FAIL; \
|
|
} \
|
|
} while (0)
|
|
|
|
/* One selected command set; shared HAL and presentation/Influx pipeline. */
|
|
static int16_t sensor_ready(bool *ready) {
|
|
#if NANOPSX_SENSOR == NANOPSX_SENSOR_SEN66
|
|
uint8_t padding;
|
|
return sen66_get_data_ready(&padding, ready);
|
|
#else
|
|
return sen5x_read_data_ready(ready);
|
|
#endif
|
|
}
|
|
|
|
static int16_t sensor_read(uint16_t *pm1, uint16_t *pm2, uint16_t *pm4,
|
|
uint16_t *pm10, int16_t *rh, int16_t *temp,
|
|
int16_t *voc, int16_t *nox, uint16_t *co2) {
|
|
#if NANOPSX_SENSOR == NANOPSX_SENSOR_SEN66
|
|
return sen66_read_measured_values_as_integers(pm1, pm2, pm4, pm10,
|
|
rh, temp, voc, nox, co2);
|
|
#else
|
|
*co2 = UINT16_MAX; // SEN54 has no CO2 channel: never submit a fake zero.
|
|
return sen5x_read_measured_values(pm1, pm2, pm4, pm10, rh, temp, voc, nox);
|
|
#endif
|
|
}
|
|
|
|
static int16_t sensor_start(void) {
|
|
#if NANOPSX_SENSOR == NANOPSX_SENSOR_SEN66
|
|
sen66_init(NANOPSX_SENSOR_ADDRESS);
|
|
// A sensor can keep measuring across an ESP-only reset.
|
|
int16_t rc = sen66_stop_measurement();
|
|
if (rc) return rc;
|
|
int8_t product[32] = {0};
|
|
rc = sen66_get_product_name(product, sizeof(product) - 1);
|
|
if (rc) return rc;
|
|
if (strncmp((const char *)product, "SEN66", 5) != 0) {
|
|
ESP_LOGE(TAG, "Expected SEN66, product=%s", (char *)product);
|
|
return -1;
|
|
}
|
|
ESP_LOGI(TAG, "Sensor identified: %s", (char *)product);
|
|
return sen66_start_continuous_measurement();
|
|
#else
|
|
return sen5x_start_measurement();
|
|
#endif
|
|
}
|
|
|
|
static int16_t sensor_stop(void) {
|
|
#if NANOPSX_SENSOR == NANOPSX_SENSOR_SEN66
|
|
return sen66_stop_measurement();
|
|
#else
|
|
return sen5x_stop_measurement();
|
|
#endif
|
|
}
|
|
|
|
/* ====================== App-Context: Device Tag ====================== */
|
|
static esp_err_t make_device_tag_once(void) {
|
|
if (g_device_tag) return ESP_OK;
|
|
|
|
char* dev = NULL;
|
|
if (storage_load_device_name(&dev) && dev && dev[0]) {
|
|
size_t need = strlen("device=") + strlen(dev) + 1;
|
|
g_device_tag = (char*)malloc(need);
|
|
if (!g_device_tag) {
|
|
if (dev) free(dev);
|
|
return ESP_ERR_NO_MEM;
|
|
}
|
|
snprintf(g_device_tag, need, "device=%s", dev);
|
|
free(dev);
|
|
return ESP_OK;
|
|
}
|
|
g_device_tag = strdup("device=unknown");
|
|
return g_device_tag ? ESP_OK : ESP_ERR_NO_MEM;
|
|
}
|
|
|
|
/* ====================== Producer-Task ====================== */
|
|
static void air_sensor_task_producer(void* arg) {
|
|
(void)arg;
|
|
|
|
const TickType_t period = pdMS_TO_TICKS(1000); // exakt 1 Hz
|
|
TickType_t last_wake = xTaskGetTickCount();
|
|
|
|
ESP_LOGI(TAG, "Producer started (1 Hz).");
|
|
|
|
for (; g_run; ) {
|
|
bool ready = false;
|
|
int16_t rc = sensor_ready(&ready);
|
|
if (rc != 0) {
|
|
ESP_LOGW(TAG, "sensor_ready error: %d", (int)rc);
|
|
} else if (ready) {
|
|
uint16_t pm1p0=0, pm2p5=0, pm4p0=0, pm10p0=0, co2_raw=UINT16_MAX;
|
|
int16_t rh_raw=0, t_raw=0, voc_idx=0, nox_idx=0;
|
|
|
|
rc = sensor_read(&pm1p0, &pm2p5, &pm4p0, &pm10p0,
|
|
&rh_raw, &t_raw, &voc_idx, &nox_idx, &co2_raw);
|
|
if (rc != 0) {
|
|
ESP_LOGW(TAG, "sensor_read error: %d", (int)rc);
|
|
} else {
|
|
// Roh → Struct (wie bisher)
|
|
air_sensor_sample_t s = {
|
|
.pm1p0 = pm1p0,
|
|
.pm2p5 = pm2p5,
|
|
.pm4p0 = pm4p0,
|
|
.pm10p0 = pm10p0,
|
|
.temp_c = (t_raw == INT16_MAX) ? NAN : ((float)t_raw) / 200.0f,
|
|
.rh_pct = (rh_raw == INT16_MAX) ? NAN : ((float)rh_raw) / 100.0f,
|
|
.voc_idx = voc_idx,
|
|
.nox_idx = nox_idx
|
|
};
|
|
|
|
/* === GUI-Variablen jede Sekunde updaten (skaliert) === */
|
|
char buf[24];
|
|
float pm1f = (s.pm1p0 == 0xFFFF) ? NAN : s.pm1p0 / 10.0f;
|
|
float pm2f = (s.pm2p5 == 0xFFFF) ? NAN : s.pm2p5 / 10.0f;
|
|
float pm4f = (s.pm4p0 == 0xFFFF) ? NAN : s.pm4p0 / 10.0f;
|
|
float pm10f = (s.pm10p0 == 0xFFFF) ? NAN : s.pm10p0 / 10.0f;
|
|
float vocf = (s.voc_idx == 0x7FFF) ? NAN : s.voc_idx / 10.0f;
|
|
// float noxf = (s.nox_idx == 0x7FFF) ? NAN : s.nox_idx / 10.0f; // bei Bedarf anzeigen
|
|
|
|
// Temperatur
|
|
if (isfinite(s.temp_c)) snprintf(buf, sizeof(buf), "%.1f", (double)s.temp_c);
|
|
else snprintf(buf, sizeof(buf), "--");
|
|
set_var_temp(buf);
|
|
|
|
// Feuchte
|
|
if (isfinite(s.rh_pct)) snprintf(buf, sizeof(buf), "%.1f", (double)s.rh_pct);
|
|
else snprintf(buf, sizeof(buf), "--");
|
|
set_var_humidity(buf);
|
|
|
|
// VOC-Index (gerundet), nicht verfügbare Werte als "--"
|
|
if (!isfinite(vocf)) snprintf(buf, sizeof(buf), "--");
|
|
else snprintf(buf, sizeof(buf), "%d", (int)lround(vocf));
|
|
set_var_voc_ind(buf);
|
|
|
|
// PM (µg/m³) mit einer Nachkommastelle
|
|
if (!isfinite(pm10f)) snprintf(buf, sizeof(buf), "--"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm10f);
|
|
set_var_pm10p0(buf);
|
|
if (!isfinite(pm4f)) snprintf(buf, sizeof(buf), "--"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm4f);
|
|
set_var_pm4p0(buf);
|
|
if (!isfinite(pm2f)) snprintf(buf, sizeof(buf), "--"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm2f);
|
|
set_var_pm2p5(buf);
|
|
if (!isfinite(pm1f)) snprintf(buf, sizeof(buf), "--"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm1f);
|
|
set_var_pm1p0(buf);
|
|
|
|
/* === Influx submit (generisch, variabel viele Felder) === */
|
|
const double pm1 = (s.pm1p0 == 0xFFFF) ? NAN : ((double)s.pm1p0 / 10.0);
|
|
const double pm2_5 = (s.pm2p5 == 0xFFFF) ? NAN : ((double)s.pm2p5 / 10.0);
|
|
const double pm4 = (s.pm4p0 == 0xFFFF) ? NAN : ((double)s.pm4p0 / 10.0);
|
|
const double pm10 = (s.pm10p0 == 0xFFFF) ? NAN : ((double)s.pm10p0 / 10.0);
|
|
const double voc = (s.voc_idx == 0x7FFF) ? NAN : ((double)s.voc_idx / 10.0);
|
|
const double nox = (s.nox_idx == 0x7FFF) ? NAN : ((double)s.nox_idx / 10.0);
|
|
const double temp = (double)s.temp_c;
|
|
const double rh = (double)s.rh_pct;
|
|
const double co2 = (co2_raw == UINT16_MAX) ? NAN : (double)co2_raw;
|
|
#if NANOPSX_HAS_DISPLAY
|
|
ui_sample_t display_sample = {
|
|
.timestamp_ms = (uint64_t)(esp_timer_get_time() / 1000),
|
|
.available = true,
|
|
.values = {(float)co2, (float)pm2_5, (float)temp,
|
|
(float)rh, (float)voc, (float)nox}
|
|
};
|
|
ui_model_publish(&display_sample);
|
|
#endif
|
|
static unsigned samples;
|
|
if (++samples % 10 == 0) {
|
|
ESP_LOGI(TAG, "%s sample: PM2.5=%.1f temp=%.2f rh=%.2f CO2=%.0f",
|
|
NANOPSX_SENSOR_NAME, pm2_5, temp, rh, co2);
|
|
}
|
|
|
|
net_influx_field_t f[] = {
|
|
{ .key="pm1", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=pm1 },
|
|
{ .key="pm2_5", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=pm2_5 },
|
|
{ .key="pm4", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=pm4 },
|
|
{ .key="pm10", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=pm10 },
|
|
{ .key="temp", .type=NET_INFLUX_F_FLOAT, .precision=3, .v.f=temp },
|
|
{ .key="rh", .type=NET_INFLUX_F_FLOAT, .precision=3, .v.f=rh },
|
|
{ .key="voc", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=voc },
|
|
{ .key="nox", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=nox },
|
|
{ .key="co2", .type=NET_INFLUX_F_FLOAT, .precision=0, .v.f=co2 },
|
|
};
|
|
|
|
net_influx_point_t p = {
|
|
.measurement = "air",
|
|
.tags = g_device_tag ? g_device_tag : "device=unknown",
|
|
.ts_ms = 0, // Messzeitpunkt; Worker richtet UTC an Server-HTTP-Date aus
|
|
.fields = f,
|
|
.num_fields = sizeof(f) / sizeof(f[0]),
|
|
};
|
|
|
|
(void)net_influx_submit_point(&p);
|
|
}
|
|
}
|
|
|
|
vTaskDelayUntil(&last_wake, period); // präzise 1 Hz
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Producer stopping.");
|
|
g_task_prod = NULL;
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
/* ====================== Öffentliche API ====================== */
|
|
esp_err_t air_sensor_app_init(const air_sensor_i2c_cfg_t* cfg) {
|
|
if (g_inited) {
|
|
ESP_LOGW(TAG, "already initialized");
|
|
return ESP_OK;
|
|
}
|
|
if (!cfg) return ESP_ERR_INVALID_ARG;
|
|
|
|
ESP_LOGI(TAG, "Initializing selected air sensor...");
|
|
|
|
// 1) I2C-Subsystem
|
|
|
|
|
|
// 2) HAL-Konfiguration
|
|
struct esp32_i2c_config hal_cfg = {
|
|
.addr = cfg->addr,
|
|
.port = cfg->port,
|
|
.sda = cfg->sda,
|
|
.scl = cfg->scl,
|
|
.sda_pullup = cfg->sda_pullup,
|
|
.scl_pullup = cfg->scl_pullup,
|
|
.freq = cfg->freq
|
|
};
|
|
CHECK_ESP_OR_RET(sensirion_i2c_config_esp32(&hal_cfg));
|
|
|
|
// 3) HAL init
|
|
sensirion_i2c_hal_init();
|
|
CHECK_ESP_OR_RET(sensirion_i2c_esp32_ok());
|
|
|
|
// 4) Messung starten
|
|
CHECK_SEN_OR_RET(sensor_start());
|
|
ESP_LOGI(TAG, "Measurement started.");
|
|
|
|
// 5) Device Tag (App-Kontext)
|
|
CHECK_ESP_OR_RET(make_device_tag_once());
|
|
|
|
// 6) Influx Worker starten (Influx-spezifische Init/Retry/Backoff läuft intern)
|
|
net_influx_runtime_cfg_t rt = {
|
|
.queue_len = INFLUX_QUEUE_LEN,
|
|
.task_stack = INFLUX_TASK_STACK,
|
|
.task_prio = INFLUX_TASK_PRIO,
|
|
.init_retry_ms = INFLUX_INIT_RETRY_MS,
|
|
.backoff_start_ms = INFLUX_BACKOFF_START_MS,
|
|
.backoff_max_ms = INFLUX_BACKOFF_MAX_MS,
|
|
.fails_before_reinit = INFLUX_FAILS_REDISCOVER,
|
|
.ca_cert_pem_opt = NULL,
|
|
.http_timeout_ms = INFLUX_HTTP_TIMEOUT_MS,
|
|
};
|
|
CHECK_ESP_OR_RET(net_influx_start(&rt));
|
|
|
|
g_run = true;
|
|
g_inited = true;
|
|
return ESP_OK;
|
|
}
|
|
|
|
void air_sensor_app_deinit(void) {
|
|
if (!g_inited) return;
|
|
|
|
ESP_LOGI(TAG, "Deinitializing SEN5x app...");
|
|
|
|
g_run = false;
|
|
|
|
for (int i = 0; i < 50 && g_task_prod; i++) {
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
|
}
|
|
|
|
if (g_task_prod) {
|
|
ESP_LOGW(TAG, "Producer did not stop in time; skip sensor_stop/HAL free");
|
|
// Hier NICHT sensor_stop() / HAL free, weil Task evtl. noch I2C nutzt
|
|
return;
|
|
}
|
|
|
|
net_influx_stop();
|
|
|
|
if (g_device_tag) {
|
|
free(g_device_tag);
|
|
g_device_tag = NULL;
|
|
}
|
|
|
|
sensor_stop();
|
|
sensirion_i2c_hal_free();
|
|
|
|
// i2cdev_done(); // NICHT hier, da Bus global genutzt wird
|
|
|
|
g_inited = false;
|
|
ESP_LOGI(TAG, "Air sensor app deinitialized.");
|
|
}
|
|
|
|
|
|
/**
|
|
* Kompatibilität zur bisherigen main.c:
|
|
* Diese Task fungiert nun als "Launcher": startet Producer und beendet sich dann.
|
|
* In main.c bleibt der Aufruf von xTaskCreate(air_sensor_app_task, ...) unverändert.
|
|
*/
|
|
void air_sensor_app_task(void* arg) {
|
|
(void)arg;
|
|
|
|
if (!g_inited) {
|
|
ESP_LOGE(TAG, "air_sensor_app_task called before air_sensor_app_init()");
|
|
vTaskDelete(NULL);
|
|
return;
|
|
}
|
|
|
|
// Producer starten
|
|
if (xTaskCreate(air_sensor_task_producer, "air_sensor_prod",
|
|
AIR_SENSOR_PROD_STACK, NULL, AIR_SENSOR_PROD_PRIO, &g_task_prod) != pdPASS) {
|
|
ESP_LOGE(TAG, "Failed to create producer task");
|
|
vTaskDelete(NULL);
|
|
return;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Launcher done (producer started).");
|
|
vTaskDelete(NULL); // Launcher beendet sich selbst
|
|
}
|