#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <ESP32Servo.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String BOT_TOKEN = "1640646548:AAGDxn2tn0TUGs2ZpGzanomrxOtpWBAF7p8";
const String CHAT_ID = "966600232";
const String TELEGRAM_API_URL = "https://api.telegram.org/bot" + BOT_TOKEN + "/sendMessage";
const byte TRIG_PIN = 4;
const byte ECHO_PIN = 5;
const byte TRIG_PIN1 = 26;
const byte ECHO_PIN1 = 27;
const byte SERVO_PIN = 18;
Servo servo;
WiFiClientSecure wifiClient;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
int retry_count = 0;
while (WiFi.status() != WL_CONNECTED && retry_count < 10) {
delay(1000);
Serial.println("Connecting to WiFi...");
retry_count++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi");
} else {
Serial.println("Failed to connect to WiFi");
}
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
servo.attach(SERVO_PIN);
servo.write(0);
wifiClient.setInsecure();
sendTelegramMessage("Online");
}
void loop() {
long duration, distance, duration1, distance1;
////////////////////ULTRASONIC 1////////////////////////////////////////////////////////
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1;
if (distance <= 0 || distance > 400) {
distance = 0;
return;
}
////////////////////ULTRASONIC 2////////////////////////////////////////////////////////
digitalWrite(TRIG_PIN1, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN1, LOW);
duration1 = pulseIn(ECHO_PIN1, HIGH);
distance1 = (duration1 / 2) / 29.1;
if (distance1 <= 0 || distance1 > 400) {
distance1 = 0;
return;
}
if (distance < 10) {
sendTelegramMessage("Sampah terdeteksi! Membuka penutup...");
openLid();
delay(5000);
closeLid();
sendTelegramMessage("Menutup penutup.");
}
if (distance1 < 10) {
sendTelegramMessage("Tempat Sampah Telah Penuh");
}
delay(1000);
}
void openLid() {
servo.write(90);
}
void closeLid() {
servo.write(0);
}
// void sendTelegramMessage(String message) {
// if (WiFi.status() == WL_CONNECTED) {
// HTTPClient http;
// String url = TELEGRAM_API_URL + "?chat_id=" + CHAT_ID + "&text=" + message;
// Serial.println(url);
// http.begin(wifiClient, url);
// int httpCode = http.GET();
// if (httpCode > 0) {
// String payload = http.getString();
// Serial.println(payload);
// } else {
// Serial.printf("HTTP error: %s\n", http.errorToString(httpCode).c_str());
// }
// http.end();
// } else {
// Serial.println("WiFi Disconnected");
// }
// }
void sendTelegramMessage(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://api.telegram.org/bot" + BOT_TOKEN + "/sendMessage?chat_id=" + CHAT_ID + "&text=" + message;
Serial.println(url);
http.begin(wifiClient, url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
} else {
Serial.printf("HTTP error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}