/*
---------------------------------------------------------------------------------------------------------
Project Name : Home Automation Using IOT
Components Used : Esp32, DHT22, LCD I2C, Breadboard.
---------------------------------------------------------------------------------------------------------
==========================================================================================================
Blynk IoT platform
===========================================================================================================
*/
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library for using I2C LCD
#include "DHTesp.h" // Include the DHTesp library for using DHT temperature and humidity sensor
#include <WiFi.h> // Include the WiFi library for ESP32 WiFi functionality
#include <ThingSpeak.h> // Include the ThingSpeak library for sending data to ThingSpeak IoT platform
const char *ssid = "Wokwi-GUEST"; // WiFi SSID
const char *password = ""; // WiFi password
const char *apiKey = "7TNE3OXRDMJ2UY15"; // ThingSpeak API key
const int channelNumber = 2461596; // ThingSpeak channel number
WiFiClient client; // Declare a WiFiClient object for connecting to WiFi
LiquidCrystal_I2C lcd(0x27, 20, 4); // Initialize a LiquidCrystal_I2C object with I2C address 0x27 and 20x4 dimensions
// Define custom characters for temperature symbol
byte t1[8] = {B00000, B00001, B00010, B00100, B00100, B00100, B00100, B00111};
byte t2[8] = {B00111, B00111, B00111, B01111, B11111, B11111, B01111, B00011};
byte t3[8] = {B00000, B10000, B01011, B00100, B00111, B00100, B00111, B11100};
byte t4[8] = {B11111, B11100, B11100, B11110, B11111, B11111, B11110, B11000};
// Define custom characters for humidity symbol
byte hum1[8] = {B00000, B00001, B00011, B00011, B00111, B01111, B01111, B11111};
byte hum2[8] = {B11111, B11111, B11111, B01111, B00011, B00000, B00000, B00000};
byte hum3[8] = {B00000, B10000, B11000, B11000, B11100, B11110, B11110, B11111};
byte hum4[8] = {B11111, B11111, B11111, B11110, B11100, B00000, B00000, B00000};
// Define custom characters for home symbol
byte house1[8] = {B00000, B00001, B00011, B00011, B00111, B01111, B01111, B11111};
byte house2[8] = {B11111, B11111, B11100, B11100, B11100, B11100, B11100, B11100};
byte house3[8] = {B00000, B10010, B11010, B11010, B11110, B11110, B11110, B11111};
byte house4[8] = {B11111, B11111, B11111, B10001, B10001, B10001, B11111, B11111};
// Define custom characters for lock symbol
byte Lck[] = {B01110, B10001, B10001, B11111, B11011, B11011, B11111, B00000};
// Define custom character for degree symbol
byte d[8] = {0b00011, 0b00011, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000};
// Initialize a DHTesp object for temperature and humidity sensor
DHTesp temps; // Declare an instance of the DHTesp class for temperature and humidity sensing
void connectWiFi() {
Serial.println("Connecting to WiFi..."); // Print message to Serial Monitor
WiFi.begin(ssid, password); // Connect to WiFi network using provided SSID and password
while (WiFi.status() != WL_CONNECTED) { // Wait until WiFi connection is established
delay(1000); // Wait for 1 second
Serial.println("Connecting to WiFi..."); // Print message to Serial Monitor
}
Serial.println("Connected to WiFi"); // Print message to Serial Monitor
}
void displayWelcomeMessage() {
lcd.clear(); // Clear the LCD screen
lcd.createChar(6, Lck); // Create custom character for lock symbol
lcd.createChar(1, house1); // Create custom character for house symbol
lcd.createChar(2, house2); // Create custom character for house symbol
lcd.createChar(3, house3); // Create custom character for house symbol
lcd.createChar(4, house4); // Create custom character for house symbol
lcd.setCursor(1, 2); // Set cursor position for displaying house symbol
lcd.write(1); // Display house symbol
lcd.setCursor(1, 3); // Set cursor position for displaying house symbol
lcd.write(2); // Display house symbol
lcd.setCursor(2, 2); // Set cursor position for displaying house symbol
lcd.write(3); // Display house symbol
lcd.setCursor(2, 3); // Set cursor position for displaying house symbol
lcd.write(4); // Display house symbol
lcd.setCursor(17, 2); // Set cursor position for displaying house symbol
lcd.write(1); // Display house symbol
lcd.setCursor(17, 3); // Set cursor position for displaying house symbol
lcd.write(2); // Display house symbol
lcd.setCursor(18, 2); // Set cursor position for displaying house symbol
lcd.write(3); // Display house symbol
lcd.setCursor(18, 3); // Set cursor position for displaying house symbol
lcd.write(4); // Display house symbol
lcd.setCursor(19, 0); // Set cursor position for displaying lock symbol
lcd.write(6); // Display lock symbol
lcd.setCursor(9, 0);
lcd.print("connected-"); // Print message to LCD
lcd.setCursor(2, 1);
lcd.print("HOME AUTOMATION"); // Print message to LCD
lcd.setCursor(6, 2);
lcd.print("USING IOT"); // Print message to LCD
delay(3000); // Delay for 3 seconds
}
void setup() {
Serial.begin(115200); // Initialize serial communication at baud rate 115200
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight of the LCD
displayWelcomeMessage(); // Call the function to display the welcome message on the LCD
connectWiFi(); // Call the function to connect to the WiFi network
ThingSpeak.begin(client); // Initialize ThingSpeak with the WiFi client object
temps.setup(15, DHTesp::DHT22); // Initialize the DHT sensor with pin 15 and DHT22 sensor type
delay(2000); // Delay for 2 seconds
}
void loop() {
TempAndHumidity x = temps.getTempAndHumidity(); // Get temperature and humidity readings from the DHT sensor
float tmp = x.temperature; // Store the temperature value
float hum = x.humidity; // Store the humidity value
ThingSpeak.setField(1, tmp); // Set the first field of ThingSpeak to the temperature value
ThingSpeak.setField(2, hum); // Set the second field of ThingSpeak to the humidity value
int status = ThingSpeak.writeFields(channelNumber, apiKey); // Write the data to ThingSpeak and get the status
if (status == 200) { // Check if the data was sent successfully
Serial.println("Data sent to ThingSpeak successfully"); // Print success message to serial monitor
} else {
Serial.print("Error sending data to ThingSpeak: "); // Print error message to serial monitor
Serial.println(status); // Print the status code to serial monitor
}
lcd.clear(); // Clear the LCD display
lcd.createChar(1,t1); // Create custom character for temperature symbol
lcd.createChar(2,t2); // Create custom character for temperature symbol
lcd.createChar(3,t3); // Create custom character for temperature symbol
lcd.createChar(4,t4); // Create custom character for temperature symbol
lcd.createChar(5, d); // Create custom character for degree symbol
lcd.createChar(6, Lck); // Create custom character for lock symbol
lcd.setCursor(19,0); // Set cursor position for lock symbol
lcd.write(6); // Display lock symbol
lcd.setCursor(1,1); // Set cursor position for temperature symbol
lcd.write(1); // Display temperature symbol
lcd.setCursor(1,2); // Set cursor position for temperature symbol
lcd.write(2); // Display temperature symbol
lcd.setCursor(2,1); // Set cursor position for temperature symbol
lcd.write(3); // Display temperature symbol
lcd.setCursor(2,2); // Set cursor position for temperature symbol
lcd.write(4); // Display temperature symbol
lcd.setCursor(4,1); // Set cursor position for temperature label
lcd.print("Temperature :"); // Print temperature label
lcd.setCursor(7,2); // Set cursor position for temperature value
lcd.print(tmp); // Print temperature value
lcd.setCursor(11,2); // Set cursor position for degree symbol
lcd.write(5); // Display degree symbol
lcd.setCursor(12,2); // Set cursor position for unit (Celsius)
lcd.print("C"); // Print unit (Celsius)
delay(750); // Delay for 750 milliseconds
lcd.clear(); // Clear the LCD display
lcd.createChar(1,hum1); // Create custom character for humidity symbol
lcd.createChar(2,hum2); // Create custom character for humidity symbol
lcd.createChar(3,hum3); // Create custom character for humidity symbol
lcd.createChar(4,hum4); // Create custom character for humidity symbol
lcd.setCursor(19,0); // Set cursor position for lock symbol
lcd.write(6); // Display lock symbol
lcd.setCursor(3,1); // Set cursor position for humidity symbol
lcd.write(1); // Display humidity symbol
lcd.setCursor(3,2); // Set cursor position for humidity symbol
lcd.write(2); // Display humidity symbol
lcd.setCursor(4,1); // Set cursor position for humidity symbol
lcd.write(3); // Display humidity symbol
lcd.setCursor(4,2); // Set cursor position for humidity symbol
lcd.write(4); // Display humidity symbol
lcd.setCursor(6,1); // Set cursor position for humidity label
lcd.print("Humidity :"); // Print humidity label
lcd.setCursor(7,2); // Set cursor position for humidity value
lcd.print(hum); // Print humidity value
lcd.setCursor(12,2); // Set cursor position for humidity unit (%)
lcd.print("%"); // Print humidity unit (%)
delay(750); // Delay for 750 milliseconds
}