#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Suchat_2"; // ใส่ชื่อ Wi-Fi ของคุณ
const char* password = "0857432070"; // ใส่รหัสผ่าน Wi-Fi ของคุณ
const char* lineToken = "IPjbGk5tH89reotEdxzSDKYbUA6TKkE2YlHEkJHXEwz"; // ใส่ LINE Notify Access Token
int ledPin = 2; // ตั้งค่าขา LED
int ledState = LOW; // สถานะเริ่มต้นของ LED
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// เชื่อมต่อ Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// เปลี่ยนสถานะ LED (จำลองการเปลี่ยนสถานะด้วยการหน่วงเวลา)
ledState = !ledState;
digitalWrite(ledPin, ledState);
if (ledState == HIGH) {
sendLineNotification("LED ON");
} else {
sendLineNotification("LED OFF");
}
delay(5000); // หน่วงเวลา 5 วินาทีเพื่อดูการเปลี่ยนแปลงสถานะ
}
void sendLineNotification(String message) {
if (WiFi.status() == WL_CONNECTED) { // ตรวจสอบการเชื่อมต่อ Wi-Fi
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify"); // API URL สำหรับ LINE Notify
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + String(lineToken));
String postData = "message=" + message;
int httpResponseCode = http.POST(postData); // ส่ง POST request
if (httpResponseCode > 0) {
String response = http.getString(); // รับข้อความตอบกลับจากเซิร์ฟเวอร์
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.println("Error on sending POST: " + String(httpResponseCode));
}
http.end(); // ปิดการเชื่อมต่อ HTTP
} else {
Serial.println("Error: WiFi not connected");
}
}