#include "SD_Card.h" #include "Display_ST7789.h" #include "LVGL_Driver.h" #include "LVGL_Example.h" #include #include #include // -------- WIFI -------- const char* ssid = "iPhoneBENI"; const char* password = "FabAcademy26"; const char* serverURL = "http://172.20.10.3/data"; // -------- LVGL OBJECTS -------- lv_obj_t *titleLabel; lv_obj_t *tempLabel; lv_obj_t *humLabel; lv_obj_t *statusLabel; unsigned long lastUpdate = 0; // -------- UI -------- void createUI() { lv_obj_clean(lv_scr_act()); // 🔥 limpia pantalla (quita demo) lv_obj_set_style_bg_color(lv_scr_act(), lv_color_white(), 0); titleLabel = lv_label_create(lv_scr_act()); lv_label_set_text(titleLabel, "Sensor data"); lv_obj_set_style_text_font(titleLabel, &lv_font_montserrat_16, 0); lv_obj_align(titleLabel, LV_ALIGN_TOP_MID, 0, 20); tempLabel = lv_label_create(lv_scr_act()); lv_label_set_text(tempLabel, "Temp: --.- C"); lv_obj_set_style_text_font(tempLabel, &lv_font_montserrat_16, 0); lv_obj_align(tempLabel, LV_ALIGN_CENTER, 0, -20); humLabel = lv_label_create(lv_scr_act()); lv_label_set_text(humLabel, "Hum: --.- %"); lv_obj_set_style_text_font(humLabel, &lv_font_montserrat_16, 0); lv_obj_align(humLabel, LV_ALIGN_CENTER, 0, 20); statusLabel = lv_label_create(lv_scr_act()); lv_label_set_text(statusLabel, "Connecting..."); lv_obj_set_style_text_font(statusLabel, &lv_font_montserrat_12, 0); lv_obj_align(statusLabel, LV_ALIGN_BOTTOM_MID, 0, -15); } // -------- WIFI -------- void connectWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to Wi-Fi"); lv_label_set_text(statusLabel, "Connecting WiFi..."); int attempts = 0; while (WiFi.status() != WL_CONNECTED && attempts < 40) { delay(500); Serial.print("."); attempts++; Timer_Loop(); // mantiene LVGL activo } Serial.println(); if (WiFi.status() == WL_CONNECTED) { Serial.println("Wi-Fi connected"); Serial.print("IP: "); Serial.println(WiFi.localIP()); lv_label_set_text(statusLabel, "WiFi OK"); } else { Serial.println("Wi-Fi failed"); lv_label_set_text(statusLabel, "WiFi FAIL"); } } // -------- HTTP + JSON -------- void updateData() { if (WiFi.status() != WL_CONNECTED) { lv_label_set_text(statusLabel, "No WiFi"); return; } HTTPClient http; http.begin(serverURL); int httpCode = http.GET(); if (httpCode == 200) { String payload = http.getString(); Serial.print("JSON: "); Serial.println(payload); StaticJsonDocument<200> doc; DeserializationError error = deserializeJson(doc, payload); if (!error) { float temp = doc["temperature"]; float hum = doc["humidity"]; String tempText = "Temp: " + String(temp,1) + " C"; String humText = "Hum: " + String(hum,1) + " %"; lv_label_set_text(tempLabel, tempText.c_str()); lv_label_set_text(humLabel, humText.c_str()); lv_label_set_text(statusLabel, "Updated"); Serial.println(tempText); Serial.println(humText); Serial.println("------------------"); } else { Serial.println("JSON error"); lv_label_set_text(statusLabel, "JSON error"); } } else { Serial.print("HTTP error: "); Serial.println(httpCode); lv_label_set_text(statusLabel, "HTTP error"); } http.end(); } // -------- SETUP -------- void setup() { Serial.begin(115200); delay(2000); Serial.println("Booting..."); Flash_test(); LCD_Init(); Lvgl_Init(); SD_Init(); createUI(); connectWiFi(); } // -------- LOOP -------- void loop() { Timer_Loop(); if (millis() - lastUpdate > 2000) { updateData(); lastUpdate = millis(); } delay(5); }