Skip to content

Instantly share code, notes, and snippets.

@fxprime
Created November 27, 2019 08:03
Show Gist options
  • Save fxprime/2caa48f1c6910cf02819b585012ca758 to your computer and use it in GitHub Desktop.
Save fxprime/2caa48f1c6910cf02819b585012ca758 to your computer and use it in GitHub Desktop.
จากบทความ แจ้งเตือนไฟดับผ่าน line notification
/***********************|
* WiFI สำหรับ ESP8266 |
***********************/
#include <ESP8266WiFi.h>
#include <WiFiClientSecureAxTLS.h> // กรณีขึ้น Error ให้เอาบรรทัดนี้ออก
/************************|
* รหัส WiFi ที่ต้องเชื่อมต่อ |
************************/
char* ssid = "Kanuengnit";
char* password = "0869420966";
/************************|
* ตั้งค่า Static IP ตรงนี้ |
************************/
IPAddress local_IP(192, 168, 43, 123);
IPAddress gateway(192, 168, 43, 1);
IPAddress subnet(255, 255, 254, 0);
IPAddress primaryDNS(8, 8, 8, 8);
/*******************************|
* ตัวแปรที่เกี่ยวข้องกับการเปิดปิด LED |
*******************************/
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output2State = "off";
// Assign output variables to GPIO pins
const int output2 = 2;
/*******************************|
* ตัวแปรที่เกี่ยวข้องกับการเปิดปิด LED |
*******************************/
void Line_Notify(String message) ;
#define LINE_TOKEN "5qpaOYpwgVaEPq3JpATsFH5NWe195FrLQB9LVF6Uf0c"
void setup() {
/*******************************|
* ตัวแปรที่เกี่ยวข้องกับการเปิดปิด LED |
*******************************/
pinMode(output2, OUTPUT);
/***************************************|
* เริ่มการสื่อสารแบบ Serial baudrate 115200 |
***************************************/
Serial.begin(115200);
Serial.println("Started \n\n\n");
//
// /************************************************|
// * เริ่มตั้งค่า WiFi ด้วย Static ip ที่ตั้งไว้ด้วยคำสั่ง config |
// * และต่อ WiFi ด้วยคำสั่ง begin |
// ***********************************************/
// //Format : void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
// WiFi.config(local_IP, primaryDNS, gateway, subnet);
//
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(50);
Serial.print(".");
}
// แสดงค่า Ip หลังต่อเชื่อมต่อ WiFi สำเร็จแล้ว
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
/*************************|
* Stabilize ตัว analog |
*************************/
int val;
for(int i=0; i<30; i++) {
delay(10);
val = analogRead(A0);
}
if(val > 300) {
Line_Notify( "ไฟติดแล้วจ้า" );
}
}
bool isPowerDown = false;
bool lastPowerState = false;
void blinkPeriod() {
if(millis() % 1000 < 500) {
digitalWrite(output2, true);
}else{
digitalWrite(output2, false);
}
}
void readAnalogSensor() {
static uint32_t last_st = millis();
if( millis() - last_st < 50 )
return;
last_st = millis();
isPowerDown = (analogRead(A0) < 300);
Serial.println(analogRead(A0));
if(!isPowerDown) {
digitalWrite(output2, false);
}else{
blinkPeriod();
}
if(lastPowerState != isPowerDown) {
Line_Notify( (isPowerDown ? "ไฟดับจ้า":"ไฟติดแล้วจ้า"));
lastPowerState = isPowerDown;
}
}
void loop(){
readAnalogSensor();
}
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