#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
// กำหนดค่าต่างๆ
#define DHTPIN 4 // กำหนดพินที่เชื่อมต่อเซ็นเซอร์
#define DHTTYPE DHT22 // กำหนดประเภทเซ็นเซอร์ DHT11 หรือ DHT22
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Wokwi-GUEST"; // ชื่อ Wi-Fi ของคุณ
const char* password = ""; // รหัสผ่าน Wi-Fi ของคุณ
const char* lineNotifyToken = "mdMZj9Mjn001fQgwjSv0BZhHsdGGsSqGlrqDmJ7eh2z"; // Token ของ Line Notify
unsigned long previousMillis = 0; // ตัวแปรสำหรับเวลา
const long interval = 100000; // 1 ชั่วโมง (3600000 ms)
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
const long currentMillis = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
// เช็คค่าวัดว่าถูกต้องหรือไม่
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// แสดงผลที่ Serial Monitor
Serial.println("Temperature: " + String(t) + "°C, Humidity: " + String(h) + "%");
delay(2000); // อ่านค่าใหม่ทุก 2 วินาที
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
String message = "";
bool alert = false;
// ตรวจสอบอุณหภูมิ
if (t > 26) {
message += "อุณหภูมิผิดปกติ: " + String(t) + "°C\n";
alert = true;
} else {
message += "อุณหภูมิปกติ: " + String(t) + "°C\n";
}
// ตรวจสอบความชื้น
if (h < 40) {
message += "ความชื้นผิดปกติ: " + String(h) + "%\n";
alert = true;
} else {
message += "ความชื้นปกติ: " + String(h) + "%\n";
}
// ส่งข้อความแจ้งเตือน
if (alert) {
sendLineNotify(message);
}
Serial.println(message);
delay(2000); // อ่านค่าใหม่ทุก 2 วินาที
}
}
void sendLineNotify(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + String(lineNotifyToken));
http.POST("message=" + message);
http.end();
} else {
Serial.println("WiFi not connected");
}
}