/**void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, Muthu");
Serial.println("Let's Go!!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
PIR output connectted in ESP pin 13
DHT22 output connectted in ESP pin 14
LED(assume LED as Buzzer) output connectted in ESP pin 15
Button output connectted in ESP pin 18
**/
#define BLYNK_TEMPLATE_ID "TMPL3PP98wbLx"
#define BLYNK_TEMPLATE_NAME "Home automation Version 1"
#define BLYNK_AUTH_TOKEN "2wjduz1ULBIH1fqyZxsbEgskupZX4swl"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
// Blynk Authentication Token
char auth[] = BLYNK_AUTH_TOKEN;
// WiFi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// DHT sensor settings
#define DHTPIN 14
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// PIR sensor pin
const int pirPin = 13;
// Buzzer pin
const int buzzerPin = 15;
// LED pin
const int ledPin = 16;
const int ledPin1 = 17;
// Button pin
const int buttonPin = 18;
// Variables
int pirState = LOW;
int buttonState = LOW;
int oldValue = HIGH;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Initialize DHT sensor
dht.begin();
// Initialize pins
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
Blynk.run();
readSensors();
}
void readSensors() {
// Read PIR sensor
pirState = digitalRead(pirPin);
if (pirState == HIGH) {
Serial.println(pirState);
digitalWrite(buzzerPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(1000);
Blynk.virtualWrite(V2, 1); // Trigger virtual pin for motion notification
} else {
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
delay(1000);
Blynk.virtualWrite(V2, 0); // Reset virtual pin for motion notification
}
//Read button (door/window sensor)
buttonState = digitalRead(buttonPin);
/**
if (buttonState == LOW) {
digitalWrite(ledPin1, HIGH);
delay(1000);
Blynk.virtualWrite(V3, 1); // Trigger virtual pin for door/window notification
} else {
Blynk.virtualWrite(V3, 0); // Reset virtual pin for door/window notification
}
**/
int newValue = buttonState;
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue != oldValue)
{
if(newValue == LOW)
{
digitalWrite(ledPin1, HIGH);
//delay(2000);
Blynk.virtualWrite(V3, 1); // Trigger virtual pin for door/window notification
delay(2000);
Serial.println("The button is pressed.");
}
else
{
digitalWrite(ledPin1, LOW);
Blynk.virtualWrite(V3, 0); // Reset virtual pin for door/window notification
delay(2000);
Serial.println("The button is released.");
}
// Remember the value for the next time.
oldValue = newValue;
}
// Read DHT sensor
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Send sensor data to Blynk
Blynk.virtualWrite(V0, t); // Temperature
Blynk.virtualWrite(V1, h); // Humidity
delay(100);
}