// #include <WiFi.h>
// #include <WiFiClient.h>
// String ssid = "Wokwi-GUEST"; //nama ssid wifi kalian
// String pass = ""; //password wifi kalian
// String token = "6518526803:AAEe9_lemPbNfjXYz4fRxsS8AtslFXoDmoos"; //token bot baru kalian
// const int id = 5412754826; //id telegram kalian
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// SSID dan Password WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Telegram BOT
#define BOTtoken "6444844687:AAHsAIA87cVL0TL5V_ycU4ySiHvD1UVZH30"
#define CHAT_ID "5587146263"
#define pin_merah 23
#define pin_putih 22
#define pin_biru 21
#define pin_pir 15
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Cek Pesan Setiap 1 detik
int interval = 1000;
unsigned long waktu_terakhir;
bool status_merah, status_putih;
void setup() {
// atur mode pin menjadi output
pinMode(pin_merah, OUTPUT);
pinMode(pin_putih, OUTPUT);
pinMode(pin_biru, OUTPUT);
pinMode(pin_pir, INPUT);
// Serial monitor
Serial.begin(115200);
// Hubungkan ke Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Sedang menghubungkan ke WiFi..");
}
// IP Address
Serial.println(WiFi.localIP());
Serial.print("Atur Waktu: ");
configTime(0, 0, "pool.ntp.org");
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop() {
cek_pesan();
if (digitalRead(pin_pir) == HIGH) {
digitalWrite(pin_merah, HIGH);
digitalWrite(pin_putih, HIGH);
digitalWrite(pin_biru, HIGH);
bot.sendMessage(CHAT_ID, "Terfeteksi pergerakan", "");
Serial.println("Sensor activated!");
} else {
digitalWrite(pin_merah, LOW);
digitalWrite(pin_putih, LOW);
digitalWrite(pin_biru, LOW);
Serial.println("sensor not active!");
}
delay(100); // Delay a little bit to improve simulation performance
}
// Cek Pesan Terbaru
void cek_pesan(){
if (millis() > waktu_terakhir + interval){
int banyakPesan = bot.getUpdates(bot.last_message_received + 1);
while(banyakPesan) {
Serial.println("got response");
handleNewMessages(banyakPesan);
banyakPesan = bot.getUpdates(bot.last_message_received + 1);
}
waktu_terakhir = millis();
}
}
// Memproses pesan yang diterima
void handleNewMessages(int numNewMessages) {
for (int i=0; i<numNewMessages; i++) {
// Cek Chat ID
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Terima pesan dari telegram
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Selamat Datang, " + from_name + ".\n";
welcome += "Komen berikut untuk kontrol LED.\n\n";
welcome += "/merah_on untuk LED merah ON \n";
welcome += "/merah_off untuk LED merah OFF \n";
welcome += "/putih_on untuk LED putih ON \n";
welcome += "/putih_off untuk LED putih OFF \n";
welcome += "/biru_on untuk LED biru ON \n";
welcome += "/biru_off untuk LED biru OFF \n";
welcome += "/status_merah mendapatkan status LED merah\n";
welcome += "/status_putih mendapatkan status LED putih\n";
welcome += "/status_biru mendapatkan status LED biru\n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/merah_on") {
bot.sendMessage(chat_id, "LED merah ON", "");
led_merah(true);
}
if (text == "/merah_off") {
bot.sendMessage(chat_id, "LED merah OFF", "");
led_merah(false);
}
if (text == "/biru_on") {
bot.sendMessage(chat_id, "LED biru ON", "");
led_biru(true);
}
if (text == "/biru_off") {
bot.sendMessage(chat_id, "LED biru OFF", "");
led_biru(false);
}
if (text == "/putih_on") {
bot.sendMessage(chat_id, "LED putih ON", "");
led_putih(true);
}
if (text == "/putih_off") {
bot.sendMessage(chat_id, "LED putih OFF", "");
led_putih(false);
}
if (text == "/status_merah") {
if (digitalRead(pin_merah)){
bot.sendMessage(chat_id, "LED merah ON", "");
}
else{
bot.sendMessage(chat_id, "LED merah OFF", "");
}
}
if (text == "/status_putih") {
if (digitalRead(pin_putih)){
bot.sendMessage(chat_id, "LED putih ON", "");
}
else{
bot.sendMessage(chat_id, "LED putih OFF", "");
}
}
if (text == "/status_biru") {
if (digitalRead(pin_biru)){
bot.sendMessage(chat_id, "LED biru ON", "");
}
else{
bot.sendMessage(chat_id, "LED biru OFF", "");
}
}
}
}
void led_merah(bool isOn){
if(isOn){
digitalWrite(pin_merah, HIGH);
}
else{
digitalWrite(pin_merah, LOW);
}
}
void led_putih(bool isOn){
if(isOn){
digitalWrite(pin_putih, HIGH);
}
else{
digitalWrite(pin_putih, LOW);
}
}
void led_biru(bool isOn){
if(isOn){
digitalWrite(pin_biru, HIGH);
}
else{
digitalWrite(pin_biru, LOW);
}
}