#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST";
const char* password = " ";
const char* token = "UNuHlNN8kYeQOISWw5XiL2EYoz2LDpTigRBamlXJfum";
#define DHTPIN 2 // Pin connected to DHT22
#define DHTTYPE DHT22 // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
int pirPin = 4;
int ledPin = 18;
int buzzer = 5;
int pirState = LOW;
int val = 0;
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin("Wokwi-GUEST", "",6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(" READY ");
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(ledPin, LOW); // ให้ LED ปิดตอนเริ่มต้น
digitalWrite(buzzer, LOW);
}
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(pirPin); // 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=Motion Detected");
Serial.println("Motion detected!");
// 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=Motion ended");
Serial.println("Motion ended!");
// 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=อุณหภูมิสูงมาก ระวังเกิดอาการ Heat 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(3000); // Send a message and read sensor data every 5 seconds
}
}