// File: src/sen5x_app.c #include "sen5x_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 "sen5x_i2c.h" #include "net_influx.h" #include "storage.h" #include #include #include #include // NAN, isfinite, lround #include "vars.h" static const char* TAG = "sen5x_app"; /* ====================== Konfiguration ====================== */ #define SEN5X_PROD_STACK 4096 #define SEN5X_PROD_PRIO 6 // Producer leicht höher priorisiert // Influx worker runtime (läuft in net_influx.c) #define INFLUX_QUEUE_LEN 16 #define INFLUX_TASK_STACK 6144 #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) } sen5x_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) /* ====================== Lokale Float-Wrapper (optional) ====================== */ static int16_t sen5x_read_measured_values_float(float* pm1p0, float* pm2p5, float* pm4p0, float* pm10p0, float* rh, float* t, float* voc, float* nox) { uint16_t pm1_i=0, pm2_i=0, pm4_i=0, pm10_i=0; int16_t rh_i=0, t_i=0, voc_i=0, nox_i=0; int16_t err = sen5x_read_measured_values(&pm1_i, &pm2_i, &pm4_i, &pm10_i, &rh_i, &t_i, &voc_i, &nox_i); if (err) return err; if (pm1p0) *pm1p0 = (pm1_i == 0xFFFF) ? NAN : pm1_i / 10.0f; if (pm2p5) *pm2p5 = (pm2_i == 0xFFFF) ? NAN : pm2_i / 10.0f; if (pm4p0) *pm4p0 = (pm4_i == 0xFFFF) ? NAN : pm4_i / 10.0f; if (pm10p0) *pm10p0 = (pm10_i == 0xFFFF) ? NAN : pm10_i / 10.0f; if (rh) *rh = (rh_i == 0x7FFF) ? NAN : rh_i / 100.0f; if (t) *t = (t_i == 0x7FFF) ? NAN : t_i / 200.0f; if (voc) *voc = (voc_i == 0x7FFF) ? NAN : voc_i / 10.0f; if (nox) *nox = (nox_i == 0x7FFF) ? NAN : nox_i / 10.0f; return 0; } static int16_t sen5x_read_measured_pm_values_float(float* pm1, float* pm2, float* pm4, float* pm10, float* n05, float* n10, float* n25, float* n40, float* n100, float* dtyp) { uint16_t pm1_i=0, pm2_i=0, pm4_i=0, pm10_i=0, n05_i=0, n10_i=0, n25_i=0, n40_i=0, n100_i=0, dtyp_i=0; int16_t err = sen5x_read_measured_pm_values(&pm1_i, &pm2_i, &pm4_i, &pm10_i, &n05_i, &n10_i, &n25_i, &n40_i, &n100_i, &dtyp_i); if (err) return err; if (pm1) *pm1 = (pm1_i == 0xFFFF) ? NAN : pm1_i / 10.0f; if (pm2) *pm2 = (pm2_i == 0xFFFF) ? NAN : pm2_i / 10.0f; if (pm4) *pm4 = (pm4_i == 0xFFFF) ? NAN : pm4_i / 10.0f; if (pm10) *pm10 = (pm10_i == 0xFFFF) ? NAN : pm10_i / 10.0f; if (n05) *n05 = (n05_i == 0xFFFF) ? NAN : n05_i / 10.0f; if (n10) *n10 = (n10_i == 0xFFFF) ? NAN : n10_i / 10.0f; if (n25) *n25 = (n25_i == 0xFFFF) ? NAN : n25_i / 10.0f; if (n40) *n40 = (n40_i == 0xFFFF) ? NAN : n40_i / 10.0f; if (n100) *n100 = (n100_i == 0xFFFF) ? NAN : n100_i / 10.0f; if (dtyp) *dtyp = (dtyp_i == 0xFFFF) ? NAN : dtyp_i / 1000.0f; // µm return 0; } /* ====================== 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 sen5x_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 = sen5x_read_data_ready(&ready); if (rc != 0) { ESP_LOGW(TAG, "sen5x_read_data_ready error: %d", (int)rc); } else if (ready) { uint16_t pm1p0=0, pm2p5=0, pm4p0=0, pm10p0=0; int16_t rh_raw=0, t_raw=0, voc_idx=0, nox_idx=0; rc = sen5x_read_measured_values(&pm1p0, &pm2p5, &pm4p0, &pm10p0, &rh_raw, &t_raw, &voc_idx, &nox_idx); if (rc != 0) { ESP_LOGW(TAG, "sen5x_read_measured_values error: %d", (int)rc); } else { // Roh → Struct (wie bisher) sen5x_sample_t s = { .pm1p0 = pm1p0, .pm2p5 = pm2p5, .pm4p0 = pm4p0, .pm10p0 = pm10p0, .temp_c = ((float)t_raw) / 200.0f, .rh_pct = ((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 snprintf(buf, sizeof(buf), "%.1f", (double)s.temp_c); set_var_temp(buf); // Feuchte snprintf(buf, sizeof(buf), "%.1f", (double)s.rh_pct); set_var_humidity(buf); // VOC-Index (gerundet), falls NAN → "nan" if (!isfinite(vocf)) snprintf(buf, sizeof(buf), "nan"); 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), "nan"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm10f); set_var_pm10p0(buf); if (!isfinite(pm4f)) snprintf(buf, sizeof(buf), "nan"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm4f); set_var_pm4p0(buf); if (!isfinite(pm2f)) snprintf(buf, sizeof(buf), "nan"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm2f); set_var_pm2p5(buf); if (!isfinite(pm1f)) snprintf(buf, sizeof(buf), "nan"); 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; 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 }, }; net_influx_point_t p = { .measurement = "air", .tags = g_device_tag ? g_device_tag : "device=unknown", .ts_ms = 0, // Serverzeit .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 sen5x_app_init(const sen5x_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 SEN5x app..."); // 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(sen5x_start_measurement()); 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 sen5x_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 sen5x_stop_measurement/HAL free"); // Hier NICHT sen5x_stop_measurement() / HAL free, weil Task evtl. noch I2C nutzt return; } net_influx_stop(); if (g_device_tag) { free(g_device_tag); g_device_tag = NULL; } sen5x_stop_measurement(); sensirion_i2c_hal_free(); // i2cdev_done(); // NICHT hier, da Bus global genutzt wird g_inited = false; ESP_LOGI(TAG, "SEN5x 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(sen5x_app_task, ...) unverändert. */ void sen5x_app_task(void* arg) { (void)arg; if (!g_inited) { ESP_LOGE(TAG, "sen5x_app_task called before sen5x_app_init()"); vTaskDelete(NULL); return; } // Producer starten if (xTaskCreate(sen5x_task_producer, "sen5x_prod", SEN5X_PROD_STACK, NULL, SEN5X_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 }