From b7080c359a4060f59be1c659f3002ea00abf79c5 Mon Sep 17 00:00:00 2001 From: rxf Date: Mon, 8 Jul 2024 15:58:12 +0200 Subject: [PATCH] WIP_20240708_1558 --- src/grafik.cpp | 43 ++++++++++++++++++++++++++----------------- src/grafik.h | 8 ++++++-- src/main.cpp | 39 ++++++++++++++++++++++++++++++--------- src/weifi.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/weifi.h | 16 ++++++++++++++++ 5 files changed, 121 insertions(+), 28 deletions(-) diff --git a/src/grafik.cpp b/src/grafik.cpp index 4fe5344..ccf2dad 100644 --- a/src/grafik.cpp +++ b/src/grafik.cpp @@ -42,7 +42,7 @@ void buildarc(ARC arc) { char valuelow[10]; sprintf(valuelow, "%d", arc.lowvalue); lv_label_set_text(low_label, valuelow); - lv_obj_align(low_label, LV_ALIGN_LEFT_MID, 0, 8); + lv_obj_align(low_label, LV_ALIGN_LEFT_MID, 5, 8); static lv_style_t style_low_label; lv_style_init(&style_low_label); lv_style_set_text_font(&style_low_label, &lv_font_montserrat_10); @@ -81,27 +81,27 @@ void buildarc(ARC arc) { void lv_create_main_gui(void) { // Create a text label aligned on top: https://docs.lvgl.io/master/widgets/label.html - lv_obj_t * date_label = lv_label_create(lv_screen_active()); - lv_label_set_text(date_label, "17:30"); - lv_obj_align(date_label, LV_ALIGN_TOP_MID, 0, 10); - - // Set font type and font size. More information: https://docs.lvgl.io/master/overview/font.html - static lv_style_t style_date_label; - lv_style_init(&style_date_label); - lv_style_set_text_font(&style_date_label, &lv_font_montserrat_28); - lv_obj_add_style(date_label, &style_date_label, 0); - - // Create a text label aligned on top: https://docs.lvgl.io/master/widgets/label.html - lv_obj_t * time_label = lv_label_create(lv_screen_active()); - lv_label_set_text(time_label, "Dienstag, 12. Dezember 2024"); - lv_obj_align(time_label, LV_ALIGN_TOP_MID, 0, 40); + time_label = lv_label_create(lv_screen_active()); + lv_label_set_text(time_label, "17:30"); + lv_obj_align(time_label, LV_ALIGN_TOP_MID, 0, 10); // Set font type and font size. More information: https://docs.lvgl.io/master/overview/font.html static lv_style_t style_time_label; lv_style_init(&style_time_label); - lv_style_set_text_font(&style_time_label, &lv_font_montserrat_12); + lv_style_set_text_font(&style_time_label, &lv_font_montserrat_28); lv_obj_add_style(time_label, &style_time_label, 0); + // Create a text label aligned on top: https://docs.lvgl.io/master/widgets/label.html + date_label = lv_label_create(lv_screen_active()); + lv_label_set_text(date_label, "Dienstag, 12. Dezember 2024"); + lv_obj_align(date_label, LV_ALIGN_TOP_MID, 0, 40); + + // Set font type and font size. More information: https://docs.lvgl.io/master/overview/font.html + static lv_style_t style_date_label; + lv_style_init(&style_date_label); + lv_style_set_text_font(&style_date_label, &lv_font_montserrat_12); + lv_obj_add_style(date_label, &style_date_label, 0); + /* Refresh-Symbol anzeigen lv_obj_t * refresh_label = lv_label_create(lv_screen_active()); lv_label_set_text(refresh_label, LV_SYMBOL_REFRESH); @@ -181,7 +181,7 @@ void setValuetoArc(ARC arc, float value) { // Axen lv_obj_t * scale = lv_scale_create(container_row); lv_obj_set_size(scale, 15, 75); - lv_scale_set_mode(scale, LV_SCALE_MODE_VERTICAL_RIGHT); + lv_scale_set_mode(scale, LV_SCALE_MODE_VERTICAL_LEFT); lv_scale_set_label_show(scale, true); @@ -200,6 +200,15 @@ void setValuetoArc(ARC arc, float value) { lv_style_set_text_font(&style_scale, &lv_font_montserrat_10); lv_obj_add_style(scale, &style_scale, 0); + lv_obj_t * unitxAx_label = lv_label_create(container_row); + lv_label_set_text(unitxAx_label, "°C"); + lv_obj_align(unitxAx_label, LV_ALIGN_TOP_RIGHT, -20, 8); + static lv_style_t style_unitxAx_label; + lv_style_init(&style_unitxAx_label); + lv_style_set_text_font(&style_unitxAx_label, &lv_font_montserrat_12); + lv_obj_add_style(unitxAx_label, &style_unitxAx_label, 0); + + } diff --git a/src/grafik.h b/src/grafik.h index 3aa2a06..5d9e89e 100644 --- a/src/grafik.h +++ b/src/grafik.h @@ -39,11 +39,15 @@ typedef struct { #ifdef MAIN -ARC arcTemp = {NULL, "temp", 30, 0, 110, 30.0, "%.1f", "°C", 10, 30, 20, 25}; -ARC arcHum = {NULL, "humi", 180, 0, 110, 60.0, "%.0f", "%",0, 100, 65, 70}; +ARC arcTemp = {NULL, "temp", 40, 0, 110, 30.0, "%.1f", "°C", 10, 30, 20, 25}; +ARC arcHum = {NULL, "humi", 170, 0, 110, 60.0, "%.0f", "%",0, 100, 65, 70}; +lv_obj_t * date_label; +lv_obj_t * time_label; #else extern ARC arcTemp; extern ARC arcHum; +extern lv_obj_t * date_label; +extern lv_obj_t * time_label; #endif diff --git a/src/main.cpp b/src/main.cpp index 24a2663..1444fde 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,11 +28,17 @@ #include "grafik.h" #include "main.h" -long repeatTime = 10000; // alle 1sec messen -long lastTime = 0; +#define BMWREADTIME 10000 +#define NTPREADTIME 1000 + +long bmeReadtimer = 0; +long ntpReadtimer = 0; BME280Data bmedata; +int currentSecond = -1; + +void showDateTime(struct tm tinfo); void setup() { // String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch(); @@ -61,9 +67,11 @@ void setup() { connectToMqtt(); Serial.println("MQTT started"); - - lastTime = millis(); - + timeinfo = gettheTime(NTPREADTIME); + showDateTime(timeinfo); + + bmeReadtimer = millis(); + drawChart(); /* @@ -92,14 +100,27 @@ void setup() { } void loop() { - if (millis() - lastTime > repeatTime) { - lastTime = millis(); + timeinfo = gettheTime(NTPREADTIME); + if (timeinfo.tm_sec == 0) { // minute over + showDateTime(timeinfo); + printLocalTime(); + } + if (millis() - bmeReadtimer > BMWREADTIME) { + bmeReadtimer = millis(); bmedata = readBME280(); setValuetoArc(arcTemp, bmedata.temp); setValuetoArc(arcHum, bmedata.hum); - updatechartData(bmedata.hum, bmedata.temp); - } + } lv_task_handler(); // let the GUI do its work lv_tick_inc(5); // tell LVGL how much time has passed delay(5); // let this time pass +} + +void showDateTime(struct tm tinfo) { + char time[6]; + char date[50]; + strftime(time, 6, "%H:%M", &tinfo); + strftime(date, 50, "%A, %d %B %Y", &tinfo); + lv_label_set_text(time_label, time); + lv_label_set_text(date_label, date); } \ No newline at end of file diff --git a/src/weifi.cpp b/src/weifi.cpp index 3b684e0..44d3a13 100644 --- a/src/weifi.cpp +++ b/src/weifi.cpp @@ -3,6 +3,45 @@ #include "weifi.h" #include "mqtt.h" +struct tm gettheTime(int repeat) { + if(!getLocalTime(&timeinfo)){ + Serial.println("Failed to obtain time"); + } + return timeinfo; +} +void printLocalTime(){ + struct tm timeinfo; + if(!getLocalTime(&timeinfo)){ + Serial.println("Failed to obtain time"); + return; + } + Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); + Serial.print("Day of week: "); + Serial.println(&timeinfo, "%A"); + Serial.print("Month: "); + Serial.println(&timeinfo, "%B"); + Serial.print("Day of Month: "); + Serial.println(&timeinfo, "%d"); + Serial.print("Year: "); + Serial.println(&timeinfo, "%Y"); + Serial.print("Hour: "); + Serial.println(&timeinfo, "%H"); + Serial.print("Hour (12 hour format): "); + Serial.println(&timeinfo, "%I"); + Serial.print("Minute: "); + Serial.println(&timeinfo, "%M"); + Serial.print("Second: "); + Serial.println(&timeinfo, "%S"); + + Serial.println("Time variables"); + char timeHour[3]; + strftime(timeHour,3, "%H", &timeinfo); + Serial.println(timeHour); + char timeWeekDay[10]; + strftime(timeWeekDay,10, "%A", &timeinfo); + Serial.println(timeWeekDay); + Serial.println(); +} void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); @@ -13,8 +52,12 @@ void connectToWifi() { } Serial.print("Connected to the WiFi with IP: "); Serial.println(WiFi.localIP()); + + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + printLocalTime(); } + /* void WiFiEvent(WiFiEvent_t event) { Serial.printf("[WiFi-event] event: %d\n", event); diff --git a/src/weifi.h b/src/weifi.h index 66d7497..9ae7708 100644 --- a/src/weifi.h +++ b/src/weifi.h @@ -2,17 +2,33 @@ #define WEIFI_H #include +#include "time.h" + #include "main.h" +#ifdef MAIN +struct tm timeinfo; +#else +extern struct tm timeinfo; +#endif + #ifdef WEIFI const char* ssid = "Mizar"; const char* password = "RingNebelM57"; +const char *ntpServer = "pool.ntp.org"; +const long gmtOffset_sec = 3600; +const int daylightOffset_sec = 3600; + #endif // #warning "You need to set the SSID and PASSWORD in weifi.h" void connectToWifi(); +struct tm gettheTime(int repeat); +void printLocalTime(); + + // void WiFiEvent(WiFiEvent_t event);