#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define BOT_TOKEN "8412476718:AAFQhYH89R83iqoi3Z0VNxDlxEOivu9mu5w"
#define green 25
#define yellow 26
#define red 27
#define led 2
#define kuning 18
#define DHTPIN 19
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done
void handleNewMessages(int numNewMessages) {
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++){
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
Serial.print("Pengirim : ");
Serial.print(chat_id); Serial.print(", ");
Serial.println(from_name);
if (from_name == "")
from_name = "Guest";
if (text == "/ledon"){
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
bot.sendMessage("chat_id", "Oke BOSS, Lampu saya nyalakan", "");
bot.sendMessage("7407329348", "Oke BOSS, Lampu saya nyalakan", "");
}
if (text == "/ledoff"){
digitalWrite(led, LOW); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "Led is OFF", "");
}
if (text == "/ceksensor"){
}
if (text == "/start"){
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
welcome += "This is Flash Led Bot example.\n\n";
welcome += "/ledon : to switch the Led ON\n";
welcome += "/ledoff : to switch the Led OFF\n";
welcome += "/status : Returns current status of LED\n";
welcome += "Contact Us: Difi (0852327864848) \n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
pinMode(led,OUTPUT);
pinMode(kuning,OUTPUT);
pinMode(red,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(green,OUTPUT);
lcd.backlight(); lcd.init();
lcd.setCursor(0,0); lcd.print(" JAM DIGITAL ");
Serial.begin(115200);
Serial.println("Hello, ESP32!");
dht.begin();
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600) {
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop() {
if (millis() - bot_lasttime > BOT_MTBS) {
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);
}
bot_lasttime = millis();
}
if(Serial.available()) {
char input_serial = Serial.read();
if(input_serial == 'a') {
digitalWrite(kuning,HIGH);
lcd.setCursor(9,1); lcd.print("K = ON ");
}
else if(input_serial == 'B') {
digitalWrite(kuning,LOW);
lcd.setCursor(9,1); lcd.print("K = OFF");
}
}
digitalWrite(led,HIGH);
lcd.setCursor(0,1); lcd.print("LED ON ");
delay(500);
digitalWrite(led,LOW);
lcd.setCursor(0,1); lcd.print("LED OFF");
delay(500);
int h = dht.readHumidity();
float t = dht.readTemperature();
lcd.setCursor(0,0);
lcd.print("T="); lcd.print(t,1); lcd.write(223); lcd.print("C ");
lcd.print("H="); lcd.print(h); lcd.print("% ");
if(t<30) {
digitalWrite(green,HIGH);
digitalWrite(yellow,LOW);
digitalWrite(red,LOW);
}
else if(t>=30 && t<=40) {
digitalWrite(green,LOW);
digitalWrite(yellow,HIGH);
digitalWrite(red,LOW);
}
else {
digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
digitalWrite(red,HIGH);
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
}