#define BLYNK_TEMPLATE_ID "TMPL3AVby_oHX"
#define BLYNK_TEMPLATE_NAME "test"
#define BLYNK_AUTH_TOKEN "aefsohISrabr-5j1DJbLdRESj0LKc5hO"
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>
char ssid[] = "Wokwi-GUEST";//network configuration-using WiFi to connect to Blynk
char pass[] = "";
#include <ESP32Time.h>//time library to keep track of latest motion trigger
ESP32Time rtc(3600);
int sensorPin=0;//pin configurations
int buzzerPin=2;
int buttonPin=4;
int volVar; //variable that decides the mode(sound of the buzzer)
long lastTrip=0;//variable to store the latest occurence of motion trip
long buttonTrip;//variable to store the latest button push
int bufferTime=10;//time from the latest motion within
int bufferDelay=4;
//pushing the button will result in automatic switching to a lower pitch
//set to 10 seconds for demonstration purposes
void ringDoor(int volVar) //function for the different sound to be generated in different modes
{
if (volVar==1)
{
tone(buzzerPin,100);
delay(100);
noTone(buzzerPin);
}
else if (volVar==2)
{
tone(buzzerPin,200);
delay(100);
noTone(buzzerPin);
}
else if (volVar==3)
{
tone(buzzerPin,300);
delay(100);
noTone(buzzerPin);
}
}
BLYNK_WRITE(V0) //reading the mode value from the slider in Blynk app through virtual pin 0
{
volVar = param.asInt();
if (volVar==1)
{
Serial.println("Buzzer mode is in night mode-Lowest Pitch");//displaying the current buzzer mode
}else if (volVar==2)
{
Serial.println("Buzzer mode is in ambient mode-Medium Pitch");
}else
{
Serial.println("Buzzer mode is in away mode-Highest Pitch");
}
}
void setup() {
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pinMode(sensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
int buttonState=digitalRead(buttonPin);//reads state of the button
int sensorTrip=digitalRead(sensorPin);//reads motion trip in PIR sensor
if (sensorTrip==HIGH)
{
lastTrip=rtc.getLocalEpoch();//updating the latest motion trip of the PIR sensor
}
if (buttonState == HIGH)
{
buttonTrip=rtc.getLocalEpoch();
if((buttonTrip-lastTrip)<=(bufferTime-bufferDelay))//checking if the time of button push is
//within the buffer time of the latest motion trip
{
volVar=1;
Serial.println("Recent activity");
}
else{
Blynk.run(); //if not, obtain the mode variable val from blynk app
}
ringDoor(volVar); //playing the buzzer
}
sensorTrip=LOW;//resetting motion trip to low
}