#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include "DHT.h"
#define LED_Naik 5
#define LED_Turun 22
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define WIFI_SSID "FITRIA AMRINA"
#define WIFI_PASSWORD "20071982"
#define BOT_TOKEN "5062535577:AAFB-k-dWQG9HmEPduqFq8jNZaPWA4YEoes"
#define CHAT_ID "375153334"
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
const unsigned long BOT_MTBS = 1000;
unsigned long bot_lasttime;
float temperatureC;
float humidity;
void handleNewMessages(int numNewMessages)
{
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID )
{
bot.sendMessage(chat_id, "Unauthorized user", "");
}
else
{
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
if (text == "/status")
{ String msg = "Temperatur : ";
msg += msg.concat(temperatureC);
msg += "'C\n";
msg += "Kelembapan : ";
msg += msg.concat(humidity);
msg += "%\n";
bot.sendMessage(chat_id,msg, "");
}
if (text == "/start")
{
String welcome = "MONITORING RUANGAN KUBIKEL \n";
welcome += "/status : nilai Suhu dan Humidity \n";
bot.sendMessage(chat_id, welcome, "");
}
}
}
}
void setup()
{
pinMode(LED_Naik, OUTPUT);
pinMode(LED_Turun, OUTPUT);
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
dht.begin();
}
void loop()
{
humidity = dht.readHumidity();
temperatureC = dht.readTemperature();
if (humidity <65 && temperatureC >35)
{
digitalWrite(LED_Naik, HIGH);
digitalWrite(LED_Turun, LOW);
}
else if (humidity >75 && temperatureC <29)
{
digitalWrite(LED_Naik, LOW);
digitalWrite(LED_Turun, HIGH);
}
else
{
digitalWrite(LED_Naik, LOW);
digitalWrite(LED_Turun, LOW);
}
delay(1000);
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();
}
}