Researched the exact modules the user is building with and fixed three
concrete issues the earlier speculative firmware got wrong:
1. WiFi: confirmed the target board (Waveshare ESP32-P4-Module-DEV-KIT,
chip ESP32-P4NRW32) bridges WiFi through an onboard ESP32-C6
co-processor over a fixed 7-pin SDIO link (CLK18/CMD19/D0-14/D1-15/
D2-16/D3-17/RESET54, cross-confirmed against Espressif's own
esp-hosted-mcu docs). wifi_manager.c's esp_wifi_init()/esp_wifi_start()
calls don't need to change — esp_wifi_remote/esp_hosted provide a
drop-in-compatible API — but the component manifest (new
main/idf_component.yml) and sdkconfig.defaults were missing entirely.
2. Real pin conflict: the presence sensor's original UART pins (17/18)
directly collided with the SDIO CLK/D3 pins above — wiring it there
would have broken WiFi, the sensor, or both. Moved to GPIO4/5.
3. Swapped placeholder parts for the user's actual hardware:
- BME280 -> BMP280 (GY-BMP280 module): temp+pressure only, no humidity.
Rewrote the driver rather than just renaming it — the old code would
have read nonexistent humidity registers and reported garbage
forever. 3.3V-only wiring note added (the BME280 assumption of
5V-tolerant logic doesn't hold for this specific breakout).
- LD2410 -> RD-03E (Ai-Thinker, not Hi-Link — a different manufacturer
with a different, incompatible UART protocol). Rewrote the frame
parser against the RD-03E's actual (if less-documented) 5-byte
simple-report format. Reports numeric distance instead of a boolean,
which better fits both the hardware's actual output and the backend's
statistical anomaly detector.
README, CMakeLists.txt, and all cross-references updated to match.
74 lines
2.8 KiB
C
74 lines
2.8 KiB
C
// Sensor driver registry -- see sensor_registry.h.
|
|
//
|
|
// ADDING A NEW SENSOR: write its driver as a new .c/.h pair implementing
|
|
// sensor_driver_t's init()/read() (see sensor_driver.h), add the .c file to
|
|
// main/CMakeLists.txt's SRCS list, #include its header below, and add one
|
|
// line to the s_drivers[] array. Nothing else in this firmware needs to
|
|
// change -- app_main.c's task loop and telemetry_client.c's POST logic both
|
|
// iterate the registry generically and have no per-sensor-type branches.
|
|
|
|
#include <stdbool.h>
|
|
#include "sensor_registry.h"
|
|
#include "bmp280.h"
|
|
#include "rd03e.h"
|
|
#include "esp_log.h"
|
|
|
|
static const char *TAG = "sensor_registry";
|
|
|
|
// The concrete list of sensor drivers compiled into this firmware. Order
|
|
// matters only for init() sequencing (e.g. bus setup before device use);
|
|
// read() order just determines readings array ordering in the POST body.
|
|
static const sensor_driver_t s_drivers[] = {
|
|
{ .name = "bmp280", .init = bmp280_init, .read = bmp280_read },
|
|
{ .name = "rd03e", .init = rd03e_init, .read = rd03e_read },
|
|
// Add new drivers here, e.g.:
|
|
// { .name = "my_sensor", .init = my_sensor_init, .read = my_sensor_read },
|
|
};
|
|
|
|
static const size_t s_driver_count = sizeof(s_drivers) / sizeof(s_drivers[0]);
|
|
|
|
// Tracks which drivers initialized successfully so a failed driver's read()
|
|
// isn't called every cycle (and doesn't spam logs) -- but the array stays
|
|
// a fixed compile-time list either way, no dynamic registration needed at
|
|
// this project's scale.
|
|
static bool s_driver_ready[sizeof(s_drivers) / sizeof(s_drivers[0])];
|
|
|
|
void sensor_registry_init_all(void) {
|
|
for (size_t i = 0; i < s_driver_count; i++) {
|
|
const sensor_driver_t *drv = &s_drivers[i];
|
|
if (drv->init == NULL) {
|
|
s_driver_ready[i] = true;
|
|
continue;
|
|
}
|
|
esp_err_t err = drv->init();
|
|
if (err == ESP_OK) {
|
|
s_driver_ready[i] = true;
|
|
ESP_LOGI(TAG, "driver '%s' initialized", drv->name);
|
|
} else {
|
|
s_driver_ready[i] = false;
|
|
ESP_LOGW(TAG, "driver '%s' failed to initialize (%s) -- it will be skipped",
|
|
drv->name, esp_err_to_name(err));
|
|
}
|
|
}
|
|
}
|
|
|
|
esp_err_t sensor_registry_collect(sensor_reading_t *out, size_t max_out, size_t *out_count) {
|
|
size_t total = 0;
|
|
for (size_t i = 0; i < s_driver_count && total < max_out; i++) {
|
|
if (!s_driver_ready[i]) {
|
|
continue;
|
|
}
|
|
const sensor_driver_t *drv = &s_drivers[i];
|
|
size_t produced = 0;
|
|
esp_err_t err = drv->read(&out[total], max_out - total, &produced);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGW(TAG, "driver '%s' read failed (%s) -- skipping this cycle",
|
|
drv->name, esp_err_to_name(err));
|
|
continue;
|
|
}
|
|
total += produced;
|
|
}
|
|
*out_count = total;
|
|
return ESP_OK;
|
|
}
|