NodeMCU 控制 LINE傳送信息

1. 基本概念
    FTTT基於任務的條件觸發,類似程式語言,即:「若XXX進行YYY行為,執行ZZZ。」。每一個可以觸發或者作為任務的網站叫做一個Channel,觸發的條件叫做Triggers,之後執行的任務叫做Actions,綜合上面的一套流程叫做Task。(來源:維基百科)

  IFTTT能利用各種軟體的伺服器傳送資料,本實驗室由nodeMCU透過wifi連接網路再將訊息傳至webhooks,透過webhooks的網址傳送觸發啟動LINE訊息。

2. 設定IFTTT
(1) 開一個新的專案

 (2)點選If This (add)

 (3)搜尋webhooks

 (4)選擇觸發方式receive a web request

 (5)設定傳送名稱ex:line

(6)設定觸發後的動作
(7)選擇LINE

(8)選擇動作-傳送訊息

(9)設定訊息\群組\訊息內容\圖片


  • Recipient為要傳送的群組
  • Message內容之Value1為所要傳送的值,其他為文字
(10)完成動作


3. 測試
        (1)在My services 選擇webhooks
            
(2)點選Documentation即可進行觸發動作測試
(3)進行觸發設定
  • {event} 填入名稱 ex:line
  • {"value1" : "   ⃣  "}填入測試的數值 ex: 13579
  • 按 test it  即可測試line的設定是否成功


4.電路圖-按鈕觸發LINE測試
按鈕利用提升電阻(平時為HIGH,按下為LOW)接至D1。
並提供nodeMCU電源。


5. 撰寫程式

//本範例是利用一顆按鈕觸發LINE發送訊號

#include 
#include 
#define SW1 5                    //設定按鈕腳位
// 設定網路基地台SSID跟密碼

const char* ssid     = "EE620";         
const char* password = "ee620ee620";
const char* host = "maker.ifttt.com";  //Ifttt主機
const char* eventName   = "LINE001";  //Ifttt觸發條件名稱
const char* key = "dLORvC4astBhJ1NQ4nuEAk";  //Ifttt授權碼


void setup() {
  Serial.begin(9600);                 //序列埠連接包率
  Serial.print("Connecting to ");     //顯示文字
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  pinMode(SW1,INPUT);

}

void loop() {
  Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  if(SW1==0)
  {
    String url = "/trigger/";  //組合觸發網址
    url += eventName;
    url += "/with/key/";
    url += key;
    url += "?value1=";
    url += String(555);
    Serial.print("Requesting URL: ");
    Serial.println(url);  
    
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");
    int timeout = millis() + 5000;  
    while (client.available() == 0) {
      if (timeout - millis() < 0) {
        Serial.println("Client Timeout !");
        client.stop();
        return;
      }
    }
    while(client.available()){  //顯示回應訊息
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }
  }    
  Serial.println();
  Serial.println("closing connection");

  delay(1000);  //每秒鐘一次    
    
    
}

0 留言

張貼留言

Post a Comment (0)

較新的 較舊