#define BLYNK_TEMPLATE_ID "TMPL6kvrBXJBS"
#define BLYNK_TEMPLATE_NAME "Smart Home Trainer"
#define BLYNK_AUTH_TOKEN "XeFwxIWHS1hgB2evzKVsVLOR8KJUE7et"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
String data1,data2;
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// BLYNK_CONNECTED() {
// Blynk.syncVirtual(V1);
// Blynk.syncVirtual(V2);
// Blynk.syncVirtual(V3);
// Blynk.syncVirtual(V4);
// }
// BLYNK_WRITE(V1) {
// vsw1 = param.asInt();
// }
// BLYNK_WRITE(V2) {
// vsw2 = param.asInt();
// }
// BLYNK_WRITE(V3) {
// vsw3 = param.asInt();
// }
// BLYNK_WRITE(V4) {
// vsw4 = param.asInt();
// }
BlynkTimer timer;
const int trigPin = 5;
const int echoPin = 18;
long duration;
float distanceCm;
float distanceInch;
const int buzzer = 25;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
void setup() {
Serial.begin(115200); // Starts the serial communication
Blynk.begin(auth, ssid, pass);
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT); // Sets buzzer as an Output
}
void loop() {
Blynk.run();
timer.run();
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED / 2;
// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;
if(distanceCm < 100)
tone(buzzer, 262, 250);
else tone(buzzer, 0, 0);
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print("Data1: ");
// set cursor to first column, second row
lcd.print(distanceCm);
lcd.print("cm");
lcd.setCursor(0, 1);
lcd.print("Data2: ");
// set cursor to first column, second row
lcd.print(distanceInch);
lcd.print("in");
data1 = "Jarak 1 : "+String(distanceCm)+"cm";
data2 = "Jarak 2 : "+String(distanceInch)+"inch";
Blynk.virtualWrite(V1, distanceCm);
Blynk.virtualWrite(V2, distanceInch);
delay(1000);
lcd.clear();
}