#define BLYNK_TEMPLATE_ID "TMPLYru4IzZt"
#define BLYNK_DEVICE_NAME "DHT22"
#define BLYNK_AUTH_TOKEN "6Au9hCXXvKxYcs340Bmnn60wAmjeQqhU"

char auth[] = "6Au9hCXXvKxYcs340Bmnn60wAmjeQqhU";


#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define  trig  D1
#define  echo  D2

long duration;
int distance;

char ssid[] = "Wokwi-GUEST";
char pass[] = "";

BlynkTimer timer;

void setup()
{
  // Debug console
  pinMode(trig, OUTPUT);  
  pinMode(echo, INPUT);  
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);


  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}
void sendSensor()
{
  digitalWrite(trig, LOW);   
  delayMicroseconds(2);      

  digitalWrite(trig, HIGH);  
  delayMicroseconds(10);      
  digitalWrite(trig, LOW);   

  duration = pulseIn(echo, HIGH);   
  distance = duration * 0.034 / 2;   

  Serial.print("Distance = ");       
  Serial.println(distance);
  
  if(distance <= 5)
  {
    Blynk.tweet("My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk");
    Blynk.notify("Post has been twitted");
  }
  Blynk.virtualWrite(V0, distance);
  delay(1000);                       
}