2023年7月3日 星期一

ESP32 Line Notify

 ESP32 Line Notify 

參考來源 https://youyouyou.pixnet.net/blog/post/120275932-%E7%AC%AC%E5%8D%81%E4%B8%89%E7%AF%87-esp32-line%E9%80%9A%E7%9F%A5%EF%BC%9A%E5%80%89%E5%BA%AB%E6%BA%AB%E5%BA%A6%E7%95%B0%E5%B8%B8%E6%A9%9F%E5%99%A8%E4%BA%BA



測試程式

#include <WiFiManager.h>         // https://github.com/tzapu/WiFiManager
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
//請修改以下參數--------------------------------------------
//char SSID[] = "TOTOLINK_A3002MU";//你的手機熱點名稱
//char PASSWORD[] = "24063173";//熱點密碼
//char SSID[] = "alex9ufo";//你的手機熱點名稱
//char PASSWORD[] = "alex9981";//熱點密碼
String Linetoken = "A2tZgAzEJis1w6xgNeJuID0ew9Nza8cQ8ECylbX5fbG";//跟LINE申請
int sensor;
int randNumber;
//---------------------------------------------------------

WiFiClientSecure client;//網路連線物件
char host[] = "notify-api.line.me";//LINE Notify API網址
#define BUILTIN_LED 2
//======================================================
void autoconnect() {
  // WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  // it is a good practice to make sure your code sets wifi mode how you want it.

  // put your setup code here, to run once:


  delay(1000);

    // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  //===================================================  
  // WiFiManager
  // Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
 
  // Uncomment and run it once, if you want to erase all the stored information
  //wifiManager.resetSettings();
 
  // set custom ip for portal
  //wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

  // fetches ssid and pass from eeprom and tries to connect
  // if it does not connect it starts an access point with the specified name
  // here  "AutoConnectAP"
  // and goes into a blocking loop awaiting configuration
  //=================================================
  Serial.println("Configuring your Mobile WiFi to esp32ap...");
  Serial.println("Configuring another WiFi SSID,PWD...");
  //===================================================  
  wifiManager.autoConnect("AutoConnectAP");
  // or use this for auto generated name ESP + ChipID
  //wifiManager.autoConnect();
 
  // if you get here you have connected to the WiFi
  Serial.println("Connected.");

  Serial.println("Configuring ESP32...");
  pinMode(BUILTIN_LED, OUTPUT);
}

void setup() {
  //連線到指定的WiFi SSID
  Serial.begin(115200);
  pinMode(BUILTIN_LED, OUTPUT);
  Serial.print("Connecting to wifi.... ");
  //================================================================
  client.setInsecure();//ESP32核心 1.0.6以上  
  // 會遇到「Connection Failed」的問題,主要是因為ESP32的1.0.6的核心會強迫使用SSL認證,
  //此時可以先透過在Setup()內加上「client.setInsecure();」語法來解決
  //================================================================
  autoconnect();
  // if you get here you have connected to the WiFi
  Serial.println("Connected.");

}

void loop() {

  //嘗試讀取感測值內容
    // print a random number from 0 to 299
  randNumber = random(400);
  Serial.println(randNumber);
  //sensor = analogRead(36);//GPIO值(36為ADC0)
  sensor =randNumber;
  //讀取成功
  Serial.print("Sample OK: ");
  Serial.println(sensor);

  //設定觸發LINE訊息條件為
  if ( sensor >200) {
    //組成Line訊息內容
    String message = "發生異常,目前狀態:";
    message += "\n偵測品質=" + String(sensor) + " !";
    Serial.println(message);

    if (client.connect(host, 443)) {
      int LEN = message.length();
      //傳遞POST表頭
      String url = "/api/notify";
      client.println("POST " + url + " HTTP/1.1");
      client.print("Host: "); client.println(host);
      //權杖
      client.print("Authorization: Bearer "); client.println(Linetoken);
      client.println("Content-Type: application/x-www-form-urlencoded");
      client.print("Content-Length: "); client.println( String((LEN + 8)) );
      client.println();      
      client.print("message="); client.println(message);
      client.println();
      //等候回應
      delay(2000);
      String response = client.readString();
      //顯示傳遞結果
      Serial.println(response);
      client.stop(); //斷線,否則只能傳5次
    }
    else {
      //傳送失敗
      Serial.println("connected fail");
    }
  }
  //每5秒讀取一次
  delay(5000);
}

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...