แจ้งเตือนผ่าน Line Notify ด้วย NodeMCU หรือ ESP8622

กระทู้นี้เป็นการเริ่มต้นสำหรับ การส่งการแจ้งเตือนผ่าน Line โดยเริ่มจากการต่ออุปกรณ์เบื้องต้นสำหรับการกดเพื่อส่งค่า On และ Off กันก่อน

วงจรกดปุ่มแจ้งเตือนผ่าน line

Button_send_line_bb.pdf (647.4 KB)

Button_send_line.ino (1.8 KB)

โค้ตที่ใช้

const int led = 5;
const int button = 16;
int temp = 0;
bool Lock = false;

void setup() {
  Serial.begin(112500);
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);

}

void loop() {
  temp = digitalRead(button);
     
     if (temp == LOW) {
        digitalWrite(led, HIGH);
        Serial.println("LED Turned ON");
        delay(1000);

       }
     else {
        digitalWrite(led, LOW);
        Serial.println("LED Turned OFF");
        delay(1000);

       }
}

เมื่อเราทดสอบการกด On และ Off ว่าทำงานตามปกติก็ให้ใส่โค้ต ส่ง line โดยใช้ libraly ชื่อ TridentTD_LineNotify เผื่อความสะดวกรวดเร็วในการเขียนคำสั่งครับ สามารถดาวน์โหลดได้ที่

จากนั้นไปสมัคร line บนหน้าเว็บเพื่อเอา Token การแจ้งเตือนมาใช้

LINE Notify

LINE Notify คือบริการอันแสนสะดวกที่คุณสามารถรับข้อความแจ้งเตือนจากเว็บเซอร์วิสต่างๆ ได้ทาง LINE ไม่ว่าจะเป็น GitHub IFTTT Mackerel ฯลฯ

เมื่อ login เข้ามาแล้ว กด ออก Token แล้วใส่ชื่อที่จะเป็นเหมือนคนที่ส่ง line มาแจ้งเตือนเรา

จากนั้นเลือกคน หรือกลุ่มที่เราต้องการให้แจ้งไป แล้วกด ออก Token

image

เพิ่มการแจ้งเตือนผ่าน line

  ///// set button 1 ///
 #include <TridentTD_LineNotify.h>

#define SSID        "Eleceasy"
#define PASSWORD    "Bb20072536"
#define LINE_TOKEN  "DNi0g1Q9WuiBqhfTmKaHQWQdXXfIQ6qC0fpaH6MfJsu"
const int led = 5;
const int button = 16;
int temp = 0;
bool Lock = false;

void setup() {
  Serial.begin(112500);
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
  Serial.println(LINE.getVersion());
  WiFi.begin(SSID, PASSWORD);
  Serial.printf("WiFi connecting to %s\n",  SSID);
  while(WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(400); }
  Serial.printf("\nWiFi connected\nIP : ");
  Serial.println(WiFi.localIP());  

  // กำหนด Line Token
  LINE.setToken(LINE_TOKEN);

  // ตัวอย่างส่งข้อความ
  LINE.notify(String("Send to easyDemo ") + 10);

  // ตัวอย่างส่งข้อมูล ตัวเลข
  //LINE.notify(2342);          // จำนวนเต็ม
  //LINE.notify(212.43434,5);   // จำนวนจริง แสดง 5 หลัก

  // เลือก Line Sticker ได้จาก https://devdocs.line.me/files/sticker_list.pdf
  //LINE.notifySticker(3,240);        // ส่ง Line Sticker ด้วย PackageID 3 , StickerID 240
  //LINE.notifySticker("Hello",1,2);  // ส่ง Line Sticker ด้วย PackageID 1 , StickerID 2  พร้อมข้อความ

  // ตัวอย่างส่ง รูปภาพ ด้วย url
  //LINE.notifyPicture("https://preview.ibb.co/j6G51n/capture25610417181915334.png");
  //LINE.notifyPicture("จตุธาตุ","https://www.fotoaparat.cz/storage/pm/09/10/23/670915_a5351.jpg");
}

void loop() {
  temp = digitalRead(button);
     
     if (temp == LOW) {
      if(Lock == true){
        digitalWrite(led, HIGH);
        Serial.println("LED Turned ON");
        LINE.notify("LED ON");
        delay(1000);
        Lock = false;
      }
       }
     else {
      if(Lock == false){
        digitalWrite(led, LOW);
        Serial.println("LED Turned OFF");
        LINE.notify("LED OFF");
        delay(1000);
        Lock = true;
      }
       }
}

