diff --git a/components/communication/communication.c b/components/communication/communication.c index 131d183..45e4bc7 100644 --- a/components/communication/communication.c +++ b/components/communication/communication.c @@ -1,6 +1,5 @@ #include "communication.h" #include "esp_log.h" -#include "esp_lvgl_port.h" #include "mqtt_client.h" #include "bsp/esp-bsp.h" #include "stateManagement.h" @@ -324,40 +323,8 @@ static EventGroupHandle_t s_wifi_event_group; #define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK static int s_retry_num = 0; +wifi_callback cb; -static lv_subject_t wifiStatus; -LV_IMAGE_DECLARE(wifi_ok); -LV_IMAGE_DECLARE(wifi_ko); - -static void wifiStatus_obs_cb(lv_observer_t * observer, lv_subject_t * subject); - - -static void wifiStatus_obs_cb(lv_observer_t * observer, lv_subject_t * subject) -{ - ESP_LOGE(TAG, "On passe dans le callback de chgt de statut; %li", lv_subject_get_int(subject)); - bsp_display_lock(0); - lv_obj_t * wifiSt = lv_obj_get_child(lv_obj_get_child(lv_layer_top(), 0),2); - if(lv_obj_check_type(wifiSt, &lv_image_class)){ - switch (lv_subject_get_int(subject)) - { - case 0: - lv_image_set_src(wifiSt,&wifi_ko); - break; - case 1: - lv_image_set_src(wifiSt,&wifi_ok); - break; - default: - break; - } - }else{ - ESP_LOGE(TAG, "L'objet recuip en semble pas etre du bon type"); - } - bsp_display_unlock(); - //int32_t prev_v = lv_subject_get_previous_int(subject); - //int32_t cur_v = lv_subject_get_int(subject); - - //lv_obj_t * btn = lv_observer_get_target(observer); -} static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { @@ -367,11 +334,6 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t e } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 0"); - lv_subject_set_int(&wifiStatus,0); - lvgl_port_unlock(); - } if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { esp_wifi_connect(); @@ -382,25 +344,21 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t e { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } + cb(DISCONNECTED); ESP_LOGI(TAG, "connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 1"); - lv_subject_set_int(&wifiStatus,1); - lvgl_port_unlock(); - } ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); - mainState.wifi_init=true; } } -void wifi_init_sta(void) +void wifi_init_sta(wifi_callback callback) { - s_wifi_event_group = xEventGroupCreate(); + cb=callback; + //s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_netif_init()); @@ -442,48 +400,32 @@ void wifi_init_sta(void) ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "wifi_init_sta finished."); - lv_subject_init_int(&wifiStatus,0); - lv_subject_add_observer_obj(&wifiStatus, wifiStatus_obs_cb, NULL, NULL); - - /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum - * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ + /* + // Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum + // number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY); - /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually - * happened. */ + // xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually + // happened. if (bits & WIFI_CONNECTED_BIT) { - ESP_LOGI(TAG, "connected to ap SSID:%s", EXAMPLE_ESP_WIFI_SSID); - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 1"); - lv_subject_set_int(&wifiStatus,1); - lvgl_port_unlock(); - } + cb(CONNECTED); } else if (bits & WIFI_FAIL_BIT) { + cb(CONNECT_FAIL); ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 0"); - lv_subject_set_int(&wifiStatus,0); - lvgl_port_unlock(); - } } else { ESP_LOGE(TAG, "UNEXPECTED EVENT"); - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 0"); - lv_subject_set_int(&wifiStatus,0); - lvgl_port_unlock(); - } - } + */ } diff --git a/components/communication/include/communication.h b/components/communication/include/communication.h index aa115e7..0727cc0 100644 --- a/components/communication/include/communication.h +++ b/components/communication/include/communication.h @@ -1,2 +1,10 @@ void mqtt_app_start(void); -void wifi_init_sta(void); \ No newline at end of file +typedef enum wifi_evt{ + CONNECTED, + CONNECT_FAIL, + DISCONNECTED, + GOT_IP +} wifi_evt; +typedef void (*wifi_callback)(wifi_evt evt); +void wifi_init_sta(wifi_callback cb); + diff --git a/main/ihm.c b/main/ihm.c index a4643ea..6241165 100644 --- a/main/ihm.c +++ b/main/ihm.c @@ -123,10 +123,6 @@ void app_main_display() lv_subject_init_pointer(&forecastH2Subj, &d1); lv_subject_init_pointer(&forecastH3Subj, &d1); - on_weather_data_retrieval(weather_data_retreived); - on_weather_data_retrieval_start(weather_data_retreived_start); - initialise_weather_data_retrieval(600000); - ESP_LOGW(TAG, "Weather data retrieval initialized"); lv_theme_t * th = lv_theme_domotic_init(lv_display_get_default()); lv_display_set_theme(lv_disp_get_default(), th); /* Assign theme to display */ @@ -845,56 +841,7 @@ void draw_ihm() lv_obj_set_flex_flow(tabMeteo, LV_FLEX_FLOW_COLUMN); } -void weather_data_retreived_start() -{ - //if (display_lock("weather_data_retreived_start")) - //{ - //ESP_LOGE(TAG,"Mutex obtenu dans weather_data_retreived_start"); - lv_subject_set_int(&meteoStatus, 1); - ESP_LOGE(TAG,"Subject setted weather_data_retreived_start"); - //display_unlock("weather_data_retreived_start"); - //}else{ - // ESP_LOGE(TAG,"Impossible d'obtenir le mutex dans weather_data_retreived_start"); - //} -} -void weather_data_retreived(struct meteodailyforecast_data dailyDatas[3], struct meteoforecast_data datas[3]) -{ - -/* ESP_LOGE(TAG, "debut debug"); - printf("%lld\n", datas[0].datetime); - printf("%s\n", datas[0].previsions.desc); - printf("%f\n", datas[0].previsions.value); - printf("%lld\n", datas[1].datetime); - printf("%s\n", datas[1].previsions.desc); - printf("%f\n", datas[1].previsions.value); - printf("%lld\n", datas[2].datetime); - printf("%s\n", datas[2].previsions.desc); - printf("%f\n", datas[2].previsions.value); - ESP_LOGE(TAG, "fin debug"); -*/ - if (display_lock("weather_data_retreived")) - { - ESP_LOGV("MeteoFrance", "------------------------------------- Set des subjects J --------------------------------"); - // Prévisions des 3 prochains jours - lv_subject_set_pointer(&forecastD1Subj, &dailyDatas[0]); - lv_subject_set_pointer(&forecastD2Subj, &dailyDatas[1]); - lv_subject_set_pointer(&forecastD3Subj, &dailyDatas[2]); - - ESP_LOGV("MeteoFrance", "------------------------------------- Set des subjects H--------------------------------"); - // Prévisions des 3 prochains jours - ESP_LOGV("MeteoFrance", "Pointeur %lli", datas[0].datetime); - lv_subject_set_pointer(&forecastH1Subj, &datas[0]); - lv_subject_set_pointer(&forecastH2Subj, &datas[1]); - lv_subject_set_pointer(&forecastH3Subj, &datas[2]); - - lv_subject_set_int(&meteoStatus, 0); - display_unlock("weather_data_retreived"); - ESP_LOGV(TAG, "------------------------------------- Fin Set des subjects --------------------------------"); - }else{ - ESP_LOGE(TAG, "Impossible d'obtenir le mutex dans weather_data_retreived"); - } -} void log_cb(lv_log_level_t level, const char * buf){ ESP_LOGE(TAG, "%s",buf); } diff --git a/main/main.c b/main/main.c index a6aba0d..32c2c4a 100644 --- a/main/main.c +++ b/main/main.c @@ -51,6 +51,65 @@ static const char *TAG = "domoTic"; extern esp_mqtt_client_handle_t client; +typedef enum domo_events{ + WIFI_CONNECTED, + TIME_SETTED +} domo_events; + +static EventGroupHandle_t hevt; +#define WIFI_RDY 0b0001 + +static lv_subject_t wifiStatus; +LV_IMAGE_DECLARE(wifi_ok); +LV_IMAGE_DECLARE(wifi_ko); + +static void wifiStatus_obs_cb(lv_observer_t * observer, lv_subject_t * subject); + + +static void wifiStatus_obs_cb(lv_observer_t * observer, lv_subject_t * subject) +{ + ESP_LOGE(TAG, "On passe dans le callback de chgt de statut; %li", lv_subject_get_int(subject)); + bsp_display_lock(0); + lv_obj_t * wifiSt = lv_obj_get_child(lv_obj_get_child(lv_layer_top(), 0),2); + if(lv_obj_check_type(wifiSt, &lv_image_class)){ + switch (lv_subject_get_int(subject)) + { + case 0: + lv_image_set_src(wifiSt,&wifi_ko); + break; + case 1: + lv_image_set_src(wifiSt,&wifi_ok); + break; + default: + break; + } + }else{ + ESP_LOGE(TAG, "L'objet recuip en semble pas etre du bon type"); + } + bsp_display_unlock(); + //int32_t prev_v = lv_subject_get_previous_int(subject); + //int32_t cur_v = lv_subject_get_int(subject); + + //lv_obj_t * btn = lv_observer_get_target(observer); +} + +void send_event(domo_events evt){ + switch(evt){ + case WIFI_CONNECTED: + xEventGroupSetBits(hevt,WIFI_RDY); + 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_LOGI(TAG, "connected to ap SSID"); + if(lvgl_port_lock(0)){ + ESP_LOGE(TAG,"Statut Wifi 1"); + lv_subject_set_int(&wifiStatus,1); + lvgl_port_unlock(); + } + break; + case TIME_SETTED: + break; + } +} struct state mainState={ .wifi_init=false, @@ -238,8 +297,105 @@ void alloc_fail(size_t size, uint32_t caps, const char * function_name){ } +void wifi_cb(wifi_evt evt){ + switch(evt){ + case CONNECTED: + send_event(WIFI_CONNECTED); + case DISCONNECTED: + if(lvgl_port_lock(0)){ + ESP_LOGE(TAG,"Statut Wifi 0"); + lv_subject_set_int(&wifiStatus,0); + lvgl_port_unlock(); + } + break; + case GOT_IP: + if(lvgl_port_lock(0)){ + ESP_LOGE(TAG,"Statut Wifi 1"); + lv_subject_set_int(&wifiStatus,1); + lvgl_port_unlock(); + } + break; + case CONNECT_FAIL: + if(lvgl_port_lock(0)){ + ESP_LOGE(TAG,"Statut Wifi 0"); + lv_subject_set_int(&wifiStatus,0); + lvgl_port_unlock(); + } + break; + default: + if(lvgl_port_lock(0)){ + ESP_LOGE(TAG,"Statut Wifi 0"); + lv_subject_set_int(&wifiStatus,0); + lvgl_port_unlock(); + } + break; + } + +} + +extern lv_subject_t forecastD1Subj; +extern lv_subject_t forecastD2Subj; +extern lv_subject_t forecastD3Subj; +extern lv_subject_t forecastH1Subj; +extern lv_subject_t forecastH2Subj; +extern lv_subject_t forecastH3Subj; +extern lv_subject_t meteoStatus; + +void weather_data_retreived(struct meteodailyforecast_data dailyDatas[3], struct meteoforecast_data datas[3]) +{ + +/* ESP_LOGE(TAG, "debut debug"); + printf("%lld\n", datas[0].datetime); + printf("%s\n", datas[0].previsions.desc); + printf("%f\n", datas[0].previsions.value); + printf("%lld\n", datas[1].datetime); + printf("%s\n", datas[1].previsions.desc); + printf("%f\n", datas[1].previsions.value); + printf("%lld\n", datas[2].datetime); + printf("%s\n", datas[2].previsions.desc); + printf("%f\n", datas[2].previsions.value); + ESP_LOGE(TAG, "fin debug"); +*/ + if (display_lock("weather_data_retreived")) + { + ESP_LOGV("MeteoFrance", "------------------------------------- Set des subjects J --------------------------------"); + // Prévisions des 3 prochains jours + lv_subject_set_pointer(&forecastD1Subj, &dailyDatas[0]); + lv_subject_set_pointer(&forecastD2Subj, &dailyDatas[1]); + lv_subject_set_pointer(&forecastD3Subj, &dailyDatas[2]); + + ESP_LOGV("MeteoFrance", "------------------------------------- Set des subjects H--------------------------------"); + // Prévisions des 3 prochains jours + ESP_LOGV("MeteoFrance", "Pointeur %lli", datas[0].datetime); + lv_subject_set_pointer(&forecastH1Subj, &datas[0]); + lv_subject_set_pointer(&forecastH2Subj, &datas[1]); + lv_subject_set_pointer(&forecastH3Subj, &datas[2]); + + lv_subject_set_int(&meteoStatus, 0); + display_unlock("weather_data_retreived"); + ESP_LOGV(TAG, "------------------------------------- Fin Set des subjects --------------------------------"); + }else{ + ESP_LOGE(TAG, "Impossible d'obtenir le mutex dans weather_data_retreived"); + } +} + +void weather_data_retreived_start() +{ + //if (display_lock("weather_data_retreived_start")) + //{ + //ESP_LOGE(TAG,"Mutex obtenu dans weather_data_retreived_start"); + lv_subject_set_int(&meteoStatus, 1); + ESP_LOGE(TAG,"Subject setted weather_data_retreived_start"); + //display_unlock("weather_data_retreived_start"); + //}else{ + // ESP_LOGE(TAG,"Impossible d'obtenir le mutex dans weather_data_retreived_start"); + //} +} + + void app_main(void) { + 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); @@ -324,14 +480,21 @@ void app_main(void) 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 + + lv_subject_init_int(&wifiStatus,0); + lv_subject_add_observer_obj(&wifiStatus, wifiStatus_obs_cb, NULL, NULL); + wifi_init_sta(wifi_cb); + // 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()); + on_weather_data_retrieval(weather_data_retreived); + on_weather_data_retrieval_start(weather_data_retreived_start); + initialise_weather_data_retrieval(600000); + ESP_LOGW(TAG, "Weather data retrieval initialized"); + time_t now; struct tm timeinfo; time(&now); diff --git a/main/obtain_time.c b/main/obtain_time.c index 88f89a9..45796b1 100644 --- a/main/obtain_time.c +++ b/main/obtain_time.c @@ -17,8 +17,6 @@ void time_sync_notification_cb(struct timeval *tv) char strftime_buf[64]; // Set timezone to Eastern Standard Time and print local time - setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1); - tzset(); time_t now; struct tm timeinfo; localtime_r(&now, &timeinfo); @@ -44,6 +42,8 @@ void updateTime(void *pvParameter) while (1) { time(&now); + setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1); + tzset(); struct tm timeinfo = {0}; localtime_r(&now, &timeinfo); sprintf(strftime_buf, "%s %d %s %02d:%02d", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon], timeinfo.tm_hour, timeinfo.tm_min); diff --git a/sdkconfig b/sdkconfig index a9e9305..664de92 100644 --- a/sdkconfig +++ b/sdkconfig @@ -1041,7 +1041,7 @@ CONFIG_ESP_HTTP_CLIENT_EVENT_POST_TIMEOUT=2000 # ESP HTTPS OTA # # CONFIG_ESP_HTTPS_OTA_DECRYPT_CB is not set -CONFIG_ESP_HTTPS_OTA_ALLOW_HTTP=y +# CONFIG_ESP_HTTPS_OTA_ALLOW_HTTP is not set CONFIG_ESP_HTTPS_OTA_EVENT_POST_TIMEOUT=2000 # end of ESP HTTPS OTA @@ -2921,7 +2921,7 @@ CONFIG_STACK_CHECK_NONE=y # CONFIG_EVENT_LOOP_PROFILING is not set CONFIG_POST_EVENTS_FROM_ISR=y CONFIG_POST_EVENTS_FROM_IRAM_ISR=y -CONFIG_OTA_ALLOW_HTTP=y +# CONFIG_OTA_ALLOW_HTTP is not set CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304 CONFIG_MAIN_TASK_STACK_SIZE=3584