#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6yofDSn7W"
#define BLYNK_TEMPLATE_NAME "Security Gate System"
#define BLYNK_AUTH_TOKEN "2MnrK2xNpTAoKrSQaJXY8I6X6jaeurv7"
#define ECHO_PIN 2
#define TRIG_PIN 4
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
Servo s;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// BLYNK_WRITE(V1) {
// int degServo = param.asInt();
// Serial.print("Received value from V1: ");
// Serial.println(degServo);
// s.attach(21);
// s.write(degServo);
// }
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
Serial.print("Received value from V3: ");
Serial.println(pinValue);
if (pinValue == 1)
{
digitalWrite(17, HIGH);
Serial.println("Plat Nomor tidak masuk dalam daftar");
}
else {
digitalWrite(17, LOW);
Serial.println("Lampu Merah dimatikan");
}
}
BLYNK_WRITE(V2)
{
int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
Serial.print("Received value from V2: ");
Serial.println(pinValue);
if (pinValue == 1)
{
digitalWrite(15, HIGH);
Serial.println("Plat Nomor masuk dalam daftar");
}
else {
digitalWrite(15, LOW);
Serial.println("Lampu Hijau dimatikan");
}
}
float readDistanceCM()
{
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void dataSensor()
{
float distance = readDistanceCM();
Serial.println("Dist: " + String(distance) + "cm");
Serial.println("---");
Blynk.virtualWrite(V0, distance);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Blynk.begin(auth, ssid, pass);
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(17, OUTPUT);
pinMode(15, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
Blynk.run();
dataSensor();
}