LoginSignup
8

More than 3 years have passed since last update.

プリンを守る技術 ~その1:LINEへの通知~

Last updated at Posted at 2018-11-30

概要:

 せっかく取っておいたプリンを家族に食べられてしまった。そんな経験はありませんか?
 私が所属するaNo研は、冷蔵庫のプリン見守りデバイス「プリン・ア・ラート2」を発表しました。
 この記事は、「プリン・ア・ラート2」に使われている技術を紹介します。

紹介:

システム構成

purin_1.png
プリン・ア・ラート2は、プリンの重さを検知するセンサー、そして、小型のマイコンM5Stackで構成されています。
プリンの検知は、小皿の下の重さセンサーで行われます。プリンが取られると小皿が軽くなり、これをきっかけに、Lineへのアラートを発します。
クリップボード01.png
プリン・ア・ラートでは、Lineへの通知に、「Line Notify」を使用しています。「Line」が提供するサービスで、使用するとLINEにメッセージを送って、通知をLINEで受け取ったりすることが出来ます。

Line Notifyのサービス登録

Line NotifyをM5stackで使うためには、Line Nofityのホームページで、あなたのLineとのアクセストークンを発行します。

Line Nofity:https://notify-bot.line.me/

クリップボード04.png
Line Nofityのホームページから、「アクセストークンの発行(開発者向け)」を押します。

クリップボード06.png
そして、必要事項を記入の上、発行されたアクセストークンをコピーします。
これで、Line NotifyをM5stackで使うための準備が完了です。

実装例

以下に、M5StackでLine Notifyを発行するためのソースコードを示します。
your_tokenには、Line Notifyのページで取得した、アクセストークンを記載してください。
今回の事例であれば、センサーと接続されているポート(No36)で、センサーから、High→Lowと反応があったタイミングでLineへの通知が行われます。


#include <ssl_client.h>
#include <WiFiClientSecure.h>
#include <M5Stack.h>
const char* ssid = "wifi_ssid";
const char* password = "pass";
int switch_port=36;
void wifi_connect() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)  delay(500);
}
void setup() {
  M5.begin();
  wifi_connect();
}
void loop() {
  M5.update();
  int buttonState = digitalRead(switch_port);
  static int buttonState_old=buttonState;
  if ( (buttonState == 1) && ( (buttonState_old == 0)))  send_line_alert();
  buttonState_old=buttonState;
}
void send_line_alert() {
  const char* host = "notify-api.line.me";
  const char* token = "your_token";
  const char* message1 = "%0D%0A %0D%0Aプリンが取られました!!!%0D%0A";
  WiFiClientSecure client;
  if (!client.connect(host, 443)) {
    return;
  }
  String query = "message=" + String(message1);
  String request = "POST /api/notify HTTP/1.1\r\nHost: " + host + "\r\nAuthorization: Bearer" + token + "\r\nContent-Length: " + String(query.length()) +  "\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n" +query + "\r\n";
  client.print(request);
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r")   break;
  }
}

参考文献

にゃんてっく☆やせないアイツとESP8266―おデブなアイツのダイエットのはじまり

「プリン・ア・ラート」記事

aNo研公式ホームページ
プリンを勝手に取ると「返してね」と警告 冷蔵庫を見守る警報器「プリン・ア・ラート」がかわいい
冷蔵庫のプリンを食べる犯人に警告を発して反省までさせるデバイス「プリン・ア・ラート2」

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
What you can do with signing up
8