/*************************************************************
This sketch shows how to write values to Virtual Pins
NOTE:
BlynkTimer provides SimpleTimer functionality:
http://playground.arduino.cc/Code/SimpleTimer
App dashboard setup:
Value Display widget attached to Virtual Pin V5
*************************************************************/
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6eeOhjs8r"
#define BLYNK_TEMPLATE_NAME "sensor tinggi air"
#define BLYNK_AUTH_TOKEN "-fGBFKZvvwN7ZvF9Xsxzau8uBX7KZ3eP"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#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;
int trig_pin = 23;
int echo_pin = 22;
long echotime;
float luas_alas = 219.8;
float tinggi_gelas = 11.5;
float volume, tinggi_air, jarak_pantul;
// 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.
Blynk.virtualWrite(V1, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
digitalWrite(trig_pin, LOW);
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
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
echotime = pulseIn(echo_pin, HIGH);
jarak_pantul = (0.034 * (float) echotime)/2;
tinggi_air = tinggi_gelas - jarak_pantul;
volume = luas_alas * tinggi_air;
Serial.println("jarak permukaan = ");
Serial.println(jarak_pantul);
Serial.println(" Cm");
Serial.println("Tinggi air = ");
Serial.println(tinggi_air);
Serial.println(" Cm");
Serial.println("Volume = ");
Serial.println(volume);
Serial.println(" Cm3");
Serial.println("========================");
delay(1000);
Blynk.virtualWrite(V1, jarak_pantul);
Blynk.virtualWrite(V2, volume);
}