Files
yggdrasil/thermo_sensor/main/thermo_sensor.c
T
2026-09-25 16:33:06 +02:00

104 lines
3.3 KiB
C

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_system.h>
#include <esp_timer.h>
#include "nvs_flash.h"
#include "esp_http_server.h"
#include "ygg_http_server.h"
#include "ygg_http_routes.h"
#include "ygg_wifi.h"
#include "ygg_http_server.h"
#include "ygg_bmp280.h"
#include "ygg_http_req.h"
#include "ygg_state.h"
#include "ygg_sh1106.h"
#include "lvgl.h"
static const char *TAG = "thermo_sensor";
static lv_display_t *sh_display = NULL;
static lv_obj_t *label = NULL;
bmp280_t dev_bmp_280;
static void lvgl_tick_cb(void *arg) {
lv_tick_inc(10);
}
void lvgl_task(void *pvParameters) {
while (1) {
lv_timer_handler();
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void sensor_task(void *pvParameters) {
// Wait 1s, to avoid corrupted first sensor readout
vTaskDelay(pdMS_TO_TICKS(1000));
while (1) {
ygg_bmp280_read_sensor(&dev_bmp_280);
// Najpierw OLED, potem (blokujący) HTTP — żeby ekran
// odświeżył się nawet gdy serwer nie odpowiada.
char buf[32];
snprintf(buf, sizeof(buf), "Temp: %.2f C",
CURR_READOUT.temperature);
ygg_set_display_label(sh_display, label, buf);
ygg_send_sensor_readouts(BASE_URL,
SENSOR_NAME,
CURR_READOUT);
vTaskDelay(pdMS_TO_TICKS(10000));
}
}
void app_main()
{
// NVS - Non-Volatile Storage, this memory stays untouched
// after power off. It is key value memory stored
// in separate partition of flash. It takes about 20kB.
// Here it is not used directly. It is required by WiFi
// driver to store SSID and password.
// NVS is organized into pages. Single page is one flash
// sector - typically 4096B
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// ESP_ERR_NVS_NEW_VERSION_FOUND - NVS header page is
// stored in newer version (newer IDF) than version
// it is used by this application
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
ygg_wifi_init_sta();
httpd_handle_t server = ygg_start_webserver();
httpd_register_uri_handler(server, &ygg_temp_uri);
httpd_register_uri_handler(server, &ygg_press_uri);
httpd_register_uri_handler(server, &ygg_hum_uri);
ESP_ERROR_CHECK(i2cdev_init());
sh_display = ygg_sh1106_init();
// LVGL potrzebuje regularnego ticka, inaczej redraw nigdy się nie wykona
// (to był powód, dla którego wisiało "FIRST").
const esp_timer_create_args_t lvgl_tick_args = {
.callback = lvgl_tick_cb,
.name = "lvgl_tick",
};
esp_timer_handle_t lvgl_tick_handle = NULL;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_args, &lvgl_tick_handle));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_handle, 10 * 1000));
lv_obj_t *scr = lv_display_get_screen_active(sh_display);
label = lv_label_create(scr);
ygg_set_display_label(sh_display, label, "FIRST");
ygg_bmp280_init(&dev_bmp_280);
xTaskCreatePinnedToCore(lvgl_task, "lvgl", 4096, NULL, 4, NULL, APP_CPU_NUM);
xTaskCreatePinnedToCore(sensor_task, "bmp280_test", 8192, NULL, 5, NULL, APP_CPU_NUM);
}