#include <WiFi.h>
#include <UniversalTelegramBot.h>
#include <DHT.h>


// Credentials for Wi-Fi and Telegram Bot
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* botToken = "7520537091:AAExgS3yapX4mv9Wr7_Yhmdo9EL1mjV_pO8";
// Telegram Bot setup
WiFiClient client;
UniversalTelegramBot bot(botToken, client);
const int botRequestDelay = 1000;
unsigned long lastTimeBotRan;

// Define DHT sensor
#define DHTPIN 15  // Pin where the DHT22 is connected
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Define relay pins for 5 lamps
#define LAMP_1_PIN 23
#define LAMP_2_PIN 22
#define LAMP_3_PIN 21
#define LAMP_4_PIN 19
#define LAMP_5_PIN 18

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

  // Initialize DHT sensor
  dht.begin();
  // Set insecure connection for Wi-Fi client (useful for HTTPS)
  client.setInsecure();

  // Set Wi-Fi mode to Station (ESP32 as client)
  WiFi.mode(WIFI_STA);

  // Initialize Relay pins (set as output)
  pinMode(LAMP_1_PIN, OUTPUT);
  pinMode(LAMP_2_PIN, OUTPUT);
  pinMode(LAMP_3_PIN, OUTPUT);
  pinMode(LAMP_4_PIN, OUTPUT);
  pinMode(LAMP_5_PIN, OUTPUT);

  // Turn off all lamps initially
  digitalWrite(LAMP_1_PIN, LOW);
  digitalWrite(LAMP_2_PIN, LOW);
  digitalWrite(LAMP_3_PIN, LOW);
  digitalWrite(LAMP_4_PIN, LOW);
  digitalWrite(LAMP_5_PIN, LOW);

  // Connect to Wi-Fi
  Serial.print("Connecting to Wi-Fi");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Connected to Wi-Fi");

  // Print Bot status
  Serial.println("Bot is ready");
}

// Function to handle incoming Telegram messages
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;

    // Respond to Telegram commands
    if (text == "/start") {
      String welcomeMsg = "Welcome to ESP32 5 Lamp Control and DHT22 Bot!\n\n";
      welcomeMsg += "Here are the available commands:\n";
      welcomeMsg += "/lamp1_on - Turn on Lamp 1\n";
      welcomeMsg += "/lamp1_off - Turn off Lamp 1\n";
      welcomeMsg += "/lamp2_on - Turn on Lamp 2\n";
      welcomeMsg += "/lamp2_off - Turn off Lamp 2\n";
      welcomeMsg += "/lamp3_on - Turn on Lamp 3\n";
      welcomeMsg += "/lamp3_off - Turn off Lamp 3\n";
      welcomeMsg += "/lamp4_on - Turn on Lamp 4\n";
      welcomeMsg += "/lamp4_off - Turn off Lamp 4\n";
      welcomeMsg += "/lamp5_on - Turn on Lamp 5\n";
      welcomeMsg += "/lamp5_off - Turn off Lamp 5\n";
      welcomeMsg += "/status - Get temperature and humidity\n";

      bot.sendMessage(chat_id, welcomeMsg, "");
    }

    // Control Lamp 1
    if (text == "/lamp1_on") {
      digitalWrite(LAMP_1_PIN, HIGH);  // Turn on Lamp 1
      bot.sendMessage(chat_id, "Lamp 1 is ON", "");
    }

    if (text == "/lamp1_off") {
      digitalWrite(LAMP_1_PIN, LOW);   // Turn off Lamp 1
      bot.sendMessage(chat_id, "Lamp 1 is OFF", "");
    }

    // Control Lamp 2
    if (text == "/lamp2_on") {
      digitalWrite(LAMP_2_PIN, HIGH);  // Turn on Lamp 2
      bot.sendMessage(chat_id, "Lamp 2 is ON", "");
    }

    if (text == "/lamp2_off") {
      digitalWrite(LAMP_2_PIN, LOW);   // Turn off Lamp 2
      bot.sendMessage(chat_id, "Lamp 2 is OFF", "");
    }

    // Control Lamp 3
    if (text == "/lamp3_on") {
      digitalWrite(LAMP_3_PIN, HIGH);  // Turn on Lamp 3
      bot.sendMessage(chat_id, "Lamp 3 is ON", "");
    }

    if (text == "/lamp3_off") {
      digitalWrite(LAMP_3_PIN, LOW);   // Turn off Lamp 3
      bot.sendMessage(chat_id, "Lamp 3 is OFF", "");
    }

    // Control Lamp 4
    if (text == "/lamp4_on") {
      digitalWrite(LAMP_4_PIN, HIGH);  // Turn on Lamp 4
      bot.sendMessage(chat_id, "Lamp 4 is ON", "");
    }

    if (text == "/lamp4_off") {
      digitalWrite(LAMP_4_PIN, LOW);   // Turn off Lamp 4
      bot.sendMessage(chat_id, "Lamp 4 is OFF", "");
    }

    // Control Lamp 5
    if (text == "/lamp5_on") {
      digitalWrite(LAMP_5_PIN, HIGH);  // Turn on Lamp 5
      bot.sendMessage(chat_id, "Lamp 5 is ON", "");
    }

    if (text == "/lamp5_off") {
      digitalWrite(LAMP_5_PIN, LOW);   // Turn off Lamp 5
      bot.sendMessage(chat_id, "Lamp 5 is OFF", "");
    }

    // Get DHT22 status (temperature and humidity)
    if (text == "/status") {
      float temperature = dht.readTemperature();
      float humidity = dht.readHumidity();

      if (isnan(temperature) || isnan(humidity)) {
        bot.sendMessage(chat_id, "Failed to read from DHT sensor!", "");
      } else {
        String response = "Temperature: " + String(temperature) + "°C\n";
        response += "Humidity: " + String(humidity) + "%";
        bot.sendMessage(chat_id, response, "");
      }
    }
  }
}

void loop() {
  Serial.println("Checking for new messages...");  // For debugging
  int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  Serial.println("Number of messages: " + String(numNewMessages));  // Check if messages are received


  if (millis() > lastTimeBotRan + botRequestDelay) {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while (numNewMessages) {
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    lastTimeBotRan = millis();
  }
}