Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Last active December 25, 2020 02:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxpromer/df5d5b834acb24ed3a12fe8bb7b9c73d to your computer and use it in GitHub Desktop.
Save maxpromer/df5d5b834acb24ed3a12fe8bb7b9c73d to your computer and use it in GitHub Desktop.
void Line_Notify(String message) ;
#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("-------------");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment