#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid ="Wokwi-GUEST";
const char* password ="";
const char* token ="knBnbuSUOJkuOGjgQsGK9X2fZUOlGWbu4CW2KjQcghV";
int pirPin = 13;
int ledPin = 2;
void setup() {
Serial.begin(9600);
WiFi.begin("Wokwi-GUEST","",6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // ให้ LED ปิดตอนเริ่มต้น
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
int pirValue = digitalRead(pirPin);
if (pirValue == HIGH) {
digitalWrite(ledPin, HIGH); // เปิด LED
delay(1000);
digitalWrite(ledPin, LOW); // ปิด LED
sendLineNotify("มีจดหมายใหม่"); // ส่งข้อความไปยัง LINE Notify
}
}
}
void sendLineNotify(String message) {
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");
String encodedMessage = "message=" + urlEncode(message);
int httpCode = http.POST(encodedMessage);
if (httpCode > 0) {
String response = http.getString();
// Serial.println(httpCode);
// Serial.println(response);
} else {
Serial.println("....");
}
http.end();
}
String urlEncode(String value) {
String encodedValue = "";
char c;
for (size_t i = 0; i < value.length(); i++) {
c = value.charAt(i);
if (isAlphaNumeric(c)) {
encodedValue += c;
} else {
encodedValue += String('%');
encodedValue += String(c, HEX);
}
}
return encodedValue;
}