diff --git a/components/meteofrance/include/meteofrance.h b/components/meteofrance/include/meteofrance.h index a926b8c..b469446 100644 --- a/components/meteofrance/include/meteofrance.h +++ b/components/meteofrance/include/meteofrance.h @@ -18,7 +18,7 @@ struct forecast_temp{ }; struct meteoforecast_data{ - int datetime; + time_t datetime; bool isValid; struct forecast_temp previsions; }; diff --git a/components/meteofrance/meteofrance.c b/components/meteofrance/meteofrance.c index a99f331..5c5c2d2 100644 --- a/components/meteofrance/meteofrance.c +++ b/components/meteofrance/meteofrance.c @@ -50,10 +50,14 @@ void printftemp(struct forecast_temp *tmp){ #define MAX_SIZE 80 void printffd(struct meteoforecast_data *tmp){ - struct tm * pTime=localtime( & tmp->datetime ); + struct tm timeinfo = { 0 }; + localtime_r(&tmp->datetime, &timeinfo); char buffer[ MAX_SIZE ]; - strftime(buffer, MAX_SIZE, "%d/%m/%Y", pTime); - printf("IsValid:%s date:%d, Min:%.1f Max:%.1f Desc:%s\n", tmp->isValid ?"true":"false", tmp->datetime, tmp->previsions.min, tmp->previsions.max, tmp->previsions.desc); + strftime(buffer, MAX_SIZE, "%c", &timeinfo); + char strftime_buf[64]; + sprintf(strftime_buf,"%d %d %d", timeinfo.tm_wday, timeinfo.tm_mday, timeinfo.tm_mon+1); + ESP_LOGE(TAG,"%s",strftime_buf); + printf("IsValid:%s date:%s, Min:%.1f Max:%.1f Desc:%s\n", tmp->isValid ?"true":"false", buffer, tmp->previsions.min, tmp->previsions.max, tmp->previsions.desc); } esp_err_t _http_event_handler(esp_http_client_event_t *evt) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 0ed2572..1830cf8 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register(SRC_DIRS . fonts INCLUDE_DIRS "." - REQUIRES esp_wifi nvs_flash mqtt meteofrance) + REQUIRES esp_wifi nvs_flash mqtt meteofrance esp_netif) lvgl_port_create_c_image("images/esp_logo.png" "images/" "ARGB8888" "NONE") lvgl_port_create_c_image("images/p10j.png" "images/" "ARGB8888" "NONE") diff --git a/main/fonts/Dockerfile b/main/fonts/Dockerfile new file mode 100644 index 0000000..6de4907 --- /dev/null +++ b/main/fonts/Dockerfile @@ -0,0 +1,2 @@ +FROM node:14 +RUN npm i lv_font_conv -g diff --git a/main/fonts/Montserrat-Medium.ttf b/main/fonts/Montserrat-Medium.ttf new file mode 100644 index 0000000..6e079f6 Binary files /dev/null and b/main/fonts/Montserrat-Medium.ttf differ diff --git a/main/fonts/fa-solid-900.ttf b/main/fonts/fa-solid-900.ttf new file mode 100644 index 0000000..e7e2ecf Binary files /dev/null and b/main/fonts/fa-solid-900.ttf differ diff --git a/main/fonts/montserrat_medium_12.c b/main/fonts/montserrat_medium_12.c index a1e71c7..0892a30 100644 --- a/main/fonts/montserrat_medium_12.c +++ b/main/fonts/montserrat_medium_12.c @@ -1,7 +1,7 @@ /******************************************************************************* * Size: 12 px * Bpp: 4 - * Opts: --bpp 4 --size 12 --no-compress --font Montserrat-Medium.ttf --symbols 0123456789.°éêCABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' --format lvgl -o montserrat_medium_12.c + * Opts: --bpp 4 --size 12 --no-compress --font Montserrat-Medium.ttf --symbols 0123456789.°èéêûCABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' --format lvgl -o montserrat_medium_12.c ******************************************************************************/ #ifdef LV_LVGL_H_INCLUDE_SIMPLE @@ -442,6 +442,13 @@ static LV_ATTRIBUTE_LARGE_CONST const uint8_t glyph_bitmap[] = { 0x6, 0xb7, 0x3, 0x80, 0x84, 0x64, 0x3, 0x73, 0x80, 0x84, 0x6, 0xb7, 0x0, + /* U+00E8 "è" */ + 0x2, 0xd5, 0x0, 0x0, 0x1, 0xc3, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x2b, 0xfd, 0x50, 0xe, 0x81, + 0x4e, 0x45, 0xc0, 0x0, 0x6b, 0x7f, 0xee, 0xee, + 0xc5, 0xd0, 0x0, 0x0, 0xe, 0x92, 0x3a, 0x20, + 0x2b, 0xfe, 0x90, + /* U+00E9 "é" */ 0x0, 0x1, 0xe5, 0x0, 0x0, 0xb5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2b, 0xfd, 0x50, 0xe, 0x81, @@ -454,7 +461,14 @@ static LV_ATTRIBUTE_LARGE_CONST const uint8_t glyph_bitmap[] = { 0x0, 0x0, 0x0, 0x2b, 0xfd, 0x50, 0xe, 0x81, 0x4e, 0x45, 0xc0, 0x0, 0x6b, 0x7f, 0xee, 0xee, 0xc5, 0xd0, 0x0, 0x0, 0xe, 0x92, 0x3a, 0x20, - 0x2b, 0xfe, 0x90 + 0x2b, 0xfe, 0x90, + + /* U+00FB "û" */ + 0x1, 0xdd, 0x10, 0x0, 0xa5, 0x4b, 0x0, 0x0, + 0x0, 0x0, 0xf, 0x30, 0x2, 0xf0, 0xf3, 0x0, + 0x2f, 0xf, 0x30, 0x2, 0xf0, 0xf3, 0x0, 0x2f, + 0xe, 0x40, 0x4, 0xf0, 0xab, 0x22, 0xcf, 0x1, + 0xbf, 0xe6, 0xf0 }; @@ -531,7 +545,9 @@ static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = { {.bitmap_index = 2041, .adv_w = 100, .box_w = 6, .box_h = 7, .ofs_x = 0, .ofs_y = 0}, {.bitmap_index = 2062, .adv_w = 80, .box_w = 5, .box_h = 5, .ofs_x = 0, .ofs_y = 5}, {.bitmap_index = 2075, .adv_w = 118, .box_w = 7, .box_h = 10, .ofs_x = 0, .ofs_y = 0}, - {.bitmap_index = 2110, .adv_w = 118, .box_w = 7, .box_h = 10, .ofs_x = 0, .ofs_y = 0} + {.bitmap_index = 2110, .adv_w = 118, .box_w = 7, .box_h = 10, .ofs_x = 0, .ofs_y = 0}, + {.bitmap_index = 2145, .adv_w = 118, .box_w = 7, .box_h = 10, .ofs_x = 0, .ofs_y = 0}, + {.bitmap_index = 2180, .adv_w = 130, .box_w = 7, .box_h = 10, .ofs_x = 1, .ofs_y = 0} }; /*--------------------- @@ -543,7 +559,7 @@ static const uint16_t unicode_list_0[] = { }; static const uint16_t unicode_list_4[] = { - 0x0, 0x39, 0x3a + 0x0, 0x38, 0x39, 0x3a, 0x4b }; /*Collect the unicode lists and glyph_id offsets*/ @@ -566,8 +582,8 @@ static const lv_font_fmt_txt_cmap_t cmaps[] = .unicode_list = NULL, .glyph_id_ofs_list = NULL, .list_length = 0, .type = LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY }, { - .range_start = 176, .range_length = 59, .glyph_id_start = 66, - .unicode_list = unicode_list_4, .glyph_id_ofs_list = NULL, .list_length = 3, .type = LV_FONT_FMT_TXT_CMAP_SPARSE_TINY + .range_start = 176, .range_length = 76, .glyph_id_start = 66, + .unicode_list = unicode_list_4, .glyph_id_ofs_list = NULL, .list_length = 5, .type = LV_FONT_FMT_TXT_CMAP_SPARSE_TINY } }; @@ -587,7 +603,7 @@ static const uint8_t kern_left_class_mapping[] = 30, 31, 32, 33, 34, 35, 36, 30, 36, 36, 37, 33, 30, 30, 31, 31, 38, 39, 40, 41, 36, 42, 42, 43, - 42, 44, 45, 34, 34 + 42, 44, 45, 34, 34, 34, 36 }; /*Map glyph_ids to kern right classes*/ @@ -601,7 +617,7 @@ static const uint8_t kern_right_class_mapping[] = 23, 24, 25, 25, 25, 0, 25, 24, 26, 27, 24, 24, 28, 28, 25, 28, 25, 28, 29, 30, 31, 32, 32, 33, - 32, 34, 35, 25, 25 + 32, 34, 35, 25, 25, 25, 31 }; /*Kern values between classes*/ diff --git a/main/fonts/montserrat_medium_18.c b/main/fonts/montserrat_medium_18.c index 3ef3fd6..f00357b 100644 --- a/main/fonts/montserrat_medium_18.c +++ b/main/fonts/montserrat_medium_18.c @@ -1,7 +1,7 @@ /******************************************************************************* * Size: 18 px * Bpp: 4 - * Opts: --bpp 4 --size 18 --no-compress --font Montserrat-Medium.ttf --symbols 0123456789.°éCABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' --format lvgl -o montserrat_medium_18.c + * Opts: --bpp 4 --size 18 --no-compress --font Montserrat-Medium.ttf --symbols 0123456789.°éûCABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' --format lvgl -o montserrat_medium_18.c --font fa-solid-900.ttf --range 61461 ******************************************************************************/ #ifdef LV_LVGL_H_INCLUDE_SIMPLE @@ -732,7 +732,45 @@ static LV_ATTRIBUTE_LARGE_CONST const uint8_t glyph_bitmap[] = { 0xff, 0x33, 0xfa, 0x33, 0x33, 0x33, 0x30, 0xf, 0xd0, 0x0, 0x0, 0x0, 0x0, 0x9f, 0x90, 0x0, 0x9, 0x10, 0x0, 0xdf, 0xda, 0xae, 0xf6, 0x0, - 0x0, 0x7d, 0xff, 0xc5, 0x0 + 0x0, 0x7d, 0xff, 0xc5, 0x0, + + /* U+00FB "û" */ + 0x0, 0x4, 0xff, 0x50, 0x0, 0x0, 0x3f, 0x87, + 0xf5, 0x0, 0x0, 0x75, 0x0, 0x47, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x7f, 0x40, 0x0, 0x3, + 0xf8, 0x7f, 0x40, 0x0, 0x3, 0xf8, 0x7f, 0x40, + 0x0, 0x3, 0xf8, 0x7f, 0x40, 0x0, 0x3, 0xf8, + 0x7f, 0x40, 0x0, 0x3, 0xf8, 0x7f, 0x40, 0x0, + 0x4, 0xf8, 0x6f, 0x60, 0x0, 0x6, 0xf8, 0x2f, + 0xc0, 0x0, 0x1e, 0xf8, 0xa, 0xfd, 0x89, 0xee, + 0xf8, 0x0, 0x8d, 0xfe, 0x92, 0xf8, + + /* U+F015 "" */ + 0x0, 0x0, 0x0, 0x0, 0xb, 0xd3, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1d, 0xff, + 0xf5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x3e, 0xff, 0xff, 0xf6, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x4f, 0xff, 0xff, 0xff, 0xf8, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x6f, 0xff, 0xff, 0xff, + 0xff, 0xfa, 0x0, 0x0, 0x0, 0x0, 0x8f, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xfc, 0x10, 0x0, 0x0, + 0xaf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, + 0x20, 0x1, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xfe, 0x30, 0xcf, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x2c, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xf2, 0x2, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0x31, 0x0, 0xb, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xf0, 0x0, 0x0, 0xbf, + 0xff, 0xff, 0x96, 0x67, 0xff, 0xff, 0xff, 0x0, + 0x0, 0xb, 0xff, 0xff, 0xf0, 0x0, 0x9, 0xff, + 0xff, 0xf1, 0x0, 0x0, 0xbf, 0xff, 0xfe, 0x0, + 0x0, 0x9f, 0xff, 0xff, 0x10, 0x0, 0xb, 0xff, + 0xff, 0xe0, 0x0, 0x9, 0xff, 0xff, 0xf1, 0x0, + 0x0, 0xbf, 0xff, 0xfe, 0x0, 0x0, 0x9f, 0xff, + 0xff, 0x0, 0x0, 0x5, 0xff, 0xff, 0x80, 0x0, + 0x3, 0xff, 0xff, 0xa0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; @@ -808,7 +846,9 @@ static const lv_font_fmt_txt_glyph_dsc_t glyph_dsc[] = { {.bitmap_index = 4170, .adv_w = 161, .box_w = 12, .box_h = 14, .ofs_x = -1, .ofs_y = -4}, {.bitmap_index = 4254, .adv_w = 150, .box_w = 9, .box_h = 10, .ofs_x = 0, .ofs_y = 0}, {.bitmap_index = 4299, .adv_w = 121, .box_w = 7, .box_h = 7, .ofs_x = 0, .ofs_y = 6}, - {.bitmap_index = 4324, .adv_w = 176, .box_w = 11, .box_h = 14, .ofs_x = 0, .ofs_y = 0} + {.bitmap_index = 4324, .adv_w = 176, .box_w = 11, .box_h = 14, .ofs_x = 0, .ofs_y = 0}, + {.bitmap_index = 4401, .adv_w = 195, .box_w = 10, .box_h = 14, .ofs_x = 1, .ofs_y = 0}, + {.bitmap_index = 4471, .adv_w = 324, .box_w = 21, .box_h = 19, .ofs_x = 0, .ofs_y = -3} }; /*--------------------- @@ -820,7 +860,7 @@ static const uint16_t unicode_list_0[] = { }; static const uint16_t unicode_list_4[] = { - 0x0, 0x39 + 0x0, 0x39, 0x4b, 0xef65 }; /*Collect the unicode lists and glyph_id offsets*/ @@ -843,8 +883,8 @@ static const lv_font_fmt_txt_cmap_t cmaps[] = .unicode_list = NULL, .glyph_id_ofs_list = NULL, .list_length = 0, .type = LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY }, { - .range_start = 176, .range_length = 58, .glyph_id_start = 66, - .unicode_list = unicode_list_4, .glyph_id_ofs_list = NULL, .list_length = 2, .type = LV_FONT_FMT_TXT_CMAP_SPARSE_TINY + .range_start = 176, .range_length = 61286, .glyph_id_start = 66, + .unicode_list = unicode_list_4, .glyph_id_ofs_list = NULL, .list_length = 4, .type = LV_FONT_FMT_TXT_CMAP_SPARSE_TINY } }; @@ -864,7 +904,7 @@ static const uint8_t kern_left_class_mapping[] = 30, 31, 32, 33, 34, 35, 36, 30, 36, 36, 37, 33, 30, 30, 31, 31, 38, 39, 40, 41, 36, 42, 42, 43, - 42, 44, 45, 34 + 42, 44, 45, 34, 36, 0 }; /*Map glyph_ids to kern right classes*/ @@ -878,7 +918,7 @@ static const uint8_t kern_right_class_mapping[] = 23, 24, 25, 25, 25, 0, 25, 24, 26, 27, 24, 24, 28, 28, 25, 28, 25, 28, 29, 30, 31, 32, 32, 33, - 32, 34, 35, 25 + 32, 34, 35, 25, 31, 0 }; /*Kern values between classes*/ @@ -1136,7 +1176,7 @@ lv_font_t montserrat_medium_18 = { #endif .get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt, /*Function pointer to get glyph's data*/ .get_glyph_bitmap = lv_font_get_bitmap_fmt_txt, /*Function pointer to get glyph's bitmap*/ - .line_height = 18, /*The maximum line height required by the font*/ + .line_height = 20, /*The maximum line height required by the font*/ .base_line = 4, /*Baseline measured from the bottom of the line*/ #if !(LVGL_VERSION_MAJOR == 6 && LVGL_VERSION_MINOR == 0) .subpx = LV_FONT_SUBPX_NONE, diff --git a/main/main.c b/main/main.c index a9705ec..0d20d27 100644 --- a/main/main.c +++ b/main/main.c @@ -14,11 +14,17 @@ #include "nvs_flash.h" #include "mqtt_client.h" #include "meteofrance.h" +#include "esp_netif_sntp.h" +#include "esp_sntp.h" +#include #include "esp_lcd_touch_gt911.h" LV_FONT_DECLARE(montserrat_medium_12) LV_FONT_DECLARE(montserrat_medium_18) +char *days[7]={"Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi"}; +char *months[12]={"Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"}; + /* LCD size */ #define EXAMPLE_LCD_H_RES (800) #define EXAMPLE_LCD_V_RES (480) @@ -553,6 +559,7 @@ void draw_ihm() lv_obj_t * tab_buttons = lv_tabview_get_tab_bar(tabview); + lv_obj_set_width(tab_buttons,100); lv_obj_set_style_text_font(tab_buttons, &montserrat_medium_18, 0); lv_obj_set_style_bg_color(tab_buttons, lv_palette_darken(LV_PALETTE_GREY, 3), 0); lv_obj_set_style_text_color(tab_buttons, lv_palette_lighten(LV_PALETTE_GREY, 5), 0); @@ -601,17 +608,36 @@ void draw_ihm() static lv_style_t style_lbvValue; lv_style_init(&style_lbvValue); - lv_obj_t *main = tab1; - lv_obj_add_style(main,&no_padding,0); lv_style_set_text_font(&style_lbvValue, &lv_font_montserrat_40); + lv_obj_t *supmain = tab1; + lv_obj_set_flex_flow(supmain, LV_FLEX_FLOW_COLUMN); + + lv_obj_t *jour = lv_label_create(supmain); + lv_obj_add_style(jour,&style_font_16,0); + + setlocale(LC_ALL, "fr_FR"); + char strftime_buf[64]; + time_t now = 0; + time(&now); + struct tm timeinfo = { 0 }; + localtime_r(&now, &timeinfo); + sprintf(strftime_buf,"%s %d %s", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon]); + + lv_label_set_text(jour,strftime_buf); + lv_obj_align(jour, LV_ALIGN_CENTER,0,0); + + lv_obj_t *main = lv_obj_create(supmain); + lv_obj_add_style(main,&no_padding,0); + lv_obj_set_size(main,LV_SIZE_CONTENT,LV_SIZE_CONTENT); + lv_obj_set_flex_flow(main, LV_FLEX_FLOW_ROW); lv_obj_set_style_pad_column(main, 1, 0); /*Create a container with COLUMN flex direction*/ lv_obj_t *cont_col = lv_obj_create(main); lv_obj_set_style_pad_all(cont_col, 5, 0); - lv_obj_set_size(cont_col, 250, 480); + lv_obj_set_size(cont_col, 250, 400); lv_obj_align(cont_col, LV_ALIGN_TOP_LEFT, 0, 0); lv_obj_set_flex_flow(cont_col, LV_FLEX_FLOW_COLUMN); lv_obj_set_flex_align(cont_col,LV_FLEX_ALIGN_CENTER,LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); @@ -622,13 +648,13 @@ void draw_ihm() lv_obj_set_flex_align(cont_col2, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); /*Create a container with COLUMN flex direction*/ - lv_obj_t *cont_col3 = lv_obj_create(tab1); + lv_obj_t *cont_col3 = lv_obj_create(main); lv_obj_set_style_pad_all(cont_col3, 5, 0); - lv_obj_set_size(cont_col3, 300, 480); + lv_obj_set_size(cont_col3, 300, 400); lv_obj_set_flex_flow(cont_col3, LV_FLEX_FLOW_COLUMN); lv_obj_set_flex_align(cont_col3, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); - lv_obj_set_height(cont_col2, 480); + lv_obj_set_height(cont_col2, 400); lv_obj_set_width(cont_col2, LV_SIZE_CONTENT); lv_obj_align_to(cont_col2, cont_col, LV_ALIGN_OUT_TOP_RIGHT, 0, 0); lv_obj_set_flex_flow(cont_col2, LV_FLEX_FLOW_COLUMN); @@ -1122,13 +1148,13 @@ void weather_data_retreived(struct meteoforecast_data datas[3]) // struct meteoforecast_data* weather = (meteoforecast_data*) args; struct meteoforecast_data j1 = {}; ESP_LOGE(TAG, "debut debug"); - printf("%i\n", datas[0].datetime); + printf("%lld\n", datas[0].datetime); printf("%s\n", datas[0].previsions.desc); printf("%f\n", datas[0].previsions.min); - printf("%i\n", datas[1].datetime); + printf("%lld\n", datas[1].datetime); printf("%s\n", datas[1].previsions.desc); printf("%f\n", datas[1].previsions.min); - printf("%i\n", datas[2].datetime); + printf("%lld\n", datas[2].datetime); printf("%s\n", datas[2].previsions.desc); printf("%f\n", datas[2].previsions.min); ESP_LOGE(TAG, "fin debug"); @@ -1144,9 +1170,15 @@ void weather_data_retreived(struct meteoforecast_data datas[3]) ESP_LOGE(TAG, "------------------------------------- Fin Set des subjects --------------------------------"); } } +static void obtain_time(void); +void time_sync_notification_cb(struct timeval *tv) +{ + ESP_LOGI(TAG, "Notification of a time synchronization event"); +} void app_main(void) { + /* LCD HW initialization */ ESP_ERROR_CHECK(app_lcd_init()); @@ -1165,6 +1197,7 @@ void app_main(void) } ESP_ERROR_CHECK(ret); + /* Show LVGL objects */ if(lvgl_port_lock(0)){ app_main_display(); @@ -1174,6 +1207,26 @@ void app_main(void) wifi_init_sta(); mqtt_app_start(); + 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); + } + + 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(); + localtime_r(&now, &timeinfo); + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + on_weather_data_retrieval(weather_data_retreived); on_weather_data_retrieval_start(weather_data_retreived_start); initialise_weather_data_retrieval(60000); @@ -1186,3 +1239,25 @@ void app_main(void) lvgl_port_unlock(); } } + +static void obtain_time(void) +{ + ESP_LOGI(TAG, "Initializing and starting SNTP"); + esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org"); + config.sync_cb = time_sync_notification_cb; // Note: This is only needed if we want + esp_netif_sntp_init(&config); + + // wait for time to be set + time_t now = 0; + struct tm timeinfo = { 0 }; + int retry = 0; + const int retry_count = 15; + while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT && ++retry < retry_count) { + ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); + } + time(&now); + localtime_r(&now, &timeinfo); + + esp_netif_sntp_deinit(); +} +