#include <WiFi.h>
#include <HTTPClient.h>
#define LED_PIN 2
#define PIR_PIN 15
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* LINE_TOKEN = "SnLUULkG1PxvNpR6rSLJoqUY3aIFlT9x2TRfeNLU29Z";
int pirState = LOW;
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT);
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
int pirValue = digitalRead(PIR_PIN);
if (pirValue == HIGH && pirState == LOW)
{
Serial.println("Motion detected!");
digitalWrite(LED_PIN, HIGH);
sendNotification("มีผู้บุกรุก ที่ห้องคอม บร.5 เฟรนด์");
pirState = HIGH;
} else if (pirValue == LOW && pirState == HIGH)
{
Serial.println("Motion stopped.");
digitalWrite(LED_PIN, LOW);
pirState = LOW;
}
delay(500);
}
void sendNotification(String message)
{
if (WiFi.status() == WL_CONNECTED)
{
HTTPClient https;
String url = "https://notify-api.line.me/api/notify";
https.begin(url);
https.addHeader("Authorization", "Bearer " + String(LINE_TOKEN));
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = https.POST("message=" + message);
if (httpCode == HTTP_CODE_OK)
{
Serial.println("Notification sent!");
} else
{
Serial.println("Error sending notification: " + String(httpCode));
}
https.end();
}
}