#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
const int DHT_PIN = 23;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and size
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); // Initialize the DHT sensor
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Tes Sensor DHT22");
delay(2000); // Display the initialization message for 2 seconds
}
void loop() {
// Read temperature and humidity data
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Check if the readings are valid
if (isnan(data.temperature) || isnan(data.humidity)) {
Serial.println("Failed to read from DHT sensor!");
lcd.setCursor(0, 0);
lcd.print("Error reading data");
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second line
} else {
// Print the temperature and humidity to the Serial Monitor
Serial.println("Temp: " + String(data.temperature, 1) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
// Display temperature and its progress bar
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(data.temperature, 1);
lcd.print((char)223); // Degree symbol
lcd.print("C [");
int tempProgress = map(data.temperature, 0, 50, 0, 10); // Map temperature range (0-50°C) to a progress bar (0-10 blocks)
for (int i = 0; i < 10; i++) {
lcd.print(i < tempProgress ? (char)255 : ' '); // Full block or space
}
lcd.print("]");
// Display humidity and its progress bar
lcd.setCursor(0, 1);
lcd.print("H:");
lcd.print(data.humidity, 1);
lcd.print("% [");
int humidityProgress = map(data.humidity, 0, 100, 0, 10); // Map humidity range (0-100%) to a progress bar (0-10 blocks)
for (int i = 0; i < 10; i++) {
lcd.print(i < humidityProgress ? (char)255 : ' '); // Full block or space
}
lcd.print("]");
}
delay(2000); // Wait for 2 seconds before updating the values
}