// Blynk app token
#define BLYNK_TEMPLATE_ID "TMPL6NQnH9LoN"
#define BLYNK_TEMPLATE_NAME "Home Automation"
#define BLYNK_AUTH_TOKEN "ITX_TEp4iwrOZnWuB5oqgrBIkYY1YB7e"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include "DHTesp.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
#define DHTPIN 13
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define FLAME_SENSOR 4
#define GAS_SENSOR 27
#define WATER_LEVEL 14
int motion_sensor = 15; // choose the input pin for PIR sensor
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int pump_pin = 2;
int buzzer_pin = 12;
DHTesp dht(DHTPIN, DHTTYPE);
//Change the virtual pins according the rooms
#define button1_vpin V1
#define button2_vpin V2
#define button3_vpin V3
#define button4_vpin V4
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
Blynk.syncVirtual(button3_vpin);
Blynk.syncVirtual(button4_vpin);
}
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard
BLYNK_WRITE(button1_vpin) {
relay1_state = param.asInt();
digitalWrite(relay1_pin, relay1_state);
}
BLYNK_WRITE(button2_vpin) {
relay2_state = param.asInt();
digitalWrite(relay2_pin, relay2_state);
}
BLYNK_WRITE(button3_vpin) {
relay3_state = param.asInt();
digitalWrite(relay3_pin, relay3_state);
}
BLYNK_WRITE(button4_vpin) {
relay4_state = param.asInt();
digitalWrite(relay4_pin, relay4_state);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(motion_sensor, INPUT);
pinMode(GAS_SENSOR, INPUT);
pinMode(DHTPIN, INPUT);
pinMode(FLAME_SENSOR, INPUT);
pinMode(WATER_LEVEL, INPUT);
pinMode(pump_pin, OUTPUT);
pinMode(buzzer_pin, OUTPUT);
dht.begin();
Blynk.begin(auth, ssid, pass);
//Blynk.virtualWrite(button1_vpin, relay1_state);
//Blynk.virtualWrite(button2_vpin, relay2_state);
//Blynk.virtualWrite(button3_vpin, relay3_state);
//Blynk.virtualWrite(button4_vpin, relay4_state);
}
void loop()
{
temperatureSensor();
motionSensor();
flameSensor();
gasSensor();
waterLevelSensor();
// blynk run
Blynk.run();
timer.run();
}
void temperatureSensor(){
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
else if (temperature > 60){
digitalWrite(heatDetectAlert, HIGH);
digitalWrite(pump_pin, HIGH);
digitalWrite(buzzer_pin, HIGH);
}
else{
digitalWrite(heatDetectAlert, LOW);
digitalWrite(pump_pin, LOW);
digitalWrite(buzzer_pin, LOW);
}
// Wait a few seconds between measurements.
delay(200);
}
void motionSensor(){
val = digitalRead(motion_sensor); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
digitalWrite(motionDetectAlert, HIGH);
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
}
else {
// send message motion end
if (pirState == HIGH) {
// we have just turned of
digitalWrite(motionDetectAlert, LOW);
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void flameSensor(){
if (digitalRead(FLAME_SENSOR) == LOW) {
digitalWrite(flameDetectAlert, HIGH);
digitalWrite(pump_pin, HIGH);
digitalWrite(buzzer_pin, HIGH);
}
else {
digitalWrite(flameDetectAlert, LOW);
digitalWrite(pump_pin, LOW);
digitalWrite(buzzer_pin, LOW);
}
delay(100);
}
void gasSensor() {
unsigned int sensorValue = analogRead(GAS_SENSOR); // Read the analog value from sensor
unsigned int outputValue = map(sensorValue, 0, 1023, 0, 255); // map the 10-bit data to 8-bit data
if (outputValue > 65){
digitalWrite(gasDetectAlert, HIGH);
digitalWrite(pump_pin, HIGH);
digitalWrite(buzzer_pin, HIGH);
}
else{
digitalWrite(gasDetectAlert, LOW);
digitalWrite(pump_pin, LOW);
digitalWrite(buzzer_pin, LOW);
}
}
void waterLevelSensor(){
unsigned int sensorValue = analogRead(WATER_LEVEL);
if (sensorValue < 540){
return;
}
uint8_t outputValue = map(sensorValue, 540, 800, 0, 255);
if(outputValue > 245){
// send a message warning!
digitalWrite(waterOverDetectAlert, HIGH);
}
else{
digitalWrite(waterOverDetectAlert, LOW);
}
}