#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include "ThingSpeak.h"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int myChannelNumber = 2561732;
const char* myAPIKey = "I6P5Q1VIU8WDZEKT";
const char* server = "api.thingspeak.com";
WiFiClient client;
const int servoPin = 16;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define trigPin 12
#define echoPin 13
#define BUZZER_PIN 4
Servo myservo;
long duration;
int distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
myservo.attach(servoPin, 1000, 2400);
LCD.init();
LCD.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
LCD.println("SELAMAT DATANG!!");
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("Wifi connected !");
Serial.println(WiFi.localIP());
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); //memulai thingspeak
}
void loop() {
// put your main code here, to run repeatedly:
myservo.write(90);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;
Serial.print("JARAK : ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
if (distance >= 150 && distance <= 200)
{
myservo.write(0);
tone(BUZZER_PIN, 1000);
delay(1000);
noTone(BUZZER_PIN);
LCD.clear();
LCD.setCursor(0,0);
LCD.println("SILAHKAN TEMPEL");
LCD.setCursor(6,1);
LCD.print("KARTU");
delay(3000);
}
else if (distance > 200)
{
myservo.write(90);
noTone(BUZZER_PIN);
LCD.clear();
LCD.println("SELAMAT DATANG!!");
//delay(3000);
}
else
{
myservo.write(90);
}
delay(10);
ThingSpeak.setField(1,distance);
int x = ThingSpeak.writeFields(myChannelNumber,myAPIKey);
}