ESP8266 Telegram Relay

i have program for activate relay with telegram, its work but not really work 100%, because its doesn't give me reply from as my coding do and the wifi esp had connected, thanks for the time before

#include <ESP8266WiFi.h>
#include <UniversalTelegramBot.h>


const char* ssid = "*****"; 
const char* password = "*****";
const char* telegramToken = "*****"; 
const char* chat_id = "*****";

WiFiClientSecure client;
UniversalTelegramBot bot(telegramToken, client);
 

const int relayPin = 5;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi!");

  Serial.println("Telegram bot connected!");
  
  pinMode(relayPin, OUTPUT);
}

void loop() {
  // Mendapatkan pesan dari Telegram
  int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  
  while (numNewMessages) {
    Serial.println("New message received!");
    
    String command = bot.messages[numNewMessages - 1].text;
    Serial.println("Command: " + command);
    
    if (command == "/on") {
      digitalWrite(relayPin, HIGH); // Menyalakan relay
      bot.sendMessage(bot.messages[numNewMessages - 1].chat_id, "Relay turned ON!");
    } else if (command == "/off") {
      digitalWrite(relayPin, LOW); // Mematikan relay
      bot.sendMessage(bot.messages[numNewMessages - 1].chat_id, "Relay turned OFF!");
    } else {
      bot.sendMessage(bot.messages[numNewMessages - 1].chat_id, "Invalid command!");
    }
    
    numNewMessages--;
  }

  delay(1000);
}

I have the impression there's something missing between these two lines.

What is your actual question? It probably doesn't work 100% because it doesn't react on WiFi disconnects (or Telegram disconnects I guess, but that part is missing).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.