#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* token = "i5Jl53UeTS0kDqzELnYcHDC04bFNHrDo2NPZu4MlHVm"; //Tokeh Line ที่ต้องการให้แจ้งเตือน
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
#define DHTPIN 4 // Pin connected to DHT22
#define DHTTYPE DHT22 // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
dht.begin();
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
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");
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
http.POST("message=แจ้งเตือนผู้บุกรุก");
Serial.println("ส่งข้อความ : แจ้งเตือนผู้บุกรุก!");
Serial.println("==================================");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
http.POST("message=เหตุการณ์ปกติ");
Serial.println("ส่งข้อความ : เหตุการณ์ปกติ!");
Serial.println("==================================");
// We only want to print on the output change, not state
pirState = LOW;
}
}
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("ความชื้นสัมพัทธ์: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("อุณหภูมิ: ");
Serial.print(temperature);
Serial.println(" °C");
if (temperature > 40) {
http.POST("message=อุณหภูมิ = "+String(temperature,2)+" C"+"\n🌡อุณหภูมิสูงมาก ระวังเกิดอาการ \nHeat Stroke");
Serial.println("High temperature message sent");
}
if (humidity < 40) {
http.POST("message= ความชื้นต่ำ ควรเปิดเครื่องกรองอากาศ 🌫");
Serial.println("Low humidity message sent");
Serial.println("==================================");
}
}
int httpCode = http.POST("READY"); // Use an empty POST request to send only headers
if (httpCode > 0) {
String response = http.getString();
//Serial.print("HTTP Response code: ");
//Serial.println(httpCode);
//Serial.print("Server response: ");
// Serial.println(response);
} else {
Serial.println(".....");
}
http.end();
delay(1000); // Send a message and read sensor data every 5 seconds
}
}