//EEE3453 Computer Programming for Microcontroller
//End-of-Module Assessment – Mini Project Stage 2
// Intelligent environmental protection temperature and humidity sensor system
// Include necessary libraries for the project
#include <Wire.h> // Required for I2C communication
#include <LiquidCrystal_I2C.h> // For the LCD screen over I2C
#include "DHTesp.h" // For the DHT sensor
#include <DHT.h> // For the DHT sensor
#include "RTClib.h"
#include <WiFi.h> // For the Real Time Clock
// Initialize the LCD displays with their respective I2C addresses and sizes
LiquidCrystal_I2C lcd(0x27, 20, 4);
LiquidCrystal_I2C lcd2(0x3F, 20, 4);
// Initialize DHT22 sensor
DHT dht22(17, DHT22);
DHTesp dhtSensor;
// Initialize the RTC (Real Time Clock)
RTC_DS1307 rtc;
// Define pin numbers for various components
int PURPLE = 21;
int YELLOW = 20;
int CYAN = 19;
int Red_alarm = 13;
int mon = 15;
int buzzer = 5;
int relay = 37;
int GREEN1 = 9;
int ORANGE1 = 8;
int YELLOW1 = 7;
int BLUE1 = 6;
const char* ssid="Wokwi-GUEST";
const char* password="Wokwi-GUEST";
// Network Time Protocol (NTP) server information
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
// Function to display a spinner animation on the LCD
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 1);
lcd.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
// Function to print local time on the LCD
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
lcd.setCursor(0, 1);
lcd.println("Connection Err");
return;
}
lcd.setCursor(8, 0);
lcd.println(&timeinfo, "%H:%M:%S");
lcd.setCursor(0, 1);
lcd.println(&timeinfo, "%d/%m/%Y %Z");
}
// Setup function to initialize components
void setup() {
Serial.begin(115200);
Serial.println("Environmental conditions in suburban areas Construct application system-detection end: start");
dht22.begin(); // initialization DHT
dhtSensor.setup(17, DHTesp::DHT22);
Wire.begin(16, 15);
lcd.init();
lcd2.init();
lcd.backlight();
lcd2.backlight();
lcd.println("Power On Connecting to wifi");
delay(500);
// Setting pin modes for all the digital pins
pinMode(PURPLE, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(CYAN, OUTPUT);
pinMode(Red_alarm, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(GREEN1, OUTPUT);
pinMode(ORANGE1, OUTPUT);
pinMode(YELLOW1, OUTPUT);
pinMode(BLUE1, OUTPUT);
pinMode(relay, OUTPUT);
// Checking and initializing the RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// The following lines would set the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to WiFi network!");
}
// Main program loop
void loop() {
// Get current time from RTC
DateTime time = rtc.now();
// Print different time formats to the Serial Monitor
Serial.println(String("FULL:\t") + time.timestamp(DateTime::TIMESTAMP_FULL));
Serial.println(String("DATE:\t") + time.timestamp(DateTime::TIMESTAMP_DATE));
Serial.println(String("TIME:\t") + time.timestamp(DateTime::TIMESTAMP_TIME));
// Re-initialize the LCD and set its backlight
lcd.init();
lcd.backlight();
// Display the date and time on the LCD
lcd.setCursor(1, 2);
lcd.print(time.timestamp(DateTime::TIMESTAMP_DATE));
lcd.setCursor(1, 3);
lcd.print(time.timestamp(DateTime::TIMESTAMP_TIME));
// Delay for half a second before continuing
delay(500);
// Get temperature and humidity readings from the DHT sensor
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float h = dht22.readHumidity(); // Read humidity
float t = dht22.readTemperature(); // Read temperature
// Print temperature and humidity
Serial.print("Temperature: " + String(data.temperature, 2) + "'C");
Serial.print(" | ");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
delay(1000);
// Check if the temperature is greater than 40 degrees Celsius
if (data.temperature>40){
// Print a high temperature warning to the serial monitor
Serial.println("High temperature warning!");
Serial.println("Fire Alarm/Air Condition turn on");
// Set the cursor to the beginning of the first line of the LCD
lcd.setCursor(0, 0);
// Display "Hot!" and the temperature on the LCD
lcd.print("Hot! ");
lcd.print("Tem:"+String(t)+ "'C");
// Move the cursor to the second line of the LCD
lcd.setCursor(5,1);
// Display the humidity on the LCD
lcd.print("Hum:"+String(h) + "%");
// Turn on the PURPLE LED
digitalWrite(PURPLE, HIGH);
// Short delay before activating the alarm
delay(50);
// Activate the red alarm, then turn it off after a short delay
digitalWrite(Red_alarm, HIGH);
delay(1000);
digitalWrite(Red_alarm, LOW);
delay(50);
// Activate the BLUE1 LED and turn off the YELLOW and CYAN LEDs
digitalWrite(BLUE1, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(CYAN, LOW);
// Turn on a relay, possibly to activate an air conditioner
digitalWrite(relay, HIGH);
// Sound a buzzer at 2000 Hz for a short tone
tone(buzzer, 2000);
delay(100);
noTone(buzzer);
delay(20);
}
// Short delay before the next reading
delay(500);
// Check if the humidity is greater than 90%
if (data.humidity>90){
// Print a high humidity warning to the serial monitor
Serial.println("Wet! Warning air is wet");
Serial.println("Raining day, Dehumidification system on");
// Similar LCD and LED operations as above for a wet warning
lcd.setCursor(0, 0);
lcd.print("WET! ");
lcd.print("Tem:"+String(t)+ "'C");
lcd.setCursor(5,1);
lcd.print("Hum:"+String(h) + "%");
digitalWrite(PURPLE, HIGH);
delay(50);
digitalWrite(PURPLE, HIGH);
delay(10);
digitalWrite(Red_alarm, HIGH);
delay(40);
digitalWrite(Red_alarm, LOW);
delay(30);
digitalWrite(GREEN1, HIGH);
digitalWrite(CYAN, LOW);
// Turn off the relay, possibly to deactivate a humidifier
digitalWrite(relay, LOW);
// Sound a lower frequency buzzer at 1000 Hz
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(30);
}
// Short delay before the next reading
delay(500);
// Check if the temperature is less than 20 degrees Celsius and humidity is less than 30%
if (data.temperature<20 && data.humidity<30){
// Print a message indicating good conditions for outdoor activities
Serial.println("Good! Outgoing");
Serial.println("Picnic or sport day");
// Display a positive message on the LCD
lcd.setCursor(0, 0);
lcd.print("Good! ");
lcd.print("Tem:"+String(t)+ "'C");
lcd.setCursor(5,1);
lcd.print("Hum:"+String(h) + "%");
// Briefly flash the YELLOW LED
digitalWrite(YELLOW, HIGH);
delay(10);
digitalWrite(YELLOW, LOW);
// Turn off CYAN LED and activate the relay
digitalWrite(CYAN, LOW);
digitalWrite(relay, HIGH);
// Sound the buzzer at 1000 Hz for a short tone
tone(buzzer, 1000);
delay(100);
noTone(buzzer);
delay(30);
}
// Short delay before the next reading
delay(500);
// Check if the humidity is less than 20%
if (data.humidity<20){
// Print a warning about dry and potentially dangerous conditions
Serial.println("Cold!!! Warning weather is dry fire crisis danger");
Serial.println("Fire Alarm ON Air & Condition turn OFF");
// Display a dry weather warning on the LCD
lcd.setCursor(0, 0);
lcd.print("Dry! ");
lcd.print("Tem:"+String(t)+ "'C");
lcd.setCursor(5,1);
lcd.print("Hum:"+String(h) + "%");
// Activate the YELLOW LED and sound the alarm briefly
digitalWrite(YELLOW, HIGH);
delay(10);
digitalWrite(Red_alarm, HIGH);
delay(30);
digitalWrite(Red_alarm, LOW);
delay(30);
digitalWrite(YELLOW, LOW);
// Turn on the GREEN1 LED
digitalWrite(GREEN1, HIGH);
// Turn off the relay, possibly to deactivate heating elements or humidifiers
digitalWrite(relay, LOW);
// Sound a very short buzzer tone at 100 Hz
tone(buzzer, 100);
delay(50);
noTone(buzzer);
delay(10);
}
// Short delay before the next reading
delay(500);
// Check if the temperature is greater than 10 degrees Celsius
if (data.temperature>10){
// Print a message indicating cool weather but no immediate action required
Serial.println("Cool!!! Warning weather is dry Low temperature");
Serial.println("Cool. Turn off all the Machine");
// Display information about the cool temperature on the LCD
lcd.setCursor(0, 0);
lcd.print("Cool");
lcd.print("Tem:"+String(t)+ "'C");
lcd.setCursor(5,1);
lcd.print("Hum:"+String(h) + "%");
// Turn off the PURPLE LED
digitalWrite(PURPLE, LOW);
// Sound the alarm briefly and then turn it off
digitalWrite(Red_alarm, HIGH);
delay(30);
digitalWrite(Red_alarm, LOW);
delay(30);
// Turn on the ORANGE1 and CYAN LEDs
digitalWrite(ORANGE1, HIGH);
digitalWrite(CYAN, HIGH);
// Turn off the relay
digitalWrite(relay, LOW);
// Sound a short buzzer tone at 100 Hz
tone(buzzer, 100);
delay(50);
noTone(buzzer);
delay(10);
}
// Short delay before the next reading
delay(500);
// Check if the temperature is less than -10 degrees Celsius
if (data.temperature<-10){
// Print a message indicating cold but normal weather
Serial.println("Cold Today");
// Display a normal weather message on the LCD
lcd.setCursor(0, 0);
lcd.print("Normal");
lcd.print("Tem:"+String(t)+"'C");
lcd.setCursor(5,1);
lcd.print("Hum:"+String(h)+ "%");
// Ensure the PURPLE and YELLOW LEDs are off and CYAN is on
digitalWrite(PURPLE, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(CYAN, HIGH);
// Turn off the relay
digitalWrite(relay, LOW);
// Sound a long buzzer tone at 100 Hz
tone(buzzer, 100);
delay(2000);
noTone(buzzer);
delay(500);
}
// Short delay before the next reading
delay(500);
// Check if the temperature is less than -40 degrees Celsius
if (data.temperature<-40){
// Print a warning about extremely cold conditions
Serial.println("Freeze!!! Warning Snow and ice");
// Display a freezing weather warning on the LCD
lcd.setCursor(0, 0);
lcd.print("Ice!");
lcd.print("Tem:"+String(t)+"'C");
lcd.setCursor(5,1);
lcd.print("Hum:"+String(h)+ "%");
// Ensure the PURPLE and YELLOW LEDs are off and CYAN is on
digitalWrite(PURPLE, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(CYAN, HIGH);
// Turn off the relay
digitalWrite(relay, LOW);
// Sound a buzzer tone at 600 Hz for a very short duration
tone(buzzer, 600);
delay(50);
noTone(buzzer);
delay(10);
} else {
// If none of the above conditions are met, print a normal weather message
Serial.println("Good day Normal");
}
// Short delay before the next reading
delay(500);
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1