#define BLYNK_TEMPLATE_NAME "light"
#define BLYNK_AUTH_TOKEN "eZvQklag8EkKXmNcvLUYhT_0tX1q9eJI"
#define BLYNK_TEMPLATE_ID "TMPL32aIeJ5xF"
#include <BlynkSimpleEsp32.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 m_p_u;
#define TRIG_PIN1 5
#define ECHO_PIN1 18
#define TRIG_PIN2 16
#define ECHO_PIN2 17
#define TRIG_PIN3 4
#define ECHO_PIN3 15
#define TRIG_PIN4 13
#define ECHO_PIN4 14
#define BUZZER_PIN 19
char auth[]="eZvQklag8EkKXmNcvLUYhT_0tX1q9eJI";
char ssid[]="Wokwi-GUEST";
char pass[]="";
void setup() {
Blynk.begin(auth,ssid,pass);
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial)
delay(20);
if(!m_p_u.begin()){
while(1){
delay(20);
}
}
// Initialize ultrasonic sensors
pinMode(TRIG_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIG_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);
pinMode(TRIG_PIN3, OUTPUT);
pinMode(ECHO_PIN3, INPUT);
pinMode(TRIG_PIN4, OUTPUT);
pinMode(ECHO_PIN4, INPUT);
// Initialize buzzer
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW); // Ensure buzzer is off at the start
}
long readUltrasonicDistance(int trigPin, int echoPin) {
long duration, distance;
// Clear the TRIG_PIN by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the TRIG_PIN HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the ECHO_PIN, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
return distance;
}
void loop() {
Blynk.run();
long left = readUltrasonicDistance(TRIG_PIN1, ECHO_PIN1);
long right = readUltrasonicDistance(TRIG_PIN2, ECHO_PIN2);
long front = readUltrasonicDistance(TRIG_PIN3, ECHO_PIN3);
long back = readUltrasonicDistance(TRIG_PIN4, ECHO_PIN4);
Serial.print("left:");
Serial.println("The obstacle is found at the Left side");
Serial.print(left);
Serial.println(" cm");
Serial.print("right:");
Serial.println("The obstacle is found at the Right side");
Serial.print(right);
Serial.println(" cm");
Serial.print("front:");
Serial.println("The obstacle is found at the Front side");
Serial.print(front);
Serial.println(" cm");
Serial.print("back:");
Serial.println("The obstacle is found at the Back side");
Serial.print(back);
Serial.println(" cm");
// If any sensor detects an object closer than 10 cm, turn on the buzzer
if((left<=10))
{
tone(BUZZER_PIN,1000); //set the buzzer as an output
delay(1000); //wait for 500 milliseconds
Blynk.virtualWrite(left,HIGH);
}
else
{
noTone(BUZZER_PIN); //stop the tone
delay(200); //wait for 500 milliseconds
Blynk.virtualWrite(left,LOW);
}
if((right<=10))
{
tone(BUZZER_PIN,1000); //set the buzzer as an output
delay(1000); //wait for 500 milliseconds
Blynk.virtualWrite(right,HIGH);
}
else
{
noTone(BUZZER_PIN); //stop the tone
delay(200); //wait for 500 milliseconds
Blynk.virtualWrite(right,LOW);
}
if((front<=10))
{
tone(BUZZER_PIN,1000); //set the buzzer as an output
delay(1000); //wait for 500 milliseconds
Blynk.virtualWrite(front,HIGH);
}
else
{
noTone(BUZZER_PIN); //stop the tone
delay(200); //wait for 500 milliseconds
Blynk.virtualWrite(front,LOW);
}
if((back<=10))
{
tone(BUZZER_PIN,1000); //set the buzzer as an output
delay(1000); //wait for 500 milliseconds
Blynk.virtualWrite(back,HIGH);
}
else
{
noTone(BUZZER_PIN); //stop the tone
delay(200); //wait for 500 milliseconds
Blynk.virtualWrite(back,LOW);
}
{
sensors_event_t acc, gcc, temp;
m_p_u.getEvent(&acc, &gcc, &temp);
Serial.println("Acceleration on x axes");
Serial.println(acc.acceleration.x);
delay(1000); // this speeds up the simulation
Serial.println("Acceleration on y axes");
Serial.println(acc.acceleration.y);
delay(1000);
Serial.println("Acceleration on z axes");
Serial.println(acc.acceleration.z);
delay(1000);
Serial.println("Rotation of x axes: ");
Serial.println((gcc.gyro.x)*180/3.14);
Serial.println("Rotation of y axes: ");
Serial.println((gcc.gyro.x)*180/3.142);
Serial.println("Rotation of z axes: ");
Serial.println((gcc.gyro.x)*180/3.142);
delay(1000);
Serial.println("Temperature:");
Serial.println(temp.temperature);
Serial.println("degc");
Serial.println("");
delay(1000);
if((acc.acceleration.x) || (acc.acceleration.y) || (acc.acceleration.z) > 0)
{
Serial.println("gps is activated");
}
else{
Serial.println("gps is unactivated");
}
}
}