#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
DHT dht(18, DHT22); //Pin, Jenis DHT
// SSID dan Password WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
float lembab;
float suhu;
// Telegram BOT
#define BOTtoken "7173383839:AAEYFkKKelZAp_RTzGGH3yYLzrJeptM2WIQ"
#define CHAT_ID "945029011"
#define pin_biru 2
#define pin_buzzer 13 // Pin untuk buzzer
//init buzzer
int freq = 2000;
int channel = 0;
int resolution = 8;
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Cek Pesan Setiap 1 detik
int interval = 1000;
unsigned long waktu_terakhir;
bool status_biru;
void setup() {
// atur mode pin menjadi output
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("IOT SUHU-LEMBAB");
LCD.setCursor(0, 1);
LCD.print(" KELAS CE-4B ");
pinMode(pin_biru, OUTPUT);
pinMode(pin_buzzer, OUTPUT); // Atur pin buzzer sebagai output
// Serial monitor
Serial.begin(115200);
// Hubungkan ke Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Sedang menghubungkan ke WiFi..");
}
// IP Address
Serial.println(WiFi.localIP());
Serial.print("Atur Waktu: ");
configTime(0, 0, "pool.ntp.org");
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop() {
lembab = dht.readHumidity();
suhu = dht.readTemperature();
LCD.setCursor(0, 0);
LCD.print("SUHU : ");
LCD.print(round(suhu));
LCD.print(" C ");
LCD.setCursor(0, 1);
LCD.print("LEMBAB : ");
LCD.print(round(lembab));
LCD.print(" % ");
cek_pesan();
}
// Cek Pesan Terbaru
void cek_pesan(){
if (millis() > waktu_terakhir + interval){
int banyakPesan = bot.getUpdates(bot.last_message_received + 1);
while(banyakPesan) {
Serial.println("got response");
handleNewMessages(banyakPesan);
banyakPesan = bot.getUpdates(bot.last_message_received + 1);
}
waktu_terakhir = millis();
}
}
// Memproses pesan yang diterima
void handleNewMessages(int numNewMessages) {
for (int i=0; i<numNewMessages; i++) {
// Cek Chat ID
String chat_id = String(bot.messages[i].chat_id);
Serial.println(chat_id);
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Terima pesan dari telegram
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Selamat Datang, " + from_name + ".\n";
// Komen berikut untuk kontrol LED dan buzzer
welcome += "/ledon \n"; // untuk LED biru ON
welcome += "/ledoff \n"; // untuk LED biru OFF
welcome += "/statusled\n"; // mendapatkan status LED biru
welcome += "/suhu\n"; // mendapatkan suhu
welcome += "/lembab\n"; // mendapatkan kelembaban
welcome += "/buzzon \n"; // untuk buzzer ON
welcome += "/buzzoff \n"; // untuk buzzer OFF
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/ledon") {
bot.sendMessage(chat_id, "LED biru ON", "");
led_biru(true);
}
if (text == "/ledoff") {
bot.sendMessage(chat_id, "LED biru OFF", "");
led_biru(false);
}
if (text == "/suhu") {
String msg = "Suhu ruangan: ";
msg += suhu;
msg += "C";
bot.sendMessage(chat_id, msg, "");
}
if (text == "/lembab") {
String msg = "Kelembaban: ";
msg += lembab;
msg += "%";
bot.sendMessage(chat_id, msg, "");
}
if (text == "/statusled") {
if (digitalRead(pin_biru)){
bot.sendMessage(chat_id, "LED biru ON", "");
} else {
bot.sendMessage(chat_id, "LED biru OFF", "");
}
}
if (text == "/buzzon") {
bot.sendMessage(chat_id, "Buzzer ON", "");
buzzer(true);
}
if (text == "/buzzoff") {
bot.sendMessage(chat_id, "Buzzer OFF", "");
buzzer(false);
}
}
}
void led_biru(bool isOn){
if (isOn) {
digitalWrite(pin_biru, HIGH);
} else {
digitalWrite(pin_biru, LOW);
}
}
void buzzer(bool isOn) {
if (isOn) {
digitalWrite(pin_buzzer, HIGH);
} else {
digitalWrite(pin_buzzer, LOW);
}
}