#define BLYNK_TEMPLATE_ID "TMPL2fncfpLtN"
#define BLYNK_TEMPLATE_NAME "esp"
#define BLYNK_AUTH_TOKEN "iT3RMf_ITB3yMAL-haL6_rsw7NIrEKz3"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Pin definitions
#define TRIG_PIN 4 // Pin connected to Trig pin of ultrasonic sensor
#define ECHO_PIN 5 // Pin connected to Echo pin of ultrasonic sensor
#define LEDPIN 2 // Pin connected to the onboard LED (GPIO 2)
// Blynk credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "network";
char pass[] = "12345678";
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(LEDPIN, OUTPUT); // Set LED pin as output
pinMode(TRIG_PIN, OUTPUT); // Set Trig pin as output
pinMode(ECHO_PIN, INPUT); // Set Echo pin as input
}
BLYNK_WRITE(V3) {
int ledState = param.asInt(); // Get the value from the Blynk app (V3)
digitalWrite(LEDPIN, ledState); // Control LED based on Blynk command
}
void loop() {
Blynk.run(); // Run Blynk
// Clear the Trig pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the Trig pin HIGH for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the Echo pin and calculate the distance
long duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration * 0.034 / 2;
// Send the distance to the Blynk app
Blynk.virtualWrite(V4, distance); // Send distance data to virtual pin V4
delay(1000); // Delay between reads
}