วันจันทร์ที่ 23 กรกฎาคม พ.ศ. 2561

Arduino + W5100 ส่งการแจ้งเตือนเข้า LINE

Arduino + W5100 ส่งการแจ้งเตือนเข้า LINE

เมื่อ 2 เดือนที่ผ่านมา
โดย เจ้าของร้าน

Arduino + W5100 ส่งการแจ้งเตือนเข้า LINE
จากบทความของ น้องท่านหนึ่งในกลุ่ม Facebook Arduino Thailand ที่ได้เขียนบทความเกี่ยวกับการแจ้งเตือน ผ่าน Line Notify โดยใช้ Nodemcu esp8266 คราวนี้ทางร้านกลัว Arduino จะน้อยใจและเสียใจ เผื่อใครอยากใช้สาย Lan เลยพัฒนา Code ตัวอย่าง สำหรับ Arduino กันบ้าง แต่บทความนี้เราจะ ใช้ Server เข้ามาเกี่ยวค่องเพื่อให้ง่ายต่อการพัฒนา ต่อยอดไปยัง Hardware อื่นๆ อีกมากมายไม่ว่าจะเป็น อุปกรณ์ Hardware บนโลกนี้ที่สามารถเขียน โปรแกรมและเชื่อมต่อ Internet ได้ มาเริ่มกันเลย
หลักการทำงานครับ Line จะบังคับให้ใช้ Methol POST ในการส่งข้อมูล สำหรับมือใหม่ บางครั้งปวดหัวบ้างเวลาเขียนโปรแกรม ในตัวแปรผิดบ้าง ส่งไม่ออกบ้าง ทาง 9Arduino จึงพัฒนาระบบเล็ก เพื่อให้บริการ โดยอาศัย Server ของทาง 9Arduino เป็นตัว Forword จาก Methol Get (ที่เขียนโปรแกรมง่ายๆ) ผ่าน Server ของเรา แล้วส่งไปยัง Server ของ Line Notify
หลักการทำงานของระบบ
หากถามว่าเราสามารถ เขียน Code ให้เชื่อมต่อโดยตรงได้ไหม ตอบว่าได้ครับ แล้วแต่การออกแบบของแต่ละบุคคลครับ
อุปกรณ์ที่ จะใช้ในวันนี้มีดังนี้
  • Arduino Uno R3 (สามารถใช้ตัวอื่นได้น่ะครับไม่จำกัด)
  • Shield W5100 หรือ จะใช้ ENC28J60 ก้ได้แล้วแต่ความถนัด
  • สวิต สำหรับส่ง Logic High หรือ Low เพื่อทดสอบ
  • อุปกรณ์อื่นๆ เช่น สายไฟ บอร์ดทดลอง เป็นต้น
เรามาเริ่มกันเลย
ก่อนอื่น เช่นเดิมครับ เราต้องไปสร้าง Token จาก Line Notify ก่อนครับ วันนี้จะไม่พูดถึงวิธีการ ขอ Token จาก Line Notify สามารถไปอ่านได้ที่บทความ ระบบแจ้งเตือนการ Login เข้า Server ผ่าน Line Notify ได้เลยครับ
เริ่มต้นขั้นตอนแรก เข้าไปสร้าง Line Notify ก่อนเลยครับ
https://notify-bot.line.me/
ไปที่หน้าของฉัน
ออก Access Token (สำหรับผู้พัฒนา) เลือก ออก Token
สร้างชื่อ Bot ของเราลงไป และเลือกวิธีการรับข้อความ สามารถเลือกรับการแจ้งเตือนแบบตัวต่อตัว หรือแจ้งเตือนไปยังกลุ่มที่ต้องการ ก็ได้เหมือนกันครับ
จะได้ Token ออกมาให้จดหรือ Copy ไว้ เดี่ยวนำไปใช้ใน Code ของเรา
การต่อวงจร ดังนี้
Arduino + W5100 ส่งการแจ้งเตือนเข้า LINE
Arduino + W5100 ส่งการแจ้งเตือนเข้า LINE
ในส่วนของ Code
#include <SPI.h>
#include <Ethernet.h>
const char* token   = ".....";    // Token ที่ได้จาก Line Notify
const char* mes = "Hello%20World";    //ข้อความที่ต้องการให้แสดง
const int buttonPin = 2;    // Pin ของปุ่มกด
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);   // IP ของ Arduino
IPAddress myDns(8, 8, 8, 8);      // DNS แนะนำให้ใช้ 8.8.8.8 เป็นของ Google
EthernetClient client;
char server[] = "line.nisit.net";   //เชื่อมต่อแบบ DNS
//IPAddress server(103,233,194,42); //เชื่อมต่อแบบ IP
unsigned long lastConnectionTime = 0;             
const unsigned long postingInterval = 10L * 1000L; 
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  while (!Serial) {
  }
  delay(3000);
  Ethernet.begin(mac, ip, myDns);
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}
void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }
if (digitalRead(buttonPin) == HIGH) {         //เช็คการกดปุ่ม D2
   if (millis() - lastConnectionTime > postingInterval) {
   httpRequest();
   }
  } else {
    delay(500);
    Serial.print(".");
  }
}
void httpRequest() {
  client.stop();
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    client.print("GET /~aline/index.php?token=");
    client.print(token);
    client.print("&mes=");
    client.println(mes);
    client.println(" HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();
    lastConnectionTime = millis();
  } else {
    Serial.println("connection failed");
  }
}
สังเกตที่อักษรสีแดงครับ เป็นสิ่งที่เราต้องแก้ไข
const char* token   = "...";    // Token ที่ได้จาก Line Notify
const char* mes = "Hello%20World";    //ข้อความที่ต้องการให้แสดง
สำหรับนักพัฒนาหากต้องการนำ Code ไปพัฒนาต่อสามารถใช้ตัวอย่างไฟล์ Code ที่ทำการ Forword เพื่อส่งข้อมูลไปยัง Server ของ Line Notify ได้เลยครับ
$token = $_GET['token'];
$mes = $_GET['mes'];
define('LINE_API',"https://notify-api.line.me/api/notify");  
$res = notify_message($mes,$token);
print_r($res);
function notify_message($message,$token){
 $data = array('message' => $message);
 $data = http_build_query($data,'','&');
 $header = array( 
         'http'=>array(
            'method'=>'POST',
            'header'=> "Content-Type: application/x-www-form-urlencoded\r\n"
                      ."Authorization: Bearer ".$token."\r\n"
                      ."Content-Length: ".strlen($data)."\r\n",
            'content' => $data
         ),
 );
 $context = stream_context_create($header);
 $result = file_get_contents(LINE_API,FALSE,$context);
 $res = json_decode($result);
 return $res;
}
สามารถใช้ไฟล์นี้นำไปใส่ใน Server ที่รองรับ Webserver php แล้วแก้ไข Code ตัวอย่างยิงเข้ามาใส่ใน Server ที่ได้สร้างขึ้นได้เหมือนกัน
VDO ผลการทดลอง
วันนี้ก็จบเพียงเท่านี้ หวังว่าจะมีประโยชน์สำหรับเพื่อนๆ น้องๆที่ต้องการ ใช้ Arduino Uno ส่งข้อความแจ้งเตือนทาง Line ครับ

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

แสดงความคิดเห็น