LINE Notifyを使ってESP32やRaspberryPiからスマホのLINEにメッセージを送る(その2)
LINE Notify初体験!!さて、送れるんでしょうか?💪
ESP32でのwebクライアントのプログラムはハードルが高いので、まずはRaspberryPiを使ってpythonで書いてみました。このプログラムは、メッセージ、スタンプ、画像と一通り送っています。
import requests
import pprint
def main():
send_line_notify('Hello')
def send_line_notify(notification_message):
line_notify_token = 'your line token'
line_notify_api = 'https://notify-api.line.me/api/notify'
headers = {'Authorization': f'Bearer {line_notify_token}'}
files = {'imageFile': open("miku.jpg", "rb")}
data = {'message': notification_message,'stickerId': 11, 'stickerPackageId': 1}
response = requests.post(line_notify_api, headers = headers, data = data,files=files)
pprint.pprint(response.json())
if __name__ == "__main__":
main()
メッセージの送信はrequests.post()を使います。必要な引数はLINEが指定するメッセージを送信するURL、ヘッダー、送信するメッセージデータは辞書型のデータでメッセージ、スタンプNoを、画像ファイルをアップロードする時はfilesを使います。
response = requests.post(line_notify_api, headers = headers,\
data = data,files=files)
次にEPS32のプログラムです。ちなみに画像の送信はしてません。ちょっと難しくて理解不能でした。(これは後々)
ESP32のボードに実装されているスイッチを押すとLINEにメッセージとスタンプを送るようにしてあります。
ライブラリーはWiFiClientSecureを使います。これは『https:/』にアクセスするときに使用するライブラリーのようです。
#include <WiFi.h>
#include <WiFiClientSecure.h>
const char ssid[] = "your ssid"; // SSID
const char pass[] = "your password"; // password
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(0, INPUT_PULLUP); // Initialize the BUILTIN_LED pin as an output
//wifi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while( WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.printf(".");
}
Serial.println("\nwifi connect ok");
}
int lineSend(String message,int SI=0,int PSI=0) {
const char* host = "notify-api.line.me";
const char* token = "your line token";
WiFiClientSecure client;
Serial.println("Try");
//LineのAPIサーバに接続
if (!client.connect(host, 443)) {//httpsはポート443
Serial.println("Connection failed");
return(0);
}
Serial.println("Connected");
String query = "message=" + message;
//リクエストを送信
if(SI >0){
query = query + "&stickerId="+SI+"&stickerPackageId="+PSI;
}
Serial.println(query);
String request =
String("") +
"POST /api/notify HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + token + "\r\n" +
"Content-Length: " + String(query.length()) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n\r\n" +
query + "\r\n";
client.print(request);
String res = client.readString();
Serial.println(res);
client.stop();
return(1);
}
void loop() {
int count=0;
while(1){
// put your main code here, to run repeatedly:
int a=digitalRead(0);
if(a==0) {
count++;
if(count > 250){count =250;}
}else{
count=0;
}
if(count ==2){
lineSend("Hello ESP32",11,1);
}
delay(100);
}
}
アクセスは下記で行いますが、https://は省略できるようです。ポートは443になります。これはhttps://の接続ポートが443なのでこれ以外を設定すると繋がりません。
client.connect(host, 443)
これが送信するデータです。pythonのプログラムと比べると送信するデータが少し多いですね。恐らくpythonのrequests.postが自動的に判断して送ってくれているものがたくさんあるようです。ちなみにPOST、Host、Authorizationは送信順序を変えると正常に送信できません。
String request =
String("") +
"POST /api/notify HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + token + "\r\n" +
"Content-Length: " + String(query.length()) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n\r\n" +
query + "\r\n";
送信するメッセージやスタンプ🆔はクエリー文字列、(URLパラメーター)として送信するので下記のように各コマンド間を『&』で繋いでいきます。
query = query + "&stickerId="+SI+"&stickerPackageId="+PSI;
あと、LINEスタンプですが、ドキメントで紹介されている🆔では送信できないので下記の🆔で送信します。これで2時間くらいハマった。💦
一応、ESP32からLINEにメッセージを送ることができました。💯
しかし、ESP32でのクライアントプログラムって難しいなってのが感想です。この手の処理はやっぱりRaspberryPiを使った方が簡単ですよね。😅
今日、家に帰ったら、1ヶ月前に注文していたESP32CAMが届いていたので、次回じゃないかもしれないけど、近いうち撮影画像をLINEに送るプログラムを作ってみたいと思います。😃
では🤚
この記事が気に入ったらサポートをしてみませんか?