/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL2rUUIu0S"
#define BLYNK_TEMPLATE_NAME "water"
#define BLYNK_AUTH_TOKEN "WwUbtlRlooNUxLbavr8hm1dMlGfYnD_o"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define PIN_TRIG 25
#define PIN_ECHO 26
int distanceCm;
long duration;
long water;
const char* ssid = "Wokwi-GUEST"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "";
BlynkTimer timer;
void myTimer()
{
// This function describes what will happen with each timer tick
// e.g. writing sensor value to datastream V5
Blynk.virtualWrite(V1, water);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); //Optional
WiFi.begin(ssid, password);
Serial.println("\nConnecting");
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(100);
}
Serial.println("\nConnected to the WiFi network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
timer.setInterval(1000L, myTimer);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
duration = pulseIn(PIN_ECHO, HIGH);
// Calculating the distance
distanceCm = duration * 0.034 / 2;
water = map( distanceCm ,200,60,0,100);
Blynk.run();
timer.run();
}