Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 "7111827908:AAFHy4wG3M26dLu9LmwElZYQiGwmQCvHDZs"
- #define CHAT_ID "11933555"
- #define pin_led 23 // Pin untuk LED
- WiFiClientSecure client;
- UniversalTelegramBot bot(BOTtoken, client);
- // Cek Pesan Setiap 1 detik
- int interval = 1000;
- unsigned long waktu_terakhir;
- void setup() {
- // Atur mode pin menjadi output
- pinMode(pin_led, OUTPUT);
- // 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..");
- }
- // Tampilkan IP Address
- Serial.println(WiFi.localIP());
- }
- void loop() {
- cek_pesan();
- }
- // Cek Pesan Terbaru
- void cek_pesan() {
- if (millis() > waktu_terakhir + interval) {
- int banyakPesan = bot.getUpdates(bot.last_message_received + 1);
- while (banyakPesan) {
- 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);
- if (text == "/led1on") {
- bot.sendMessage(chat_id, "LED ON", "");
- digitalWrite(pin_led, HIGH); // Nyalakan LED
- }
- if (text == "/led1off") {
- bot.sendMessage(chat_id, "LED OFF", "");
- digitalWrite(pin_led, LOW); // Matikan LED
- }
- }
- }
Advertisement