#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
#define echoPin1 12
#define trigPin1 13
#define echoPin2 27
#define trigPin2 14
int period = 1000*300;
unsigned long time_now = 0;
//Start of State Ultrasonik
bool full =false;
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Initialize Telegram BOT
#define BOTtoken "6179388391:AAEEcHkaZR2r2A60YqSqF00-QvcpjAX-SUA" // your Bot Token (Get from Botfather)
#define CHAT_ID "1148387173"
#define id "1148387173"
#ifdef ESP8266
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
#endif
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
// START OF GETTING DATA ULTRASONIC
int ultra2(){
long duration; // variable for the duration of sound wave travel
int distance; // v
// Clears the trigPin condition
digitalWrite(trigPin2, LOW);
delayMicroseconds(10);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
return distance;
}
int ultra1(){
long duration; // variable for the duration of sound wave travel
int distance; // v
// Clears the trigPin condition
digitalWrite(trigPin1, LOW);
delayMicroseconds(10);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin1, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
return distance;
}
// END OF GETTING DATA ULTRASONIC
// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {
for (int i=0; i<numNewMessages; i++) {
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Welcome, " + from_name + ".\n";
welcome += "Use the following commands to get information.\n\n";
welcome += "/person to get exiting person \n";
welcome += "/state to get quota information \n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/state") {
if (full==true){
bot.sendMessage(chat_id, "Tong Sampah Penuh");
}
else{
bot.sendMessage(chat_id, "Tong Sampah Belum Penuh");
}
}
}
}
void setup() {
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin1, INPUT); // Sets the echoPin as an INPUT
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin2, INPUT); // Sets the echoPin as an INPUT
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with ESP32");
Serial.begin(115200);
#ifdef ESP8266
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
#endif
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
#ifdef ESP32
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
#endif
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
}
void checkultra(){
Serial.println("Ultra 1 : " + String(ultra1()));
Serial.println("Ultra 2 : " + String(ultra2()));
}
void loop() {
unsigned long waktu = millis();
if(ultra1()<=12 && ultra2()<=12){
full = true;
Serial.println("Tong Sampah Penuh");
if(millis() >= time_now + period){
time_now += period;
bot.sendMessage(id, "Tong Sampah Penuh");
}
}else{
full = false;
}
if (millis() > lastTimeBotRan + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while(numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}