#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6Ine-LWO5"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "w2Y-OQsWXCBIrJYKs_bZsFYErfx1dj6J"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define LED 23
#define PIN_ECHO 12
#define PIN_TRIG 13
#define Photo 14
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const float GAMMA = 0.7;
const float RL10 = 50;
BlynkTimer timer;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
digitalWrite(PIN_TRIG, OUTPUT);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
int distance = duration / 58;
Serial.print("Distance in CM: ");
Serial.println(distance);
int photoValue = digitalRead(Photo);
Serial.print("Photoresistor: ");
Serial.println(photoValue);
String value = "";
if(distance < 50 || photoValue == HIGH){
value = "ON";
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(500);
}else{
value = "OFF";
delay(1000);
}
// Update state
Blynk.virtualWrite(V1, value);
}
void setup() {
Serial.begin(115200);
pinMode(LED,OUTPUT);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(Photo, INPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
void loop() {
Blynk.run();
timer.run();
}