#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <time.h>
#define WIFI_SSID "DHANUSH"
#define WIFI_PASSWORD "12345678"
#define BOT_TOKEN "8484728715:AAEafgC3slF9LM_g_bxnCNuramLIhi-XrCo"
const char* telegramHost = "api.telegram.org";
WiFiClientSecure client;
unsigned long lastUpdateId = 0;
const int ledPin = 2; // Change to GPIO 2 or LED_BUILTIN if needed
int ledStatus = 0;
void connectWiFi() {
Serial.print("Connecting to Wi-Fi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi connected.");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void syncTime() {
configTime(0, 0, "pool.ntp.org");
Serial.print("Synchronizing time");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println(" done.");
}
String getUpdates() {
String url = "/bot" + String(BOT_TOKEN) + "/getUpdates?offset=" + String(lastUpdateId + 1);
String payload = "";
bool jsonStarted = false;
if (!client.connect(telegramHost, 443)) {
Serial.println("Connection to Telegram failed");
return "";
}
client.println("GET " + url + " HTTP/1.1");
client.println("Host: api.telegram.org");
client.println("Connection: close");
client.println();
while (client.connected()) {
while (client.available()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
jsonStarted = true;
} else if (jsonStarted) {
payload += line;
}
}
}
client.stop();
Serial.println("Received JSON:");
Serial.println(payload);
return payload;
}
void sendMessage(String chat_id, String text) {
String url = "/bot" + String(BOT_TOKEN) + "/sendMessage";
String body = "chat_id=" + chat_id + "&text=" + text;
if (!client.connect(telegramHost, 443)) {
Serial.println("Connection to Telegram failed");
return;
}
client.println("POST " + url + " HTTP/1.1");
client.println("Host: api.telegram.org");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(body.length());
client.println();
client.print(body);
while (client.connected()) {
while (client.available()) {
client.read(); // Ignore response
}
}
client.stop();
}
void handleCommand(String text, String chat_id) {
if (text == "/ledon") {
digitalWrite(ledPin, HIGH);
ledStatus = 1;
sendMessage(chat_id, "LED is ON");
} else if (text == "/ledoff") {
digitalWrite(ledPin, LOW);
ledStatus = 0;
sendMessage(chat_id, "LED is OFF");
} else if (text == "/status") {
String status = (ledStatus == 1) ? "LED is ON" : "LED is OFF";
sendMessage(chat_id, status);
} else if (text == "/start") {
String welcome = "Welcome!\nCommands:\n/ledon\n/ledoff\n/status";
sendMessage(chat_id, welcome);
}
}
void checkTelegram() {
String payload = getUpdates();
if (payload == "") return;
DynamicJsonDocument doc(8192);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("JSON parse failed: ");
Serial.println(error.c_str());
return;
}
for (JsonObject result : doc["result"].as<JsonArray>()) {
lastUpdateId = result["update_id"].as<unsigned long>();
String message_text = result["message"]["text"].as<String>();
String chat_id = result["message"]["chat"]["id"].as<String>();
Serial.println("Command: " + message_text);
handleCommand(message_text, chat_id);
}
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
connectWiFi();
client.setInsecure(); // Don't verify SSL certificate
syncTime();
}
void loop() {
checkTelegram();
delay(1000); // Poll every 1s
}