Button_NodeMCU_send_line.ino (2.1 KB)

void Line_Notify1(String message1) ;
#include <TridentTD_LineNotify.h>
#define WIFI_SSID "NBN_311523_2.4G"
#define WIFI_PASSWORD "311523BNN"
#define LINE_TOKEN_PIR "4AwdQx6Ak5qfhtbIRZzkrXxs3odXqaPx4QaDTtILvlD"
#define PirPin D6

String message1 = "you received a new parcel";
bool beep_state = false;
bool send_state = false;
uint32_t ts, ts1, ts2;
void setup() {
  Serial.begin(115200);
  Serial.println();
  pinMode(PirPin, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
 // dht.begin();
  Serial.println("connecting");
  WiFi.mode(WIFI_STA);
  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());
  delay(10000);
  Serial.println("Pir Ready!!");
  //read_sensor();
  ts = ts1 = ts2 = millis();
}
void loop() {
  ts = millis();
  if (WiFi.status() == WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, LOW);
  } else {
    digitalWrite(LED_BUILTIN, HIGH);
  }
    
  if ((ts - ts1 >= 5000) && (beep_state == true)) {
    beep_state = false;
  } 
  
  if ((digitalRead(PirPin) == HIGH) && (beep_state == false) && (WiFi.status() == WL_CONNECTED)) {
    while (digitalRead(PirPin) == HIGH) delay(100);
    Serial.println("Detect !");
    Line_Notify1(message1);
    beep_state = true;
  }
  delay(10);
}
void Line_Notify1(String message) {
  WiFiClientSecure client;
  if (!client.connect("notify-api.line.me", 443)) 
{
    Serial.println("connection failed");
    delay(2000);
    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_PIR) + "\r\n";
  req += "Cache-Control: no-cache\r\n";
  req += "User-Agent: ESP8266\r\n";
  req += "Content-Type: application/x-www-form-urlencoded\r\n";
  req += "Content-Length: " + String(String("message=" + message1).length()) + "\r\n";
  req += "\r\n";
  req += "message=" + message1;
  // Serial.println(req);
  client.print(req);
  delay(20);
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      break;
    }
  }
}

ผมใช้โค้ดนี้เพื่อให้มันตรวจจับวัตถุเนื่องจากผมทำตู้รับพัสดุคล้ายๆกับตู้รับจดหมายเพื่อจะให้มันแจ้งเตือนทางไลน์ รันทุกอย่างเรียบร้อย แต่เมื่อ serial monitor มันขึ้น detect และ connection failed เกิดจากอะไรหรอครับผมควรแก้ตรงไหนครับ พอดีผมทำโครงการจบแก้ทุกทางแล้วทำไม่ได้จริงๆครับ

ผมทำตามตัวอย่าง มันไม่แจ้งเตือนเข้าไปในไลน์เลยครับ รบกวนขี้แนะด้วยครับ
ลองกด sw แล้ว ขึ้นแค่ว่า ON OFF ใน Serial Monitor เท่านั้นครับ ผมใช้ esp32

code >>>

#include <TridentTD_LineNotify.h>
#define SSID “xxxxt”
#define PASSWORD “xxxxxxx”
#define LINE_TOKEN “LEpVjc6bUSUWdU6cFuCHGCZEaQhAuFDEtqswPMvotRd”

const int led = 23;
const int button = 13;
int temp = 0;
bool Lock = false;

void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
Serial.println(LINE.getVersion());
WiFi.begin(SSID, PASSWORD);
Serial.printf(“WiFi connecting to %s\n”, SSID);
while(WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(400); }
Serial.printf("\nWiFi connected\nIP : ");
Serial.println(WiFi.localIP());

// กำหนด Line Token
LINE.setToken(LINE_TOKEN);

// ตัวอย่างส่งข้อความ
LINE.notify(String("Send to easyDemo ") + 10);
}

void loop() {
temp = digitalRead(button);

 if (temp == LOW) {
  if(Lock == true){
    digitalWrite(led, HIGH);
    Serial.println("LED Turned ON");
    LINE.notify("LED ON");
    delay(5000);
    Lock = false;
   }
 }
 else {
  if(Lock == false) {
    digitalWrite(led, LOW);
    Serial.println("LED Turned OFF");
    LINE.notify("LED OFF");
    delay(5000);
    Lock = true;

   }

}
}

พี่คะ คือหนูWiFiClientSecure.h หาตัวนี้ไม่เจอ หนูจะโหลดไฟล์Zip ต้องทำไงคะ