#define BLYNK_TEMPLATE_ID "TMPL6x_IhrQ28"
#define BLYNK_TEMPLATE_NAME "Esp32 sensor ultasonic"
#define BLYNK_AUTH_TOKEN "dBd_T_u1SwFRst3fBWiOH0e8UxsjLt7H"
#define BLYNK_PRINT Serial //deklarasi untuk menampilkan keterangan mulai k 5 #define ledRed 13
#define ledRed 13
#define ledYellow 12
#define ledGreen 14
#define pinEcho 2
#define pinTrigger 15
//Libraries
#include <WiFi.h> //deklarasi library WiFi.h
#include <WiFiClient.h> //deklarasi library WiFiClient.h
#include <BlynkSimpleEsp32.h> //deklarasi library blynk
BlynkTimer timer; //merubah perintah BlynkTimer menjadi "timer"
char auth[] = "4CEmTQYCH2169p1HgAzMpLzhMPM20jYV"; //deklarasi auth token da
char ssid[] = "Wokwi-GUEST"; //ssid wifi yang digunakan
char pass[] = ""; //password wifi yang digunakan
// void sendData(){ //void untuk mengirimkan pembacaan sensor atau nilai ke float waterLevel = readUltrasonic();
// float riverLevel = 300+50-waterLevel
// Blynk.virtualWrite(V1, riverLevel); //mengirimkan data suhu ke virtual
// Serial.println("WaterLevel: " + String(riverLevel, 2) + " cm"); //mena
// Serial.println("------");
// }
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Selamat datang pada sistem tandon air otomatis");
Blynk.begin(auth, ssid, pass); //inisialisasi komunikasi dengan server bl
//timer.setInterval(2000L); //interval pengiriman sendData dari mikrokontroler
pinMode(pinEcho, INPUT);
pinMode(pinTrigger, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledGreen, OUTPUT);
}
float readUltrasonic() {
digitalWrite(pinTrigger, LOW);
delayMicroseconds(2);
//trigger PIN dengan sinyal HIGH selama 10micro detik
digitalWrite(pinTrigger, HIGH);
delayMicroseconds(10);
digitalWrite(pinTrigger, LOW);
//echo PIN dengan sinyal HIGH
int duration = pulseIn(pinEcho, HIGH);
float dist_cm = duration * 0.034 / 2;
return dist_cm;
//float dist_inch = duration * 0.0133 / 2;
}
void loop() {
// put your main code here, to run repeatedly:
float waterLevel = readUltrasonic();
Serial.println("Batas Ketinggian Air adalah: ");
Serial.println(waterLevel);
if(waterLevel > 150){
digitalWrite(ledGreen, HIGH);
digitalWrite(ledYellow, LOW);
digitalWrite(ledRed, LOW);
Blynk.virtualWrite(V0, waterLevel);
}
else if(waterLevel >100 && waterLevel <=150 ){
digitalWrite(ledYellow, HIGH);
digitalWrite(ledGreen, LOW);
digitalWrite(ledRed, LOW);
Blynk.virtualWrite(V0, waterLevel);
}
else if(waterLevel <=50) {
digitalWrite(ledRed, HIGH);
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, LOW);
}
Blynk.virtualWrite(V0, waterLevel);
Blynk.run(); //menjalankan perintah yang ada di library blynk
//timer.run(); //menjalankan timer dari blynk
}