Building a Smart Home Control Panel with LVGL

Project Overview

In this project, we will develop a comprehensvie smart home control panel using LVGL. The interface will include multiple sections such as environmental monitoring, device control, scenario management, and system status display.

UI Layout Structure

The main interface is divided into four sections:

  • Header: Logo and user information
  • Sidebar: Navigation menu
  • Main Content Area: Displaying current page content
  • Status Bar: System status and time display

Application State Structure:

typedef struct {
    float temp;
    float humidity;
    float air_quality;
    
    struct {
        bool living_light;
        bool bed_light;
        bool kitchen_light;
        int brightness;
        bool curtains;
        int ac_temp;
        bool security;
    } devices;
    
    const char *current_mode;
    float power_usage;
    float energy_cost;
} home_ui_state_t;

static home_ui_state_t ui_state = {
    .temp = 24.5f,
    .humidity = 65.0f,
    .air_quality = 85.0f,
    .devices = {
        .living_light = true,
        .bed_light = false,
        .kitchen_light = true,
        .brightness = 75,
        .curtains = true,
        .ac_temp = 24,
        .security = false
    },
    .current_mode = "Home Mode",
    .power_usage = 2.3f,
    .energy_cost = 1.2f
};

Environment Monitoring Page

The environment monitoring page displays real-time sensor data:

void create_env_monitor_page() {
    lv_obj_clean(ui.main_content);
    
    lv_obj_set_flex_flow(ui.main_content, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_pad_all(ui.main_content, 20, 0);
    
    lv_obj_t *page_header = lv_label_create(ui.main_content);
    lv_label_set_text(page_header, "Environmental Data");
    lv_obj_set_style_text_font(page_header, &lv_font_montserrat_24, 0);
    
    // Create sensor data cards
    create_sensor_card(ui.main_content, LV_SYMBOL_THERMOMETER, "Temperature", "24.5°C", "Normal");
    create_sensor_card(ui.main_content, LV_SYMBOL_DROPLET, "Humidity", "65%", "Good");
    create_sensor_card(ui.main_content, LV_SYMBOL_LEAF, "Air Quality", "85", "Good");
    
    // Create chart section
    create_env_charts(ui.main_content);
}

Device Control Page

The device control page allows users to manage home devices:

void create_device_control_page() {
    lv_obj_clean(ui.main_content);
    
    lv_obj_set_flex_flow(ui.main_content, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_pad_all(ui.main_content, 20, 0);
    
    // Create device control sections
    create_device_section("Lighting Control", create_lighting_controls);
    create_device_section("Climate Control", create_climate_controls);
    create_device_section("Security System", create_security_controls);
}

Scenario Management

Scenario management allows users to switch between different home modes:

void create_scenario_page() {
    lv_obj_clean(ui.main_content);
    
    lv_obj_set_flex_flow(ui.main_content, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_pad_all(ui.main_content, 20, 0);
    
    // Create scenario selection cards
    struct {
        const char *name;
        const char *icon;
        const char *desc;
        lv_color_t color;
    } scenarios[] = {
        {"Away Mode", LV_SYMBOL_DOOR, "Energy saving mode", lv_color_hex(0xE74C3C)},
        {"Home Mode", LV_SYMBOL_HOME, "Comfort mode", lv_color_hex(0x3498DB)},
        {"Sleep Mode", LV_SYMBOL_BED, "Quiet mode", lv_color_hex(0x2C3E50)},
        {"Entertainment Mode", LV_SYMBOL_VIDEO, "Optimal entertainment mode", lv_color_hex(0x9B59B6)}
    };
    
    for(int i = 0; i < sizeof(scenarios)/sizeof(scenarios[0]); i++) {
        create_scenario_card(ui.main_content, &scenarios[i]);
    }
}

State Management and Data Persistence

State management ensures data consistency across the application:

void init_state_manager() {
    // Initialize state change tracking
    lv_obj_add_event_cb(ui.sidebar, state_change_handler, LV_EVENT_VALUE_CHANGED, NULL);
    
    // Set up data refresh interval
    lv_timer_create(update_display_data, 5000, NULL);
}

void update_display_data(lv_timer_t *timer) {
    // Update sensor data
    ui_state.temp += (rand() % 5 - 2) * 0.1f;
    ui_state.humidity += (rand() % 5 - 2) * 0.1f;
    ui_state.air_quality += (rand() % 10 - 5) * 0.1f;
    
    // Update device status visualization
    refresh_device_status();
    
    // Log updates
    printf("UI State Updated - Temperature: %.1f°C, Humidity: %.1f%%\n", 
           ui_state.temp, ui_state.humidity);
}

Application Initialization

Application setup process:

void app_setup() {
    // Initialize LVGL
    lv_init();
    
    // Set up display and input devices
    setup_display();
    setup_input_devices();
    
    // Initialize UI state
    init_state_manager();
    
    // Create main interface
    create_main_interface();
    
    // Start data refresh
    init_state_manager();
    
    // Set up automatic configuration saving
    lv_timer_create(save_config_to_flash, 30000, NULL);
}

int main() {
    // Initialize system
    app_setup();
    
    // Main application loop
    while(1) {
        lv_timer_handler();
        SDL_Delay(5);
    }
}

Key Features:

  • Responsive grid and flex layouts
  • Interactive device controls
  • Real-time data visualization
  • Scenario-based mode switching
  • Data persistence and configuration management

Tags: LVGL embedded GUI smart home UI Design State Management

Posted on Mon, 22 Jun 2026 17:43:04 +0000 by matstuff