#include <WiFi.h> // Library Wifi
#include <WiFiClientSecure.h> // Library Wifi
#include <UniversalTelegramBot.h> //Library Bot Telegram
#include <DHT.h> //Library DHT11
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define lampu 4 //
#define DHTPIN 2 // Contoh pin DHT
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Initialize Wifi connection to the router
char ssid[] = "Wokwi-GUEST"; // Wifi Name
char password[] = ""; // Wifi Password
// Initialize Telegram BOT
#define BOTtoken "7784143458:AAGzOdKhHU0gZ7JFA5Vx188p3L_jEWCzip4" // Bot token from telegram app
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
//Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address and dimensions (16x2)
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(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 == "/temperature") {
int t = dht.readTemperature();
String temp = "Temperature : ";
temp += int(t);
temp +=" *C\n";
bot.sendMessage(chat_id,temp, "");
if (t > 30){ //Jika suhu lebih dari 30 derajat celcius, maka
String temp = "Suhu Lebih Dari 30 Derajat Celcius ";
bot.sendMessage(chat_id,temp, "");
}
else { //Jika suhu kurang dari sama dengan 30 derajat celcius, maka
String temp = "Suhu Dibawah 30 Derajat Celcius ";
bot.sendMessage(chat_id,temp, "");
}
}
if (text == "/humidity") {
int h = dht.readHumidity();
String hum = "Humidity: ";
hum += int(h);
hum += " %";
bot.sendMessage(chat_id,hum, "");
}
if (text == "/lampuON") {
digitalWrite(lampu, HIGH);
lcd.setCursor(0, 2);
lcd.print("Lampu: Hidup ");
String Lampu1= "Lampu Hidup";
bot.sendMessage(chat_id,Lampu1, "");
}
else if (text == "/lampuOFF") {
digitalWrite(lampu, LOW);
lcd.setCursor(0, 2);
lcd.print("Lampu: Mati ");
String Lampu2= "Lampu Mati";
bot.sendMessage(chat_id,Lampu2, "");
}
if (text == "/start") {
String welcome = "Welcome " + from_name + ".\n";
welcome += "/temperature : Temperature reading\n";
welcome += "/humidity : Humiditiy reading\n";
welcome += "/lampuON : Lampu Hidup \n";
welcome += "/lampuOFF : Lampu Mati \n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
Serial.begin(115200); // Menggunakan baud rate 115200
dht.begin();
pinMode(lampu, OUTPUT); //Setting LED sebagai output
client.setInsecure();
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.print("Temperature:");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
}
void loop() {
double t = dht.readTemperature();
double h = dht.readHumidity();
// Update LCD display
lcd.setCursor(12, 0);
lcd.print(t);
lcd.setCursor(9, 1);
lcd.print(h);
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();
}
}