#include <WiFi.h>
#include "DHT.h"
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
DHT dht(18, DHT22);
#define ledPin 2
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define BOT_TOKEN "8412476718:AAFQhYH89R83iqoi3Z0VNxDlxEOivu9mu5w"
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
float suhu;
int kelembaban;
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;
if (from_name == "")
from_name = "Guest";
if (text == "/ledon") {
digitalWrite(ledPin, LOW);
bot.sendMessage(chat_id, "Led is ON " + chat_id, "");
}
if (text == "/ledoff") {
digitalWrite(ledPin, HIGH);
bot.sendMessage(chat_id, "Led is OFF" + chat_id, "");
}
if (text == "/ceksensor") {
String balasan = "Lapor Boss, Kondisi sensor saat ini:\n";
balasan += "Suhu : " + String(suhu) + " Celsius\n";
balasan += "Kelembaban : " + String(kelembaban) + " %\n";
bot.sendMessage(chat_id, balasan, "Markdown");
}
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";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup(){
Serial.begin(115200);
Serial.println();
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
lcd.backlight(); lcd.init();
lcd.setCursor(0,0); lcd.print("PROJECT TELEGRAM");
lcd.setCursor(0,1); lcd.print("Connecting...");
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());
lcd.setCursor(0,1); lcd.print("WiFi Connected..");
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);
dht.begin();
}
void loop(){
kelembaban = dht.readHumidity();
suhu = dht.readTemperature();
lcd.setCursor(0,1);
lcd.print("T="); lcd.print(suhu,1); lcd.write(223); lcd.print("C ");
lcd.print("H="); lcd.print(kelembaban); lcd.print("% ");
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();
}
}Loading
esp32-devkit-v1
esp32-devkit-v1