3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

M5Stack/M5StickcからのLINE Notify通知を少し改良してみた

Last updated at Posted at 2020-08-22

#M5Stack/M5StickcからのLINE Notify通知を少し改良してみた

##LINE Notify通知をはじめた理由
酷暑のテレワーク、クーラー必須ですが部屋を閉め切ると宅配のピンポンに気がつけませんね。再配達を減らすためにもインターホンの音をM5Stickcのマイクで拾い"宅配便がキタ!"とLINE Notify通知を行うようにしたところ、通知に失敗することがあったので改良してみました。

##改良前
LINE Notify通知するコードは@From_FさんのESP32とLINE Notifyを使ってナースコール的なのを作る を参考にさせていただきました。ありがとうございます。

改良前
void send(String message) {
  const char* host = "notify-api.line.me";
  const char* token = "tokentokentoken";
  Serial.println("Connect");
  //Connect to LINE notify-api
  if (!client.connect(host, 443)) {
    Serial.println("Connection failed");
    return;
  }
  Serial.println("Connected");
  //リクエストを送信
  String query = String("message=") + message;
  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);

  //受信終了まで待つ 
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
    if (line == "\r") {
      break;
    }
  }
  String line = client.readStringUntil('\n');
  Serial.println(line);
}

これで通知は成功するのだけど、lineに返ってくるのは・・・
シリアルモニタ
22:27:48.888 -> line 1=HTTP/1.1 200
22:27:48.888 -> line 1=Server: nginx
22:27:48.888 -> line 1=Date: Fri, 21 Aug 2020 13:27:49 GMT
22:27:48.888 -> line 1=Content-Type: application/json;charset=UTF-8
22:27:48.888 -> line 1=Transfer-Encoding: chunked
22:27:48.921 -> line 1=Connection: keep-alive
22:27:48.921 -> line 1=Keep-Alive: timeout=3
22:27:48.921 -> line 1=X-RateLimit-Limit: 1000
22:27:48.921 -> line 1=X-RateLimit-ImageLimit: 50
22:27:48.921 -> line 1=X-RateLimit-Remaining: 999
22:27:48.921 -> line 1=X-RateLimit-ImageRemaining: 50
22:27:48.921 -> line 1=X-RateLimit-Reset: 1598020069
22:27:48.921 -> line 1=
22:27:48.921 -> line 2=1d

通知に失敗するときは・・・
シリアルモニタ
22:27:48.921 -> line 1={"status":200,"message":"ok"}
22:27:48.921 -> line 2=

とまるでレスポンスが分割し受信しきれていないように思えました。

##改良後
そこでreadStringUntilreadStringに書き換えたところ・・・

改良例
  //受信終了まで待つ 
  while (client.connected()) {
    String line = client.readString();
    Serial.println(line);
    if (line == "\r") {
      break;
    }
  }
  String line = client.readString();
  Serial.println(line);

シリアルモニタ
22:35:17.309 -> line 1=HTTP/1.1 200
22:35:17.309 -> Server: nginx
22:35:17.309 -> Date: Fri, 21 Aug 2020 13:35:17 GMT
22:35:17.344 -> Content-Type: application/json;charset=UTF-8
22:35:17.344 -> Transfer-Encoding: chunked
22:35:17.344 -> Connection: keep-alive
22:35:17.344 -> Keep-Alive: timeout=3
22:35:17.344 -> X-RateLimit-Limit: 1000
22:35:17.344 -> X-RateLimit-ImageLimit: 50
22:35:17.344 -> X-RateLimit-Remaining: 998
22:35:17.344 -> X-RateLimit-ImageRemaining: 50
22:35:17.344 -> X-RateLimit-Reset: 1598020070
22:35:17.344 ->
22:35:17.344 -> 1d
22:35:17.344 -> {"status":200,"message":"ok"}

レスポンスの受信に1回で成功したようでした。ループ終了後のreadStringは不要かもしれませんね。
readStringはタイムアウトまで受信してくれるから'\n'を待たなくてよいと思うのだけど、他の方はどうしているのだろう?

3
6
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
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?