LoginSignup
9
9

More than 3 years have passed since last update.

M5Stack で LINE通知

Posted at

技術書典6で購入したプリン本を参考にいろいろと試してみる

通知

通知の機能としてLINEへ送信させてみる。

LINE-BOT トークン作成

トークルームを選択するひつようがあるみたいなので、とりあえず1:1用のルームにする

WiFi接続

LINEに送信するために通信がひつようなので、WiFiでの接続確認

が、つながらない。。

Aはダメなのかな。
Gに変更したら接続できたような感じがする。
でも実際に接続できているのかどうかはよくわからない。。

Notify

LINEに対してNotify。
処理は正常に流れていそうなのだが、LINE側に通知が来ない。
WiFiの接続が上手くいっていないのか、それともLINEの方がおかしいのか。

トークンを別のものに変えてみてリトライ。

やはりだめ。
WiFiを変えてみる。
WiFiMulti.hを使った処理に変更
参考
https://www.mgo-tec.com/blog-entry-trouble-shooting-esp32-wroom.html/2

void wifi_connect() {  
  const char* ssid = "<SSID 2.5GHzのやつ>";
  const char* pass = "pass";
  M5.Lcd.println(ssid);

  WiFi.disconnect(true);
  M5.Lcd.printf(".");
  delay(1000);
  M5.Lcd.printf(".");
  WiFiMulti wifiMulti;
  wifiMulti.addAP(ssid, pass);

  while (wifiMulti.run() != WL_CONNECTED) {
    delay(500);
    M5.Lcd.printf(".");
  }
  M5.Lcd.println("wifi connect ok");
}

こんな感じの処理になった。

ログも追加させたことで接続できてそうかの確認はわかりやすくなった。
ということで、やっぱりWiFiは大丈夫そうな気がする。
※接続できているかどうかを簡単に確認できればいいんだけどな。

あとはAPIからのレスポンスを見てみないとようわからん。

APIを直接実行してみる

curlで直接APIをたたいて、トークンが思ったように作成できているのかどうかを確認してみる。
https://notify-bot.line.me/doc/ja/

LINEに対してトークンで通知できるかを試す。

https://notify-api.line.me/api/status
Authorization:Bearer <token>

トークンが有効かを確認
200が返ってくるので大丈夫。

curl -X POST -H 'Authorization: Bearer <token>' -d 'message=foobar' \
https://notify-api.line.me/api/notify

通知もされる。
ということで、トークンはちゃんとできていることが確認できた。

この呼び方を[WiFiClientSecure client]に食わせてあげているのであればいいはず。
※要するにここの食わせ方が間違っているってことなんだろうな。

公式のドキュメントがどれなのかよくわからないので、適当にググって調査。
以下を参考にしてみる。
https://dotstud.io/docs/nefrybt-network-http-post/

curl -d '{"color":"green","message":"test","notify":false,"message_format":"text"}' -H 'Content-Type: application/json' https://xxxxxx.hipchat.com/v2/room/4651875/notification?auth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

これが、

String url = "/v2/room/4651875/notification?auth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  url += "&color=green&message=test&notify=false&message_format=text";

  Serial.print("connecting to ");
  Serial.println(HOST);

  if (!client.connect(HOST, HTTP_PORT)) {
    Serial.println("connection failed");
    return;
  }

  client.println("POST " + url + " HTTP/1.1");
  client.println("Content-Type: application/json");
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.println("Host: " + String(HOST));
  client.println("Connection: close");

になるみたいなので

curl -X POST -H 'Authorization: Bearer <token>' -d 'message=foobar' \
https://notify-api.line.me/api/notify

は以下のようになるはず

String url = "/api/notify";
url += "&message=sample";
client.println("POST " + url + " HTTP/1.1");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Host: notify-api.line.me");
client.println("");

client.println("Connection: close");

client.println() で順番に食わせてあげればいいってだけなのかな。

が、400エラー。
[messageがないぞ]って怒られる。

ハマりすぎてよくわからなくなったので、別アプローチ。

HTTPClient http;
http.begin("https://notify-api.line.me/api/notify?message=" + message);
http.addHeader("Authorization", "Bearer <トークン>");
int httpResponseCode = http.POST("POSTING from M5stack");

これで試してみたところ通知が送信されることは確認できた。
ただ、URLエンコードしてないと日本語は送信できない。
C++?でURLエンコードってどうやるんだろう?

ただ、とりあえず送信はできそうなので、次に行ってしまうことにする

というところでいったん終了。

次は音声を出力させたい。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9