//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 03_ESP32_TFT_LCD_Touchscreen_ILI9341_DHT11_LVGL
//---------------------------------------- Including Libraries.
#include <lvgl.h>
#include <TFT_eSPI.h>
#include "DHT.h"
//----------------------------------------
// Defines the width and height of the screen.
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320
// Digital pin connected to the DHT sensor and the type of DHT sensor (in this project I use the DHT11 sensor).
#define DHTPIN 14
#define DHTTYPE DHT11
// Define variables for the status of the DHT11 sensor reading.
#define STA_SUCCESS 1
#define STA_FAIL 0
// Creating a Drawing Buffer.
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
// Global variables for objects and widgets.
static lv_obj_t * arc_Temp;
static lv_obj_t * arc_Humd;
static lv_span_t * text_Temp_Value, * text_Humd_Value, * text_sta_Sensor_Reading;
// Variables for temperature and humidity values.
float temp_Val;
int humd_Val;
byte sta_Sensor_Reading;
// Declaring the "DHT" object as "DHT_11" and its settings.
DHT DHT_11(DHTPIN, DHTTYPE);
//________________________________________________________________________________ log_print()
// If logging is enabled, it will inform the user about what is happening in the library.
void log_print(lv_log_level_t level, const char * buf) {
LV_UNUSED(level);
Serial.println(buf);
Serial.flush();
}
//________________________________________________________________________________
//________________________________________________________________________________ get_DHT11_Sensor_Data()
void get_DHT11_Sensor_Data() {
// Read temperature as Celsius (the default).
temp_Val = DHT_11.readTemperature();
// Read humidity as Percent.
humd_Val = DHT_11.readHumidity();
// Check if any reads failed.
if (isnan(temp_Val) || isnan(humd_Val)) {
Serial.println(F("Failed to read from DHT11 sensor!"));
sta_Sensor_Reading = STA_FAIL;
temp_Val = 0.0;
humd_Val = 0;
} else {
sta_Sensor_Reading = STA_SUCCESS;
}
if (sta_Sensor_Reading == STA_SUCCESS) {
Serial.print(F("Sensor Reading Status : SUCCESS | "));
} else if (sta_Sensor_Reading == STA_FAIL) {
Serial.print(F("Sensor Reading Status : FAIL | "));
}
Serial.print(F("Temperature : "));
Serial.print(temp_Val);
Serial.print(F("°C | "));
Serial.print(F("Humidity : "));
Serial.print(humd_Val);
Serial.println(F("%"));
}
//________________________________________________________________________________
//________________________________________________________________________________ my_Timer()
// Timer Callback Function.
// In this function, the temperature and humidity values are updated from the DHT11 sensor,
// then displayed on the TFT LCD.
// The update interval is every 5 seconds.
// To set the interval, please go to the "void lv_create_main_gui".
static void my_Timer(lv_timer_t * timer) {
LV_UNUSED(timer);
// Call the get_DHT11_Sensor_Data subroutine to get the temperature and humidity values.
get_DHT11_Sensor_Data();
// Condition that is executed if the DHT11 sensor reading is successful.
if (sta_Sensor_Reading == STA_SUCCESS) {
//---------------------------------------- Updates the arc progress bar for temperature and updates the displayed temperature value.
// Based on the DHT11 sensor specifications, the measurement range for temperature is approximately 0°C - 50°C.
if (temp_Val > 50) temp_Val = 50;
if (temp_Val < 0) temp_Val = 0;
// If the temperature is above 35.00 degrees Celsius, the color of the "arc progress bar" and the temperature value text changes to RED.
if (temp_Val >= 35.00) {
lv_obj_set_style_arc_color(arc_Temp, lv_color_make(255, 0, 0), LV_PART_INDICATOR);
lv_obj_set_style_bg_color(arc_Temp, lv_color_make(214, 2, 2), LV_PART_KNOB);
lv_style_set_text_color(&text_Temp_Value->style, lv_color_make(255, 0, 0));
} else if (temp_Val < 35.00) {
lv_obj_set_style_arc_color(arc_Temp, lv_color_make(255, 117, 24), LV_PART_INDICATOR);
lv_obj_set_style_bg_color(arc_Temp, lv_color_make(255, 95, 21), LV_PART_KNOB);
lv_style_set_text_color(&text_Temp_Value->style, lv_color_make(255, 117, 24));
}
// Set the "arc progress bar" value to the latest temperature value.
lv_arc_set_value(arc_Temp, int(temp_Val));
// Update the temperature value displayed on the TFT LCD.
const char degree_celsius_symbol[] = "\u00B0C";
String str_temp_Val = "\n" + String(temp_Val) + degree_celsius_symbol;
lv_span_set_text(text_Temp_Value, str_temp_Val.c_str());
//----------------------------------------
//---------------------------------------- Updates the arc progress bar for humidity and updates the displayed humidity value.
// Based on the DHT11 sensor specifications, the humidity measurement range is approximately 20% - 90% (I rounded the range to 0% - 100%).
if (humd_Val > 100) humd_Val = 100;
if (humd_Val < 0) humd_Val = 0;
// Set the "arc progress bar" value to the latest humidity value.
lv_arc_set_value(arc_Humd, humd_Val);
// Update the humidity value displayed on the TFT LCD.
String str_humd_Val = "\n" + String(humd_Val) + "%";
lv_span_set_text(text_Humd_Value, str_humd_Val.c_str());
//----------------------------------------
//----------------------------------------
lv_span_set_text(text_sta_Sensor_Reading, "SUCCESS");
lv_style_set_text_color(&text_sta_Sensor_Reading->style, lv_color_make(34, 139, 34));
//----------------------------------------
}
// Condition that is executed if DHT11 sensor reading fails.
else if (sta_Sensor_Reading == STA_FAIL) {
//----------------------------------------
lv_arc_set_value(arc_Temp, 0);
lv_span_set_text(text_Temp_Value, "\nNN.NN°C");
//----------------------------------------
//----------------------------------------
lv_arc_set_value(arc_Humd, 0);
lv_span_set_text(text_Humd_Value, "\nNN%");
//----------------------------------------
//----------------------------------------
lv_span_set_text(text_sta_Sensor_Reading, "FAIL");
lv_style_set_text_color(&text_sta_Sensor_Reading->style, lv_color_make(255, 0, 0));
//----------------------------------------
}
}
//________________________________________________________________________________
//________________________________________________________________________________ lv_create_main_gui()
void lv_create_main_gui(void) {
//---------------------------------------- Set the screen background color.
// Without this line of code, the default screen background color is white.
lv_obj_set_style_bg_color(lv_scr_act(), LV_COLOR_MAKE(255,255,255), LV_PART_MAIN);
//----------------------------------------
//---------------------------------------- Create text with a "span" aligned center on top ("ESP32 + TFT LCD Touchscreen ILI9341 + DHT11 + LVGL").
lv_obj_t * spans = lv_spangroup_create(lv_screen_active());
lv_obj_set_style_text_align(spans, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_width(spans, 300);
lv_spangroup_set_mode(spans, LV_SPAN_MODE_BREAK);
lv_obj_align(spans, LV_ALIGN_TOP_MID, 0, 15); //--> lv_obj_align(object, Alignment Type, x, y);
//--> For more details about alignment, see here : https://docs.lvgl.io/master/widgets/obj.html#alignment
//--> List of alignments, see here : https://docs.lvgl.io/8.3/overview/coords.html#align
lv_span_t * span;
span = lv_spangroup_new_span(spans);
lv_span_set_text(span, "ESP32 + TFT LCD Touchscreen ILI9341 + DHT11 + LVGL");
lv_style_set_text_color(&span->style, LV_COLOR_MAKE(165, 42, 42));
//----------------------------------------
//---------------------------------------- Create an Arc for Temperature.
arc_Temp = lv_arc_create(lv_screen_active());
// Based on the DHT11 sensor specifications, the measurement range for temperature is approximately 0°C - 50°C.
// So the arc_Temp range value is set from 0 to 50.
lv_arc_set_range(arc_Temp, 0, 50);
lv_obj_set_size(arc_Temp, 125, 125);
lv_arc_set_rotation(arc_Temp, 135);
lv_arc_set_bg_angles(arc_Temp, 0, 270);
lv_obj_set_style_arc_color(arc_Temp, lv_color_make(199, 199, 199), 0);
lv_obj_set_style_arc_color(arc_Temp, lv_color_make(255, 117, 24), LV_PART_INDICATOR);
lv_obj_set_style_bg_color(arc_Temp, lv_color_make(255, 95, 21), LV_PART_KNOB);
lv_obj_align(arc_Temp, LV_ALIGN_TOP_LEFT, 20, 70);
lv_obj_clear_flag(arc_Temp, LV_OBJ_FLAG_CLICKABLE);
//----------------------------------------
//---------------------------------------- Create text with span to display temperature value inside "arc progress bar" for temperature.
lv_obj_t * spans_Temp = lv_spangroup_create(lv_screen_active());
lv_obj_set_style_text_align(spans_Temp, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_width(spans_Temp, 100);
lv_spangroup_set_mode(spans_Temp, LV_SPAN_MODE_BREAK);
lv_obj_align_to(spans_Temp, arc_Temp, LV_ALIGN_CENTER, 0, -25);
lv_span_t * text_Temp_Title;
text_Temp_Title = lv_spangroup_new_span(spans_Temp);
lv_span_set_text(text_Temp_Title, "Temperature");
lv_style_set_text_color(&text_Temp_Title->style, lv_color_make(255, 117, 24));
lv_style_set_text_font(&text_Temp_Title->style, &lv_font_montserrat_12);
text_Temp_Value = lv_spangroup_new_span(spans_Temp);
lv_span_set_text(text_Temp_Value, "\n--.--");
lv_style_set_text_color(&text_Temp_Value->style, lv_color_make(255, 117, 24));
lv_style_set_text_font(&text_Temp_Value->style, &lv_font_montserrat_18);
//----------------------------------------
//---------------------------------------- Create an Arc for Humidity.
arc_Humd = lv_arc_create(lv_screen_active());
// Based on the DHT11 sensor specifications, the measurement range for humidity is approximately 20% - 90%.
// So the arc_Humd range value is set from 0 to 100.
lv_arc_set_range(arc_Humd, 0, 100);
lv_obj_set_size(arc_Humd, 125, 125);
lv_arc_set_rotation(arc_Humd, 135);
lv_arc_set_bg_angles(arc_Humd, 0, 270);
lv_obj_set_style_arc_color(arc_Humd, lv_color_make(199, 199, 199), 0);
lv_obj_set_style_arc_color(arc_Humd, lv_color_make(0, 150, 255), LV_PART_INDICATOR);
lv_obj_set_style_bg_color(arc_Humd, lv_color_make(0, 138, 235), LV_PART_KNOB);
lv_obj_align(arc_Humd, LV_ALIGN_TOP_RIGHT, -20, 70);
lv_obj_clear_flag(arc_Humd, LV_OBJ_FLAG_CLICKABLE);
//----------------------------------------
//---------------------------------------- Create text with span to display humidity value inside "arc progress bar" for humidity.
lv_obj_t * spans_Humd = lv_spangroup_create(lv_screen_active());
lv_obj_set_style_text_align(spans_Humd, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_width(spans_Humd, 100);
lv_spangroup_set_mode(spans_Humd, LV_SPAN_MODE_BREAK);
lv_obj_align_to(spans_Humd, arc_Humd, LV_ALIGN_CENTER, 0, -25);
lv_span_t * text_Humd_Title;
text_Humd_Title = lv_spangroup_new_span(spans_Humd);
lv_span_set_text(text_Humd_Title, "Humidity");
lv_style_set_text_color(&text_Humd_Title->style, lv_color_make(0, 150, 255));
lv_style_set_text_font(&text_Humd_Title->style, &lv_font_montserrat_12);
text_Humd_Value = lv_spangroup_new_span(spans_Humd);
lv_span_set_text(text_Humd_Value, "\n--");
lv_style_set_text_color(&text_Humd_Value->style, lv_color_make(0, 150, 255));
lv_style_set_text_font(&text_Humd_Value->style, &lv_font_montserrat_18);
//----------------------------------------
//---------------------------------------- Create text with span to display DHT11 sensor reading status.
lv_obj_t * spans_sta_Sensor_Reading = lv_spangroup_create(lv_screen_active());
lv_obj_set_style_text_align(spans_sta_Sensor_Reading, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_width(spans_sta_Sensor_Reading, 300);
lv_spangroup_set_mode(spans_sta_Sensor_Reading, LV_SPAN_MODE_BREAK);
lv_obj_align(spans_sta_Sensor_Reading, LV_ALIGN_TOP_MID, 0, 205);
lv_span_t * text_sta_Des;
text_sta_Des = lv_spangroup_new_span(spans_sta_Sensor_Reading);
lv_span_set_text(text_sta_Des, "Sensor Reading Status : ");
lv_style_set_text_color(&text_sta_Des->style, lv_color_make(36, 36, 36));
lv_style_set_text_font(&text_sta_Des->style, &lv_font_montserrat_16);
text_sta_Sensor_Reading = lv_spangroup_new_span(spans_sta_Sensor_Reading);
lv_span_set_text(text_sta_Sensor_Reading, "Wait");
lv_style_set_text_color(&text_sta_Sensor_Reading->style, lv_color_make(36, 36, 36));
lv_style_set_text_font(&text_sta_Sensor_Reading->style, &lv_font_montserrat_16);
//----------------------------------------
//---------------------------------------- Create a timer.
// The timer is used to update the temperature and humidity values, which are then displayed on the TFT LCD.
lv_timer_t * timer = lv_timer_create(my_Timer, 5000, NULL);
lv_timer_ready(timer);
//----------------------------------------
}
//________________________________________________________________________________
//________________________________________________________________________________ VOID SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(2000);
Serial.println("ESP32 + TFT LCD Touchscreen ILI9341 320x240 + DHT11 + LVGL");
Serial.println();
String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
Serial.println(LVGL_Arduino);
Serial.println();
// Initialize the DHT sensor.
DHT_11.begin();
// Start LVGL.
lv_init();
// Register print function for debugging.
lv_log_register_print_cb(log_print);
// Create a display object.
lv_display_t * disp;
// Initialize the TFT display using the TFT_eSPI library.
disp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));
lv_display_set_rotation(disp, LV_DISPLAY_ROTATION_90);
// Function to draw the GUI (text, buttons and sliders).
lv_create_main_gui();
}
//________________________________________________________________________________
//________________________________________________________________________________ VOID LOOP()
void loop() {
// put your main code here, to run repeatedly:
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.
}
//________________________________________________________________________________
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Loading
ili9341-cap-touch
ili9341-cap-touch