#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* token = "dd29rh50b4aqSpxZCIRKORryyxvQoYNpXOaw87q7OnX"; // Token จาก https://notify-bot.line.me/th/
#define DHTPIN 4 // ขาที่เชื่อมต่อกับ DHT22
#define DHTTYPE DHT22 // ประเภทของเซ็นเซอร์ DHT
DHT dht(DHTPIN, DHTTYPE);
bool highTempNotified = false;
bool highHumidityNotified = false;
void setup() {
Serial.begin(115200);
delay(4000);
dht.begin();
delay(2000);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("กำลังเชื่อมต่อ WiFi...");
}
Serial.println("เชื่อมต่อ WiFi แล้ว");
Serial.println("-----พร้อมทำงาน-----");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Authorization", "Bearer " + String(token));
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(humidity) && isnan(temperature)) {
Serial.println("ไม่สามารถอ่านข้อมูลจากเซ็นเซอร์ DHT!");
} else {
Serial.print("ความชื้น: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("อุณหภูมิ: ");
Serial.print(temperature);
Serial.println(" °C");
String humid = String(humidity);
String tempMessage = String(temperature);
// แจ้งเตือนเมื่ออุณหภูมิสูง
if (temperature > 35 && !highTempNotified) {
http.POST("message=Server Security: แจ้งเตือน!!!-|🌡อุณหภูมิสูง🌡| (" + tempMessage + " °C)");
Serial.println("ส่งข้อความเตือนอุณหภูมิสูง");
highTempNotified = true;
} else if (temperature <= 35 && highTempNotified) {
http.POST("message=Server Security: 🌡อุณหภูมิ🌡 กลับสู่ปกติ (" + tempMessage + " °C)");
Serial.println("อุณหภูมิกลับสู่ปกติ");
highTempNotified = false;
}
// แจ้งเตือนเมื่อความชื้นสูง
if (humidity > 50 && !highHumidityNotified) {
http.POST("message=Server Security: แจ้งเตือน!!!-|🌫ความชื้นสูง🌫| (" + humid + " %)");
Serial.println("ส่งข้อความเตือนความชื้นสูง");
highHumidityNotified = true;
} else if (humidity <= 50 && highHumidityNotified) {
http.POST("message=Server Security: 🌫ความชื้น🌫 กลับสู่ปกติ (" + humid + " %)");
Serial.println("ความชื้นกลับสู่ปกติ");
highHumidityNotified = false;
}
// Post READY message
int httpCode = http.POST("message=Server Security: พร้อมทำงาน (ความชื้น: " + humid + " %, อุณหภูมิ: " + tempMessage + " °C)");
}
http.end();
delay(5000);
}
}