#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Kredensial WiFi
const char* ssid = "Wokwi-GUEST"; // Ganti dengan SSID WiFi
const char* password = ""; // Ganti dengan password WiFi
// Token Bot Telegram
const char* botToken = "7934154852:AAG2u3Et3ZfQmb3BK-J7AUBuZIFKDfmP7Zo"; // Bot Token telegram
// Inisialisasi Telegram Bot
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
const int ledPin = 2; // Pin untuk LED
const int buttonPin = 4; // Pin untuk tombol
bool ledState = LOW; // Status awal LED
bool lastButtonState = LOW; // Status tombol sebelumnya
unsigned long lastTimeBotRan;
const int botInterval = 1000; // Interval cek pesan
void setup() {
Serial.begin(115200);
// Setup pin LED dan tombol
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Menggunakan pull-up internal
// Menghubungkan ke WiFi
Serial.print("Menghubungkan ke WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nTerhubung ke WiFi");
// Inisialisasi koneksi HTTPS
client.setInsecure();
// Kirim pesan ke Telegram saat ESP32 siap
bot.sendMessage("1512018307", "siap dan terhubung!", "");
// Pastikan LED mati pada awalnya
digitalWrite(ledPin, LOW);
} #include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// WiFi credentials
const char* ssid = "OPPO A78 5G";
const char* password = "sandiaja";
// Telegram Bot Token
const char* botToken = "7934154852:AAG2u3Et3ZfQmb3BK-J7AUBuZIFKDfmP7Zo";
// Initialize Telegram Bot
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
// LED and Button pin configuration
const int ledPin = 2;
const int buttonPin = 4; // Push button pin
bool ledState = LOW;
bool lastButtonState = HIGH; // Previous button state
// Timing variables
unsigned long lastTimeBotRan;
const int botInterval = 1000;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Configure button pin as input with pull-up resistor
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected to WiFi!");
client.setInsecure();
bot.sendMessage("1512018307", "Jarvis Asisten Bot siap", "");
// Ensure LED is off initially
digitalWrite(ledPin, LOW);
}
void loop() {
// Check button state
bool buttonState = digitalRead(buttonPin);
// Button pressed logic
if (buttonState == LOW && lastButtonState == HIGH) {
// Toggle LED state
ledState = !ledState;
digitalWrite(ledPin, ledState ? HIGH : LOW);
// Send message to Telegram
String message = ledState ? "LED sekarang: Nyala" : "LED sekarang: Mati";
bot.sendMessage("1512018307", message, "");
Serial.println(message);
// Debounce delay
delay(200);
}
lastButtonState = buttonState;
// Telegram update logic
if (millis() - lastTimeBotRan > botInterval) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("Ada pesan baru telegram");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
if (text == "nyala") {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
bot.sendMessage(chat_id, "LED Nyala", "");
Serial.println("Perintah dari telegram : Lampu Menyala");
}
else if (text == "mati") {
digitalWrite(ledPin, LOW);
ledState = LOW;
bot.sendMessage(chat_id, "LED Mati", "");
Serial.println("Perintah dari telegram : Lampu Mati");
}
else {
bot.sendMessage(chat_id, "Perintah tidak dikenal. Kirim 'nyala' atau 'mati'.", "");
}
}
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
// Tangani perintah "nyala"
if (text == "nyala") {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
bot.sendMessage(chat_id, "LED Nyala", "");
Serial.println("Perintah dari Telegram: LED Menyala");
}
// Tangani perintah "mati"
else if (text == "mati") {
digitalWrite(ledPin, LOW);
ledState = LOW;
bot.sendMessage(chat_id, "LED Mati", "");
Serial.println("Perintah dari Telegram: LED Mati");
}
// Tangani perintah yang tidak dikenali
else {
String errorMessage = "Perintah tidak dikenali. Berikut adalah perintah yang tersedia:\n";
errorMessage += "1. nyala - Untuk menyalakan LED\n";
errorMessage += "2. mati - Untuk mematikan LED\n";
bot.sendMessage(chat_id, errorMessage, "");
Serial.println("Perintah dari Telegram: Perintah Tidak Dikenali");
}
}
}
void loop() {
// Cek pesan baru setiap detik
if (millis() - lastTimeBotRan > botInterval) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("Pesan baru dari Telegram");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
// Cek status tombol
bool buttonState = digitalRead(buttonPin);
// Jika tombol ditekan
if (buttonState == LOW && lastButtonState == HIGH) {
// Toggle LED state
ledState = !ledState;
digitalWrite(ledPin, ledState);
// Kirim pesan ke Telegram
if (ledState) {
bot.sendMessage("151201830", "LED Nyala dari tombol", "");
Serial.println("Tombol ditekan: LED Menyala");
} else {
bot.sendMessage("151201830", "LED Mati dari tombol", "");
Serial.println("Tombol ditekan: LED Mati");
}
delay(200); // Debounce delay
}
// Simpan status tombol saat ini
lastButtonState = buttonState ;
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4