#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
//Wifi connection
char ssid[] = "Wokwi-GUEST"; //SSID (Wifi)
char password[] = ""; //PASSWORD (WIFI)
//Telegram BOT
#define BOTtoken "5540487735:AAHYC7Y-8YjxmWQz8ch91n_sq9d0lTuUl-A" //BOT Token
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //respon bot
long Bot_lasttime; //respon bot setelah exsekusi
bool Start = false;
const int Buzzer_Pin = 33;
const int Led_Pin1 = 26;
const int Led_Pin2 = 27;
const int Pir_Pin = 25;
const int Push_Button = 32;
int Pirstatus = 0;
bool set_pir = false;
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
if (text == "/Pir_Pinon") {
digitalWrite(Pir_Pin, HIGH); // turn the Pir_Pin on
bot.sendSimpleMessage(chat_id, "Pir_Pin is ON. Kembali ke /start", "");
}
else if (text == "/Pir_Pinoff") {
digitalWrite(Pir_Pin, LOW); // turn the Pir_Pin off
bot.sendSimpleMessage(chat_id, "Pir_Pin is OFF. Kembali ke /start", "");
}
else if (text == "/status") {
if(Pirstatus){
bot.sendSimpleMessage(chat_id, "Pir_Pin is ON", "");
} else {
bot.sendSimpleMessage(chat_id, "Pir_Pin is OFF", "");
}
}
else if (text == "/start") {
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".%0A%0A";
welcome += "This is Flash Pir_Pin Bot example.";
welcome += "/Pir_Pinon : to switch the Pir_Pin ON.";
welcome += "/Pir_Pinoff : to switch the Pir_Pin OFF.";
welcome += "/status : Returns current status of Pir_Pin.";
bot.sendSimpleMessage(chat_id, welcome, "");
}
}
}
void setup() {
Serial.begin(9600);
// jika wifi diskonek
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// connect Wifi
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
client.setInsecure();
pinMode(Push_Button, INPUT_PULLUP);
pinMode(Buzzer_Pin, OUTPUT);
pinMode(Pir_Pin, OUTPUT); // pin mode output.
digitalWrite(Pir_Pin, LOW); // Pir_Pin kedadaan mati
delay(10);
digitalWrite(Led_Pin2, LOW); // led keadaan mati
pinMode(Led_Pin2, OUTPUT);
delay(10);
digitalWrite(Led_Pin1, LOW); // led keadaan mati
pinMode(Led_Pin1, OUTPUT);
delay(10);
}
void loop() {
if(digitalRead(Pir_Pin) == HIGH){
digitalWrite(Led_Pin1, HIGH);
digitalWrite(Led_Pin2, LOW);
Serial.println("Motion Detected");
delay(10);
}
else{
digitalWrite(Led_Pin1, LOW);
digitalWrite(Led_Pin2, HIGH);
Serial.println("No Motion");
delay(10);
}
if(digitalRead(Push_Button) == LOW){
digitalWrite(Buzzer_Pin, HIGH);
tone(Buzzer_Pin, 1000);
delay(1000);
digitalWrite(Buzzer_Pin, LOW);
noTone(8);
delay(10);
digitalWrite(Buzzer_Pin, HIGH);
tone(Buzzer_Pin, 1000);
delay(1000);
digitalWrite(Buzzer_Pin, LOW);
noTone(8);
delay(10);
}
if (millis() > Bot_lasttime + Bot_mtbs) {
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);
}
Bot_lasttime = millis();
}
}