/** * CYD ESP32-2432S028R — LVGL 温湿度仪表盘 * DHT11 DATA -> GPIO22 */ #include #include #include #include #include #include #define DHT_PIN 22 #define TFT_BL_PIN 21 #define XPT2046_IRQ 36 #define XPT2046_MOSI 32 #define XPT2046_MISO 39 #define XPT2046_CLK 25 #define XPT2046_CS 33 #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 240 #define DHT_READ_MS 2500 #define DHT_FAIL_TOLERANCE 3 #define EMA_ALPHA 0.35f #define HISTORY_LEN 48 #define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8)) static const lv_color_t COL_BG = lv_color_hex(0x0A1018); static const lv_color_t COL_PANEL = lv_color_hex(0x121C2A); static const lv_color_t COL_EDGE = lv_color_hex(0x1E2C40); static const lv_color_t COL_TEXT = lv_color_hex(0xF2F5FA); static const lv_color_t COL_MUTED = lv_color_hex(0x7E90A8); static const lv_color_t COL_TEMP_COOL = lv_color_hex(0x4DA3FF); static const lv_color_t COL_TEMP_OK = lv_color_hex(0x3DBF8C); static const lv_color_t COL_TEMP_WARM = lv_color_hex(0xF0A04B); static const lv_color_t COL_TEMP_HOT = lv_color_hex(0xE85D4C); static const lv_color_t COL_HUM = lv_color_hex(0x3DB8A8); static const lv_color_t COL_OK = lv_color_hex(0x4CAF7A); static const lv_color_t COL_WARN = lv_color_hex(0xE6B84D); static const lv_color_t COL_BAD = lv_color_hex(0xE05A5A); static const lv_color_t COL_ARC_BG = lv_color_hex(0x243247); SPIClass touchscreenSPI = SPIClass(VSPI); XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ); DHTesp dht; static uint32_t draw_buf[DRAW_BUF_SIZE / 4]; static lv_obj_t *arc_temp; static lv_obj_t *arc_hum; static lv_obj_t *lbl_temp_val; static lv_obj_t *lbl_hum_val; static lv_obj_t *lbl_temp_range; static lv_obj_t *lbl_hum_range; static lv_obj_t *lbl_comfort; static lv_obj_t *lbl_feels; static lv_obj_t *lbl_status; static lv_obj_t *lbl_uptime; static lv_obj_t *led_status; static lv_obj_t *bar_comfort; static lv_obj_t *chart; static lv_chart_series_t *ser_temp; static lv_chart_series_t *ser_hum; static float disp_temp = NAN; static float disp_hum = NAN; static float min_temp = NAN; static float max_temp = NAN; static float min_hum = NAN; static float max_hum = NAN; static uint32_t last_dht_ms = 0; static uint32_t last_ok_ms = 0; static uint8_t fail_streak = 0; static bool sensor_ok = false; static uint8_t bright_level = 2; static const uint8_t BRIGHT_PWM[4] = {40, 90, 160, 255}; static void set_backlight(uint8_t level) { bright_level = level & 0x03; ledcWrite(0, BRIGHT_PWM[bright_level]); } static float ema(float prev, float sample) { if (isnan(prev)) return sample; return prev + EMA_ALPHA * (sample - prev); } static float dew_point_c(float t, float h) { if (h <= 0.1f) h = 0.1f; if (h > 100.0f) h = 100.0f; const float a = 17.27f; const float b = 237.7f; float alpha = (a * t) / (b + t) + logf(h / 100.0f); return (b * alpha) / (a - alpha); } static float heat_index_c(float t, float h) { if (t < 20.0f) return t - (50.0f - h) * 0.03f; float tf = t * 1.8f + 32.0f; float hi_f = -42.379f + 2.04901523f * tf + 10.14333127f * h - 0.22475541f * tf * h - 0.00683783f * tf * tf - 0.05481717f * h * h + 0.00122874f * tf * tf * h + 0.00085282f * tf * h * h - 0.00000199f * tf * tf * h * h; return (hi_f - 32.0f) / 1.8f; } static lv_color_t temp_color(float t) { if (isnan(t)) return COL_MUTED; if (t < 18.0f) return COL_TEMP_COOL; if (t < 26.0f) return COL_TEMP_OK; if (t < 30.0f) return COL_TEMP_WARM; return COL_TEMP_HOT; } static const char *comfort_text(float t, float h) { if (isnan(t) || isnan(h)) return "NO DATA"; if (t < 16 || t > 30 || h < 30 || h > 75) return "UNCOMFORTABLE"; if (t >= 20 && t <= 26 && h >= 40 && h <= 60) return "COMFORTABLE"; return "ACCEPTABLE"; } static lv_color_t comfort_color(float t, float h) { if (isnan(t) || isnan(h)) return COL_BAD; if (t < 16 || t > 30 || h < 30 || h > 75) return COL_BAD; if (t >= 20 && t <= 26 && h >= 40 && h <= 60) return COL_OK; return COL_WARN; } static int comfort_score(float t, float h) { if (isnan(t) || isnan(h)) return 0; float t_score = 100.0f - fabsf(t - 23.0f) * 8.0f; float h_score = 100.0f - fabsf(h - 50.0f) * 2.0f; int score = (int)lroundf((t_score + h_score) * 0.5f); return constrain(score, 0, 100); } static void touchscreen_read(lv_indev_t *indev, lv_indev_data_t *data) { LV_UNUSED(indev); static bool was_pressed = false; static uint32_t press_ms = 0; if (touchscreen.tirqTouched() && touchscreen.touched()) { TS_Point p = touchscreen.getPoint(); data->point.x = map(p.y, 240, 3800, 1, SCREEN_WIDTH); data->point.y = map(p.x, 200, 3700, SCREEN_HEIGHT, 1); data->state = LV_INDEV_STATE_PRESSED; if (!was_pressed) { was_pressed = true; press_ms = millis(); } } else { data->state = LV_INDEV_STATE_RELEASED; if (was_pressed) { was_pressed = false; if (millis() - press_ms < 400) { set_backlight((bright_level + 1) & 0x03); } } } } static void arc_anim_exec(void *obj, int32_t v) { lv_arc_set_value((lv_obj_t *)obj, v); } static void anim_arc(lv_obj_t *arc, int32_t value) { int32_t cur = lv_arc_get_value(arc); if (cur == value) return; lv_anim_t a; lv_anim_init(&a); lv_anim_set_var(&a, arc); lv_anim_set_values(&a, cur, value); lv_anim_set_duration(&a, 450); lv_anim_set_exec_cb(&a, arc_anim_exec); lv_anim_set_path_cb(&a, lv_anim_path_ease_out); lv_anim_start(&a); } static lv_obj_t *make_panel(lv_obj_t *parent, int x, int y, int w, int h) { lv_obj_t *p = lv_obj_create(parent); lv_obj_set_pos(p, x, y); lv_obj_set_size(p, w, h); lv_obj_set_style_bg_color(p, COL_PANEL, 0); lv_obj_set_style_bg_opa(p, LV_OPA_COVER, 0); lv_obj_set_style_border_color(p, COL_EDGE, 0); lv_obj_set_style_border_width(p, 1, 0); lv_obj_set_style_radius(p, 14, 0); lv_obj_set_style_pad_all(p, 6, 0); lv_obj_clear_flag(p, LV_OBJ_FLAG_SCROLLABLE); return p; } static lv_obj_t *make_gauge(lv_obj_t *parent, lv_color_t accent, int max_val) { lv_obj_t *arc = lv_arc_create(parent); lv_obj_set_size(arc, 112, 112); lv_obj_align(arc, LV_ALIGN_TOP_MID, 0, 14); lv_arc_set_rotation(arc, 135); lv_arc_set_bg_angles(arc, 0, 270); lv_arc_set_range(arc, 0, max_val); lv_arc_set_value(arc, 0); lv_obj_remove_style(arc, NULL, LV_PART_KNOB); lv_obj_clear_flag(arc, LV_OBJ_FLAG_CLICKABLE); lv_obj_set_style_arc_color(arc, COL_ARC_BG, LV_PART_MAIN); lv_obj_set_style_arc_width(arc, 12, LV_PART_MAIN); lv_obj_set_style_arc_color(arc, accent, LV_PART_INDICATOR); lv_obj_set_style_arc_width(arc, 12, LV_PART_INDICATOR); lv_obj_set_style_arc_rounded(arc, true, LV_PART_MAIN); lv_obj_set_style_arc_rounded(arc, true, LV_PART_INDICATOR); return arc; } static void create_ui(void) { lv_obj_t *scr = lv_screen_active(); lv_obj_set_style_bg_color(scr, COL_BG, 0); lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0); lv_obj_clear_flag(scr, LV_OBJ_FLAG_SCROLLABLE); lv_obj_t *title = lv_label_create(scr); lv_label_set_text(title, "ROOM CLIMATE"); lv_obj_set_style_text_font(title, &lv_font_montserrat_16, 0); lv_obj_set_style_text_color(title, COL_TEXT, 0); lv_obj_align(title, LV_ALIGN_TOP_LEFT, 10, 6); lbl_uptime = lv_label_create(scr); lv_label_set_text(lbl_uptime, "tap: brightness"); lv_obj_set_style_text_font(lbl_uptime, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(lbl_uptime, COL_MUTED, 0); lv_obj_align(lbl_uptime, LV_ALIGN_TOP_LEFT, 10, 24); led_status = lv_led_create(scr); lv_obj_set_size(led_status, 10, 10); lv_obj_align(led_status, LV_ALIGN_TOP_RIGHT, -10, 10); lv_led_set_color(led_status, COL_WARN); lv_led_on(led_status); lbl_status = lv_label_create(scr); lv_label_set_text(lbl_status, "INIT"); lv_obj_set_style_text_font(lbl_status, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(lbl_status, COL_MUTED, 0); lv_obj_align(lbl_status, LV_ALIGN_TOP_RIGHT, -26, 8); lv_obj_t *p_temp = make_panel(scr, 6, 40, 152, 132); lv_obj_t *t_cap = lv_label_create(p_temp); lv_label_set_text(t_cap, "TEMP"); lv_obj_set_style_text_font(t_cap, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(t_cap, COL_MUTED, 0); lv_obj_align(t_cap, LV_ALIGN_TOP_LEFT, 2, 0); arc_temp = make_gauge(p_temp, COL_TEMP_OK, 50); lbl_temp_val = lv_label_create(p_temp); lv_label_set_text(lbl_temp_val, "--.-"); lv_obj_set_style_text_font(lbl_temp_val, &lv_font_montserrat_28, 0); lv_obj_set_style_text_color(lbl_temp_val, COL_TEMP_OK, 0); lv_obj_align(lbl_temp_val, LV_ALIGN_CENTER, 0, 6); lv_obj_t *t_unit = lv_label_create(p_temp); lv_label_set_text(t_unit, "C"); lv_obj_set_style_text_font(t_unit, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(t_unit, COL_MUTED, 0); lv_obj_align(t_unit, LV_ALIGN_CENTER, 36, 10); lbl_temp_range = lv_label_create(p_temp); lv_label_set_text(lbl_temp_range, "min -- max --"); lv_obj_set_style_text_font(lbl_temp_range, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(lbl_temp_range, COL_MUTED, 0); lv_obj_align(lbl_temp_range, LV_ALIGN_BOTTOM_MID, 0, 0); lv_obj_t *p_hum = make_panel(scr, 162, 40, 152, 132); lv_obj_t *h_cap = lv_label_create(p_hum); lv_label_set_text(h_cap, "HUM"); lv_obj_set_style_text_font(h_cap, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(h_cap, COL_MUTED, 0); lv_obj_align(h_cap, LV_ALIGN_TOP_LEFT, 2, 0); arc_hum = make_gauge(p_hum, COL_HUM, 100); lbl_hum_val = lv_label_create(p_hum); lv_label_set_text(lbl_hum_val, "--.-"); lv_obj_set_style_text_font(lbl_hum_val, &lv_font_montserrat_28, 0); lv_obj_set_style_text_color(lbl_hum_val, COL_HUM, 0); lv_obj_align(lbl_hum_val, LV_ALIGN_CENTER, 0, 6); lv_obj_t *h_unit = lv_label_create(p_hum); lv_label_set_text(h_unit, "%"); lv_obj_set_style_text_font(h_unit, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(h_unit, COL_MUTED, 0); lv_obj_align(h_unit, LV_ALIGN_CENTER, 36, 10); lbl_hum_range = lv_label_create(p_hum); lv_label_set_text(lbl_hum_range, "min -- max --"); lv_obj_set_style_text_font(lbl_hum_range, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(lbl_hum_range, COL_MUTED, 0); lv_obj_align(lbl_hum_range, LV_ALIGN_BOTTOM_MID, 0, 0); lv_obj_t *p_bot = make_panel(scr, 6, 176, 308, 58); lbl_comfort = lv_label_create(p_bot); lv_label_set_text(lbl_comfort, "WAITING"); lv_obj_set_style_text_font(lbl_comfort, &lv_font_montserrat_14, 0); lv_obj_set_style_text_color(lbl_comfort, COL_WARN, 0); lv_obj_align(lbl_comfort, LV_ALIGN_TOP_LEFT, 4, 0); lbl_feels = lv_label_create(p_bot); lv_label_set_text(lbl_feels, "FEELS -- DEW --"); lv_obj_set_style_text_font(lbl_feels, &lv_font_montserrat_12, 0); lv_obj_set_style_text_color(lbl_feels, COL_MUTED, 0); lv_obj_align(lbl_feels, LV_ALIGN_TOP_RIGHT, -4, 2); bar_comfort = lv_bar_create(p_bot); lv_obj_set_size(bar_comfort, 120, 10); lv_obj_align(bar_comfort, LV_ALIGN_TOP_LEFT, 4, 20); lv_bar_set_range(bar_comfort, 0, 100); lv_bar_set_value(bar_comfort, 0, LV_ANIM_OFF); lv_obj_set_style_bg_color(bar_comfort, COL_ARC_BG, LV_PART_MAIN); lv_obj_set_style_bg_opa(bar_comfort, LV_OPA_COVER, LV_PART_MAIN); lv_obj_set_style_radius(bar_comfort, 5, LV_PART_MAIN); lv_obj_set_style_bg_color(bar_comfort, COL_WARN, LV_PART_INDICATOR); lv_obj_set_style_radius(bar_comfort, 5, LV_PART_INDICATOR); chart = lv_chart_create(p_bot); lv_obj_set_size(chart, 168, 28); lv_obj_align(chart, LV_ALIGN_BOTTOM_RIGHT, -2, -2); lv_chart_set_type(chart, LV_CHART_TYPE_LINE); lv_chart_set_point_count(chart, HISTORY_LEN); lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 10, 40); lv_chart_set_range(chart, LV_CHART_AXIS_SECONDARY_Y, 0, 100); lv_obj_set_style_bg_opa(chart, LV_OPA_TRANSP, 0); lv_obj_set_style_border_width(chart, 0, 0); lv_obj_set_style_pad_all(chart, 0, 0); lv_chart_set_div_line_count(chart, 0, 0); ser_temp = lv_chart_add_series(chart, COL_TEMP_WARM, LV_CHART_AXIS_PRIMARY_Y); ser_hum = lv_chart_add_series(chart, COL_HUM, LV_CHART_AXIS_SECONDARY_Y); } static void refresh_meta(void) { char buf[40]; uint32_t up = millis() / 1000; lv_snprintf(buf, sizeof(buf), "up %02lu:%02lu bl %u/3", (unsigned long)(up / 60), (unsigned long)(up % 60), (unsigned)bright_level); lv_label_set_text(lbl_uptime, buf); if (sensor_ok) { uint32_t age = (millis() - last_ok_ms) / 1000; lv_snprintf(buf, sizeof(buf), "LIVE %lus", (unsigned long)age); lv_label_set_text(lbl_status, buf); lv_led_set_color(led_status, COL_OK); } } static void update_ui(bool ok) { char buf[40]; if (ok && !isnan(disp_temp) && !isnan(disp_hum)) { lv_color_t tc = temp_color(disp_temp); lv_snprintf(buf, sizeof(buf), "%.1f", disp_temp); lv_label_set_text(lbl_temp_val, buf); lv_obj_set_style_text_color(lbl_temp_val, tc, 0); lv_obj_set_style_arc_color(arc_temp, tc, LV_PART_INDICATOR); anim_arc(arc_temp, (int)lroundf(constrain(disp_temp, 0.0f, 50.0f))); lv_snprintf(buf, sizeof(buf), "%.0f", disp_hum); lv_label_set_text(lbl_hum_val, buf); anim_arc(arc_hum, (int)lroundf(constrain(disp_hum, 0.0f, 100.0f))); lv_snprintf(buf, sizeof(buf), "min %.0f max %.0f", min_temp, max_temp); lv_label_set_text(lbl_temp_range, buf); lv_snprintf(buf, sizeof(buf), "min %.0f max %.0f", min_hum, max_hum); lv_label_set_text(lbl_hum_range, buf); float feels = heat_index_c(disp_temp, disp_hum); float dew = dew_point_c(disp_temp, disp_hum); lv_snprintf(buf, sizeof(buf), "FEELS %.1f DEW %.1f", feels, dew); lv_label_set_text(lbl_feels, buf); lv_color_t cc = comfort_color(disp_temp, disp_hum); lv_label_set_text(lbl_comfort, comfort_text(disp_temp, disp_hum)); lv_obj_set_style_text_color(lbl_comfort, cc, 0); lv_bar_set_value(bar_comfort, comfort_score(disp_temp, disp_hum), LV_ANIM_ON); lv_obj_set_style_bg_color(bar_comfort, cc, LV_PART_INDICATOR); lv_chart_set_next_value(chart, ser_temp, (int32_t)lroundf(disp_temp)); lv_chart_set_next_value(chart, ser_hum, (int32_t)lroundf(disp_hum)); refresh_meta(); } else { lv_label_set_text(lbl_comfort, "SENSOR ERROR"); lv_obj_set_style_text_color(lbl_comfort, COL_BAD, 0); lv_label_set_text(lbl_status, "ERR"); lv_led_set_color(led_status, COL_BAD); } } static void read_dht(void) { TempAndHumidity data = dht.getTempAndHumidity(); float t = data.temperature; float h = data.humidity; bool ok = !(isnan(h) || isnan(t) || dht.getStatus() != DHTesp::ERROR_NONE); if (!ok) { fail_streak++; Serial.printf("DHT fail(%u): %s\n", fail_streak, dht.getStatusString()); if (fail_streak >= DHT_FAIL_TOLERANCE) { sensor_ok = false; update_ui(false); } return; } fail_streak = 0; sensor_ok = true; last_ok_ms = millis(); disp_temp = ema(disp_temp, t); disp_hum = ema(disp_hum, h); if (isnan(min_temp) || disp_temp < min_temp) min_temp = disp_temp; if (isnan(max_temp) || disp_temp > max_temp) max_temp = disp_temp; if (isnan(min_hum) || disp_hum < min_hum) min_hum = disp_hum; if (isnan(max_hum) || disp_hum > max_hum) max_hum = disp_hum; Serial.printf("T=%.1f(%.1f) C H=%.0f(%.0f) %%\n", disp_temp, t, disp_hum, h); update_ui(true); } void setup() { Serial.begin(115200); delay(200); ledcSetup(0, 5000, 8); ledcAttachPin(TFT_BL_PIN, 0); set_backlight(2); Serial.println("CYD LVGL Climate Dashboard"); Serial.printf("LVGL %d.%d.%d DHT=GPIO%d\n", lv_version_major(), lv_version_minor(), lv_version_patch(), DHT_PIN); pinMode(DHT_PIN, INPUT_PULLUP); dht.setup(DHT_PIN, DHTesp::DHT11); lv_init(); touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); touchscreen.begin(touchscreenSPI); touchscreen.setRotation(2); lv_display_t *disp = lv_tft_espi_create(240, 320, draw_buf, sizeof(draw_buf)); lv_display_set_rotation(disp, LV_DISPLAY_ROTATION_270); lv_indev_t *indev = lv_indev_create(); lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER); lv_indev_set_read_cb(indev, touchscreen_read); create_ui(); delay(1500); last_dht_ms = 0; } void loop() { uint32_t now = millis(); if (now - last_dht_ms >= DHT_READ_MS) { last_dht_ms = now; read_dht(); } static uint32_t last_meta_ms = 0; if (now - last_meta_ms >= 1000) { last_meta_ms = now; refresh_meta(); } lv_timer_handler(); lv_tick_inc(5); delay(5); }