domotic/main/main.c
2025-05-08 19:55:17 +02:00

673 lines
22 KiB
C

#include "meteofrance.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_check.h"
#include "driver/i2c.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include <locale.h>
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include <stdio.h>
#include "esp_http_server.h"
#include "bsp/esp-bsp.h"
#include "main.h"
#include "ihm.h"
// OTA
#include "esp_ota_ops.h"
#include "esp_http_client.h"
#include "esp_https_ota.h"
// Includes personnels
#include "wifi_logger.h"
#include "obtain_time.h"
#include "image_downloader.h"
#include "communication.h"
#include "stateManagement.h"
#include "driver/gpio.h"
#include "am2302_rmt.h"
#include "esp_timer.h"
// GPIO assignment
#define AM2302_GPIO 4
#include "esp_littlefs.h"
#define MOUNT_POINT "/sdcard"
// Pin assignments can be set in menuconfig, see "SD SPI Example Configuration" menu.
// You can also change the pin assignments here by changing the following 4 lines.
#define PIN_NUM_MISO 13
#define PIN_NUM_MOSI 11
#define PIN_NUM_CLK 12
#define PIN_NUM_CS 10
static const char *TAG = "domoTic";
extern esp_mqtt_client_handle_t client;
struct state mainState={
.wifi_init=false,
.display_init=false
};
void mount_sd_card()
{
// Options for mounting the filesystem.
// If format_if_mount_failed is set to true, SD card will be partitioned and
// formatted in case when mounting fails.
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = false,
.max_files = 5,
.allocation_unit_size = 16 * 1024};
sdmmc_card_t *card;
const char mount_point[] = MOUNT_POINT;
ESP_LOGI(TAG, "Initializing SD card");
ESP_LOGI(TAG, "Using SPI peripheral");
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
spi_bus_config_t bus_cfg = {
.mosi_io_num = PIN_NUM_MOSI,
.miso_io_num = PIN_NUM_MISO,
.sclk_io_num = PIN_NUM_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4000,
};
esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to initialize bus.");
return;
}
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = PIN_NUM_CS;
slot_config.host_id = host.slot;
ESP_LOGI(TAG, "Mounting filesystem");
ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK)
{
if (ret == ESP_FAIL)
{
ESP_LOGE(TAG, "Failed to mount filesystem. "
"If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
}
else
{
ESP_LOGE(TAG, "Failed to initialize the card (%s). "
"Make sure SD card lines have pull-up resistors in place.",
esp_err_to_name(ret));
}
return;
}
ESP_LOGI(TAG, "Filesystem mounted");
// Card has been initialized, print its properties
sdmmc_card_print_info(stdout, card);
}
extern char *days[7];
extern char *months[12];
esp_err_t _ota_http_event_handler(esp_http_client_event_t *evt)
{
switch (evt->event_id) {
case HTTP_EVENT_ERROR:
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
break;
case HTTP_EVENT_REDIRECT:
ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
break;
}
return ESP_OK;
}
extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start");
extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end");
static esp_err_t validate_image_header(esp_app_desc_t *new_app_info)
{
if (new_app_info == NULL) {
return ESP_ERR_INVALID_ARG;
}
const esp_partition_t *running = esp_ota_get_running_partition();
esp_app_desc_t running_app_info;
if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version);
}
#ifndef CONFIG_EXAMPLE_SKIP_VERSION_CHECK
if (memcmp(new_app_info->version, running_app_info.version, sizeof(new_app_info->version)) == 0) {
ESP_LOGW(TAG, "Current running version is the same as a new. We will not continue the update.");
return ESP_FAIL;
}
#endif
return ESP_OK;
}
void simple_ota_example_task(void *pvParameter)
{
esp_err_t ota_finish_err = ESP_OK;
ESP_LOGI(TAG, "Starting OTA example task");
#ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF
esp_netif_t *netif = get_example_netif_from_desc(bind_interface_name);
if (netif == NULL) {
ESP_LOGE(TAG, "Can't find netif from interface description");
abort();
}
struct ifreq ifr;
esp_netif_get_netif_impl_name(netif, ifr.ifr_name);
ESP_LOGI(TAG, "Bind interface name is %s", ifr.ifr_name);
#endif
esp_http_client_config_t config = {
.url = "https://192.168.0.28:8070/rgb_lcd.bin",
.timeout_ms = 30000,
.buffer_size = 6144,
.buffer_size_tx = 6144, //TX Buffer, Main Buffer
.event_handler = _ota_http_event_handler,
.keep_alive_enable = true,
.cert_pem = (char *)server_cert_pem_start,
#ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF
.if_name = &ifr,
#endif
};
#ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL_FROM_STDIN
char url_buf[OTA_URL_SIZE];
if (strcmp(config.url, "FROM_STDIN") == 0) {
example_configure_stdin_stdout();
fgets(url_buf, OTA_URL_SIZE, stdin);
int len = strlen(url_buf);
url_buf[len - 1] = '\0';
config.url = url_buf;
} else {
ESP_LOGE(TAG, "Configuration mismatch: wrong firmware upgrade image url");
abort();
}
#endif
#ifdef CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK
config.skip_cert_common_name_check = true;
#endif
esp_https_ota_config_t ota_config = {
.http_config = &config,
};
ESP_LOGI(TAG, "Attempting to download update from %s", config.url);
esp_https_ota_handle_t https_ota_handle = NULL;
esp_err_t err = esp_https_ota_begin(&ota_config, &https_ota_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "ESP HTTPS OTA Begin failed");
vTaskDelete(NULL);
}
esp_app_desc_t app_desc;
err = esp_https_ota_get_img_desc(https_ota_handle, &app_desc);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_https_ota_get_img_desc failed");
goto ota_end;
}
err = validate_image_header(&app_desc);
if (err != ESP_OK) {
ESP_LOGE(TAG, "image header verification failed");
goto ota_end;
}
if(bsp_display_lock(100)){
app_ota_display();
bsp_display_unlock();
}
while (1) {
err = esp_https_ota_perform(https_ota_handle);
if (err != ESP_ERR_HTTPS_OTA_IN_PROGRESS) {
break;
}
// esp_https_ota_perform returns after every read operation which gives user the ability to
// monitor the status of OTA upgrade by calling esp_https_ota_get_image_len_read, which gives length of image
// data read so far.
//ESP_LOGE(TAG,"Image size : %i", esp_https_ota_get_image_size(https_ota_handle));
int percent=esp_https_ota_get_image_len_read(https_ota_handle)*100/esp_https_ota_get_image_size(https_ota_handle);
//ESP_LOGE(TAG, "Image bytes read: %d %i%%", esp_https_ota_get_image_len_read(https_ota_handle),percent);
if(bsp_display_lock(100)){
setOTAProgress(percent);
bsp_display_unlock();
}
}
if (esp_https_ota_is_complete_data_received(https_ota_handle) != true) {
// the OTA image was not completely received and user can customise the response to this situation.
ESP_LOGE(TAG, "Complete data was not received.");
} else {
ota_finish_err = esp_https_ota_finish(https_ota_handle);
if ((err == ESP_OK) && (ota_finish_err == ESP_OK)) {
ESP_LOGI(TAG, "ESP_HTTPS_OTA upgrade successful. Rebooting ...");
vTaskDelay(1000 / portTICK_PERIOD_MS);
esp_restart();
} else {
if (ota_finish_err == ESP_ERR_OTA_VALIDATE_FAILED) {
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
}
ESP_LOGE(TAG, "ESP_HTTPS_OTA upgrade failed 0x%x", ota_finish_err);
vTaskDelete(NULL);
}
}
ota_end:
esp_https_ota_abort(https_ota_handle);
ESP_LOGE(TAG, "ESP_HTTPS_OTA upgrade failed");
vTaskDelete(NULL);
}
am2302_handle_t sensor = NULL;
void readTempHumid(void *pvParameter)
{
float temperature = 0;
float humidity = 0;
while (1)
{
am2302_read_temp_humi(sensor, &temperature, &humidity);
char buff[40];
ESP_LOGI(TAG, "Temperature: %.1f °C, Humidity: %.1f %%", temperature, humidity);
sprintf(buff,"%.1f °C, %.1f %%", temperature, humidity);
show_temp(buff);
vTaskDelay(60000 / portTICK_PERIOD_MS);
}
}
void alloc_fail(size_t size, uint32_t caps, const char * function_name){
ESP_LOGE(TAG,"fail alloc %u in %" PRIu32 " in %s", size,caps,function_name);
}
static QueueHandle_t gpio_evt_queue = NULL;
// Durée pour l'arret automatique (micro-secondes ^^)
uint64_t arretAuto=5*60*1000*1000;
bool ecranEteint=true;
// Ce timer permet d'eteindre l'ecran "arretAuto" apres la derniere présence détectée
esp_timer_handle_t presence_timer;
static void IRAM_ATTR gpio_isr_handler(void* arg)
{
uint32_t gpio_num = (uint32_t) arg;
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}
static void gpio_task_example(void* arg)
{
uint32_t io_num;
int delay=50;
for (;;) {
if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
ESP_LOGE(TAG,"Got it !");
if(ecranEteint){
for (int i = 0; i < 100; i+=2)
{
if(bsp_display_lock(0)){
bsp_display_brightness_set(i);
bsp_display_unlock();
}
vTaskDelay(delay/portTICK_PERIOD_MS);
}
ecranEteint=false;
}else{
ESP_LOGI(TAG, "Ecran déjà allumé");
}
//On arrete le timer de presence
esp_timer_stop(presence_timer);
//Pour le redemarrer
ESP_ERROR_CHECK(esp_timer_start_once(presence_timer, arretAuto));
}
}
}
#define GPIO_INPUT_IO_0 CONFIG_GPIO_INPUT_CAPTEUR_PIR
void initPirSensor(){
//create a queue to handle gpio event from isr
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
//start gpio task
xTaskCreate(gpio_task_example, "gpio_task_example", 5000, NULL, 10, NULL);
gpio_config_t gpioconf={
.pin_bit_mask= 1ULL<<GPIO_INPUT_IO_0,
.intr_type=GPIO_INTR_POSEDGE,
.mode=GPIO_MODE_INPUT,
.pull_up_en=1
};
ESP_ERROR_CHECK(gpio_config(&gpioconf));
gpio_install_isr_service(0);
gpio_isr_handler_add(GPIO_INPUT_IO_0,gpio_isr_handler,(void*)(GPIO_INPUT_IO_0));
}
/* Ce timer permet d'eteindre l'ecran apres @arretAuto de présence*/
static void presence_timer_callback(void* arg)
{
int64_t time_since_boot = esp_timer_get_time();
for (int i = 100; i >= 0; i-=2)
{
if(bsp_display_lock(0)){
bsp_display_brightness_set(i);
bsp_display_unlock();
}
vTaskDelay(20/portTICK_PERIOD_MS);
}
ecranEteint=true;
}
static esp_err_t download_get_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "application/octet-stream");
httpd_resp_set_hdr(req, "Content-Disposition",
"attachment;filename=core.bin");
esp_partition_iterator_t partition_iterator = esp_partition_find(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_COREDUMP, "coredump");
const esp_partition_t *partition = esp_partition_get(partition_iterator);
int file_size = 65536;
int chunk_size = 1024;
int i = 0;
for (i = 0; i < (file_size / chunk_size); i++) {
char store_data[chunk_size];
ESP_ERROR_CHECK(
esp_partition_read(partition, i * chunk_size, store_data, chunk_size));
httpd_resp_send_chunk(req, store_data, chunk_size);
}
uint16_t pending_size = file_size - (i * chunk_size);
char pending_data[pending_size];
if (pending_size > 0) {
ESP_ERROR_CHECK(esp_partition_read(partition, i * chunk_size, pending_data,
pending_size));
httpd_resp_send_chunk(req, pending_data, pending_size);
}
httpd_resp_send_chunk(req, NULL, 0);
return ESP_OK;
}
extern const unsigned char index_start[] asm("_binary_index_html_start");
extern const unsigned char index_end[] asm("_binary_index_html_end");
static esp_err_t crash_get_handler(httpd_req_t *req) {
const size_t index_size = (index_end - index_start);
httpd_resp_set_type(req, "text/html");
httpd_resp_send_chunk(req, (const char *)index_start, index_size);
httpd_resp_send_chunk(req, NULL, 0);
assert(0);
return ESP_OK;
}
static const httpd_uri_t download = {
.uri = "/download",
.method = HTTP_GET,
.handler = download_get_handler,
.user_ctx = NULL,
};
static const httpd_uri_t crash = {
.uri = "/crash",
.method = HTTP_GET,
.handler = crash_get_handler,
.user_ctx = NULL,
};
static esp_err_t root_get_handler(httpd_req_t *req) {
const size_t index_size = (index_end - index_start);
httpd_resp_set_type(req, "text/html");
httpd_resp_send_chunk(req, (const char *)index_start, index_size);
httpd_resp_send_chunk(req, NULL, 0);
return ESP_OK;
}
static const httpd_uri_t root = {
.uri = "/",
.method = HTTP_GET,
.handler = root_get_handler,
.user_ctx = NULL,
};
static httpd_handle_t start_webserver(void) {
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.max_resp_headers = 1024;
config.lru_purge_enable = true;
// Start the httpd server
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &root);
httpd_register_uri_handler(server, &download);
httpd_register_uri_handler(server, &crash);
return server;
}
ESP_LOGI(TAG, "Error starting server!");
return NULL;
}
void app_main(void)
{
init_display();
const esp_timer_create_args_t periodic_timer_args = {
.callback = &presence_timer_callback,
/* name is optional, but may help identify the timer when debugging */
.name = "presence"
};
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &presence_timer));
/* Start the timers */
ESP_ERROR_CHECK(esp_timer_start_once(presence_timer, 30*1000*1000));
initPirSensor();
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
printf("Free heap size: %" PRIu32 " bytes\n", esp_get_free_heap_size());
heap_caps_print_heap_info(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
heap_caps_register_failed_alloc_callback(alloc_fail);
printf("1- Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
esp_log_level_set("wifi", ESP_LOG_ERROR);
esp_log_level_set(TAG, ESP_LOG_VERBOSE);
printf("2- Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
//mount_sd_card();
bsp_sdcard_mount();
//lv_log_register_print_cb(log_cb);
// LCD HW initialization
//ESP_ERROR_CHECK(app_lcd_init());
printf("4 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
// Touch initialization
//ESP_ERROR_CHECK(app_touch_init());
printf("5 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
// LVGL initialization
//ESP_ERROR_CHECK(app_lvgl_init());
printf("6 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
ESP_LOGI(TAG, "Initializing LittleFS");
esp_vfs_littlefs_conf_t conflfs = {
.base_path = "/littlefs",
.partition_label = "littlefs",
.format_if_mount_failed = true,
.dont_mount = false,
};
// Use settings defined above to initialize and mount LittleFS filesystem.
// Note: esp_vfs_littlefs_register is an all-in-one convenience function.
esp_err_t retlfs = esp_vfs_littlefs_register(&conflfs);
if (retlfs != ESP_OK){
if (retlfs == ESP_FAIL){
ESP_LOGE(TAG, "Failed to mount or format filesystem");
}else if (retlfs == ESP_ERR_NOT_FOUND){
ESP_LOGE(TAG, "Failed to find LittleFS partition");
}else{
ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(retlfs));
}
return;
}
size_t total = 0, used = 0;
retlfs = esp_littlefs_info(conflfs.partition_label, &total, &used);
if (retlfs != ESP_OK)
{
ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(retlfs));
}
else
{
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
}
// On affiche au plus tot l'ecran de démarrage
// ESP_ERROR_CHECK(esp_lcd_panel_mirror(lcd_panel,true,true));
/*
display_lock("app_main");
app_ota_display();
display_unlock("app_main");
for (size_t i = 0; i < 100; i++)
{
display_lock("app_main");
setOTAProgress(i);
display_unlock("app_main");
vTaskDelay(100/portTICK_PERIOD_MS);
}
*/
app_main_display();
printf("7 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
printf("8 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
start_wifi_logger();
wifi_log_e("test", "%s %d %f", "hello world wifi logger", 43, 45.341223242); // write log over wifi with log level -> ERROR
esp_log_level_set("tcp_handler", ESP_LOG_NONE);
printf("8b - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
printf("9 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
/* Ensure to disable any WiFi power save mode, this allows best throughput
* and hence timings for overall OTA operation.
*/
esp_wifi_set_ps(WIFI_PS_NONE);
xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 1, NULL);
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
// Is time set? If not, tm_year will be (1970 - 1900).
if (timeinfo.tm_year < (2016 - 1900))
{
ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
obtain_time();
// update 'now' variable with current time
time(&now);
}
printf("10. Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
heap_caps_print_heap_info(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
printf("11. Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
ESP_LOGW(TAG, "On telecharge l'image cuve");
TaskHandle_t xHandle = NULL;
BaseType_t ret1;
ret1 = xTaskCreate(&imgdwn, "imageDownload_task", 3 * 1024, NULL, 1, &xHandle);
if (ret1 != pdPASS)
{
ESP_LOGE(TAG, "Impossiblke de creer la tache imageDownload_task %i", ret1);
}
BaseType_t ret2 = xTaskCreate(&updateTime, "updateTimeTask", 3 * 1024, NULL, 1, NULL);
if (ret2 != pdPASS)
{
ESP_LOGE(TAG, "Impossiblke de creer la tache updateTimeTask %i", ret2);
}
printf("12. Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize());
// Show LVGL objects
if(display_lock("draw_ihm")){
draw_ihm();
display_unlock("draw_ihm");
}else{
ESP_LOGE(TAG,"Impossible d'obtenir le mutex pour draw_ihm");
}
mqtt_app_start();
while(!mainState.wifi_init){
vTaskDelay(pdTICKS_TO_MS(10));
}
start_webserver();
/*
// Configuration de la sonde Temp/Humid.
am2302_config_t am2302_config = {
.gpio_num = AM2302_GPIO,
};
am2302_rmt_config_t rmt_config = {
.clk_src = RMT_CLK_SRC_DEFAULT,
};
ESP_ERROR_CHECK(am2302_new_sensor_rmt(&am2302_config, &rmt_config, &sensor));
xTaskCreate(&readTempHumid, "read_temp_task", 8192, NULL, 5, NULL);
*/
}