#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <DHT.h>
#include "time.h"
// ================== WIFI ==================
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ================== TELEGRAM ==================
#define BOT_TOKEN "8756996903:AAExGSLxqXuAr8ws7rSCBpNNvxvlirYAc10"
#define CHAT_ID "7829056748"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// ================== DHT ==================
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// ================== OUTPUT ==================
#define BUZZER 4
#define LED_MERAH 16
#define LED_KUNING 17
#define LED_HIJAU 5
// ================== TIME (WIB) ==================
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7 * 3600; // WIB = UTC+7
const int daylightOffset_sec = 0;
// Status terakhir
String lastStatus = "";
// ================== SETUP ==================
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(BUZZER, OUTPUT);
pinMode(LED_MERAH, OUTPUT);
pinMode(LED_KUNING, OUTPUT);
pinMode(LED_HIJAU, OUTPUT);
// WiFi
WiFi.begin(ssid, password);
Serial.print("Menghubungkan WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Terhubung");
client.setInsecure(); // supaya HTTPS jalan
// Time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
bot.sendMessage(CHAT_ID, "✅ Sistem Cold Storage Aktif", "");
}
// ================== AMBIL WAKTU ==================
String getTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return "Waktu gagal";
}
char buffer[50];
strftime(buffer, sizeof(buffer), "%A, %d-%m-%Y %H:%M:%S WIB", &timeinfo);
return String(buffer);
}
// ================== LOOP ==================
void loop() {
float suhu = dht.readTemperature();
if (isnan(suhu)) {
Serial.println("Sensor Error!");
return;
}
String status;
String pesan;
// ================== KONDISI ==================
if (suhu > 5.0) {
status = "BAHAYA";
digitalWrite(BUZZER, HIGH);
digitalWrite(LED_MERAH, HIGH);
digitalWrite(LED_KUNING, LOW);
digitalWrite(LED_HIJAU, LOW);
} else if (suhu >= 2.0) {
status = "WASPADA";
digitalWrite(BUZZER, LOW);
digitalWrite(LED_MERAH, LOW);
digitalWrite(LED_KUNING, HIGH);
digitalWrite(LED_HIJAU, LOW);
} else {
status = "NORMAL";
digitalWrite(BUZZER, LOW);
digitalWrite(LED_MERAH, LOW);
digitalWrite(LED_KUNING, LOW);
digitalWrite(LED_HIJAU, HIGH);
}
// ================== KIRIM TELEGRAM ==================
if (status != lastStatus) {
String waktu = getTime();
pesan = "📢 NOTIFIKASI COLD STORAGE\n\n";
pesan += "🕒 Waktu : " + waktu + "\n";
pesan += "🌡 Suhu : " + String(suhu) + " °C\n";
pesan += "⚠ Status: " + status + "\n\n";
if (status == "BAHAYA") {
pesan += "🚨 SUHU TERLALU TINGGI!";
} else if (status == "WASPADA") {
pesan += "⚠ Perlu perhatian";
} else {
pesan += "✅ Kondisi aman";
}
bot.sendMessage(CHAT_ID, pesan, "");
lastStatus = status;
}
delay(3000);
}