#define BLYNK_TEMPLATE_ID "TMPL68QtvehBr"
#define BLYNK_TEMPLATE_NAME "SmartGlasses"
#define BLYNK_AUTH_TOKEN "lUp3lxRbgkVF_QQPil7HgWJ8Cuxf-QEr"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define PIN_TRIG 12
#define PIN_ECHO 14
#define LEDR 2
#define LEDY 3
#define LEDG 2
#define BTN 5
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
int cm = readDistanceCM();
if (cm <100){
digitalWrite(LEDR, HIGH);
}else{
digitalWrite(LEDR, LOW);
}
Blynk.virtualWrite(V1, cm);
Serial.println(cm);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(LEDR,OUTPUT);
pinMode(BTN, INPUT_PULLUP);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}
int readDistanceCM() {
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
return duration * 0.034 / 2;
}