// --- Library Includes ---
#include <LiquidCrystal.h>
#include <DHT.h>
#include <WiFi.h>
// --- DHT11 Sensor Setup ---
#define DHTPIN 4 // The pin the DHT11 data is connected to.
#define DHTTYPE DHT11 // The type of sensor (DHT11 or DHT22).
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.
// --- LCD1602 Direct Wiring Setup ---
// Initialize the library with the pins used for the LCD's interface.
// LiquidCrystal(rs, enable, d4, d5, d6, d7);
const int rs = 19, en = 23, d4 = 18, d5 = 5, d6 = 17, d7 = 16;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// --- Setup Function ---
void setup() {
Serial.begin(115200);
// Initialize the DHT sensor.
dht.begin();
// Initialize the LCD display.
// Set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connecting to...");
lcd.setCursor(0, 1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.setCursor(0, 1);
delay(3000);
}
// --- Main Loop ---
void loop() {
// Read temperature and humidity every 5 seconds.
delay(5000);
// Read humidity from the sensor.
float h = dht.readHumidity();
// Read temperature as Celsius.
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor Read Error!");
return;
}
// Clear the display for the new data.
lcd.clear();
// Display temperature on the first line.
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
// Display humidity on the second line.
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(h);
lcd.print(" %");
// Print to Serial Monitor for debugging.
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}