ตัวอย่างงานที่ใช้โปรแกรม Arduino
ESP8266 / ESP8285 กับการส่งการแจ้งเตือนเข้า LINE

| #include <ESP8266WiFi.h> | |
| #include <WiFiClientSecureAxTLS.h> // กรณีขึ้น Error ให้เอาบรรทัดนี้ออก | |
| // Config connect WiFi | |
| #define WIFI_SSID "YOUR WIFINAME" | |
| #define WIFI_PASSWORD "YOUR WIFIPASSWORD" | |
| // Line config | |
| #define LINE_TOKEN "LINE ACCESS TOKEN" | |
| #define SW D2 | |
| String message = "โดนกด"; // ArduinoIDE เวอร์ชั่นใหม่ ๆ ใส่ภาษาไทยลงไปได้เลย | |
| void setup() { | |
| pinMode(SW, INPUT); | |
| Serial.begin(9600); | |
| WiFi.mode(WIFI_STA); | |
| // connect to wifi. | |
| WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
| Serial.print("connecting"); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| Serial.print("."); | |
| delay(500); | |
| } | |
| Serial.println(); | |
| Serial.print("connected: "); | |
| Serial.println(WiFi.localIP()); | |
| } | |
| void loop() { | |
| if (digitalRead(SW) == HIGH) { | |
| while(digitalRead(SW) == HIGH) delay(10); | |
| Serial.println("Enter !"); | |
| Line_Notify(message); | |
| // Serial.println(); | |
| } | |
| delay(10); | |
| } | |
| void Line_Notify(String message) { | |
| axTLS::WiFiClientSecure client; // กรณีขึ้น Error ให้ลบ axTLS:: ข้างหน้าทิ้ง | |
| if (!client.connect("notify-api.line.me", 443)) { | |
| Serial.println("connection failed"); | |
| return; | |
| } | |
| String req = ""; | |
| req += "POST /api/notify HTTP/1.1\r\n"; | |
| req += "Host: notify-api.line.me\r\n"; | |
| req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n"; | |
| req += "Cache-Control: no-cache\r\n"; | |
| req += "User-Agent: ESP8266\r\n"; | |
| req += "Connection: close\r\n"; | |
| req += "Content-Type: application/x-www-form-urlencoded\r\n"; | |
| req += "Content-Length: " + String(String("message=" + message).length()) + "\r\n"; | |
| req += "\r\n"; | |
| req += "message=" + message; | |
| // Serial.println(req); | |
| client.print(req); | |
| delay(20); | |
| // Serial.println("-------------"); | |
| while(client.connected()) { | |
| String line = client.readStringUntil('\n'); | |
| if (line == "\r") { | |
| break; | |
| } | |
| //Serial.println(line); | |
| } | |
| // Serial.println("-------------"); | |
}
![]()
การทดสอบ
หลังจาก ESP8266 เชื่อมต่อ WiFi ได้แล้ว ทดลองกดสวิตซ์ จะมีข้อความว่า "โดนกด" มาปรากฏในห้องแชทของ LINE Notify เป็นอันจบการทดสอบ
![]()
อ้างอิง : https://www.ioxhop.com/article/47/esp8266-esp8285-%E0%B8%81%E0%B8%B1%E0%B8%9A%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%AA%E0%B9%88%E0%B8%87%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%81%E0%B8%88%E0%B9%89%E0%B8%87%E0%B9%80%E0%B8%95%E0%B8%B7%E0%B8%AD%E0%B8%99%E0%B9%80%E0%B8%82%E0%B9%89%E0%B8%B2-line
|


ไม่มีความคิดเห็น:
แสดงความคิดเห็น