#include <LiquidCrystal.h>
LiquidCrystal lcd(19, 23, 18, 21, 5, 15);
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>   // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
#include <NTPClient.h>
#include <WiFiUdp.h>


// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String DT;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "in.pool.ntp.org", 19800); // UTC offset for Chennai (5 hours 30 minutes ahead)

int mixer=75,heater=300,fridge=400;
int total=mixer+heater+fridge;

// Initialize Telegram BOT
#define BOTtoken "7038921146:AAH0b15w-MJeIwCprNejHFwGeH9N-VIlUAM"
// Use @myidbot t find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "1487426406"

#ifdef ESP8266
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
#endif

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;

/*const int ledPin = 2;
bool ledState = LOW;*/

// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {
  Serial.println("handleNewMessages");
  Serial.println(String(numNewMessages));

  for (int i=0; i<numNewMessages; i++) {
    // Chat id of the requester
    String chat_id = String(bot.messages[i].chat_id);
    if (chat_id != CHAT_ID){
      bot.sendMessage(chat_id, "Unauthorized user", "");
      continue;
    }
    
    // Print the received message
    String text = bot.messages[i].text;
    Serial.println(text);

    String from_name = bot.messages[i].from_name;

    if (text == "/led_on") {
      bot.sendMessage(chat_id, "LED state set to ON", "");
      //ledState = HIGH;
      //digitalWrite(ledPin, ledState);
    }
    
    if (text == "/led_off") {
      bot.sendMessage(chat_id, "LED state set to OFF", "");
      //ledState = LOW;
      //digitalWrite(ledPin, ledState);
    }
    
    if (text == "/state") {
       // Combine all the information into a single string
                String message = "Mixer: " + String(mixer) + "\n" +
                 "Heater: " + String(heater) + "\n" +
                 "Fridge: " + String(fridge) + "\n" +
                 "Total: " + String(total);

// Send the message to Telegram
bot.sendMessage(chat_id, message, "");

      }
    }
  }

void setup() {
  lcd.begin(16, 2);
  lcd.print("Mixer :");
  lcd.print(mixer);
  lcd.print(" watts");
  lcd.setCursor(0,1); 
  lcd.print("Heater :");
  lcd.print(heater);
  lcd.print(" watts");
  Serial.begin(115200);

#ifdef ESP8266
  configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
#endif

  /*pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);*/
  
  // Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
#ifdef ESP32
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
#endif
while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to WiFi");

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  bot.sendMessage(CHAT_ID, "Bot Started", "");
  // Initialize NTP client
  timeClient.begin();
}
 
void loop() {
  lcd.setCursor(0, 1);
  if (millis() > lastTimeBotRan + botRequestDelay) {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while(numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }
    lastTimeBotRan = millis();
  }

  timeClient.update();

  // Print current date and time in Chennai
  Serial.print("Current date and time in Chennai: ");
  printDate(timeClient.getEpochTime());
  Serial.print(" ");
  String time = (timeClient.getFormattedTime());
  Serial.println(time);

  Serial.println(DT);

  delay(1000);
}

void printDate(unsigned long epochTime) {
  const char* months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  
  time_t time = (time_t)epochTime;
  struct tm* tm_info = localtime(&time);

  String month = months[tm_info->tm_mon];
  int date = tm_info->tm_mday;
  int year = tm_info->tm_year + 1900;
  
  /*Serial.print(month);
  Serial.print(" ");
  Serial.print(date);
  Serial.print(" ");
  Serial.print(year);
  Serial.print(" ");*/

  DT = String(month) + " " + int(date) + " ," + int(year) ;
  
}
current-sensorBreakout
current-sensorBreakout
current-sensorBreakout
Voltage sensorBreakout