編集履歴一覧に戻る
inoue2002のアイコン画像

inoue2002 が 2021年05月16日17時16分00秒 に編集

コメント無し

記事種類の変更

+

製作品

本文の変更

# 背景 僕はつい意味もなく冷蔵庫を開けてしまう癖があります😓ものの数十分で冷蔵庫の中に新しいものが湧くわけでもないのに。でもなんだか開けてしまうんですよね(笑) でも、ちまちま不必要に冷蔵庫を開けてしまうと、電気代のことや環境のことなどを考えると良くありません。また、5人家族の我が家では1日にどれぐらい冷蔵庫が開け閉めされ、また開いている時間は合計でどれぐらいになるのか興味を持ちました。 冷蔵庫の開け閉めの回数と開いている時間が可視化できれば、僕の冷蔵庫を不必要に開けてしまう癖も少しはおさまるかもしれないと思って開発することにしました。 電子工作も全然やったことないし、今回提供してくださったobnizとたまたま家に転がっていた距離センサーをくっつけてみたら意外とうまく値が取得できたのでこれの組み合わせを使うことにしました。(ちなみにブレットボードを今回の作品で初めて触りました。)

-

作っているうちに自分の得意分野で拡張していったら、最終的にはLINEBotが完成し、冷蔵庫の情報と一緒に我が家に不足しているものを登録できる機能も開発しました。詳細については後ます。

+

作っているうちに自分の得意分野で拡張していったら、最終的にはLINEBotが完成し、冷蔵庫の情報と一緒に我が家に不足しているものを登録できる機能も開発しました。詳細については後ほど詳く解説します。

# このプロダクトのポイント ・2021/5/13についにパブリックベータ版になったnotionAPIをフル活用している ・距離センサーを用いており、冷蔵庫だけでなく汎用性が高い ・フロントエンド・サーバーサイド・電子工作に触れることができる ・冷蔵庫の開き時間が少なくなる # センサー ![センサー](https://camo.elchika.com/fb940c36e04c22b9ed5b7ee481412742c631b8c9/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f62613339333662652d333566612d343562372d623638332d6537393063616562613333342f34363864323333342d633835632d346662642d383161362d623231313136373531393436/) # DEMO @[youtube](https://www.youtube.com/embed/k0QoRyt18fw) # システム構成図 ![構成図](https://camo.elchika.com/8107ea668d12712c2f54a5e2f6071a2ad23fd1dd/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f62613339333662652d333566612d343562372d623638332d6537393063616562613333342f37373866613633392d363061332d343831342d393436652d336364393435343537323865/) # 部品 | 品名 | 用途 | 価格 | |:---:|:---| :--- | | [obniz Board 1Y](https://obniz.io/ja/products#deviceComparison) | メインコンピュータ | 6930 | | [ブレッドボードx2 ](https://akizukidenshi.com/catalog/g/gP-05294/) | センサーの回路 | 200x2 | | [ジャンプワイヤ](https://www.amazon.co.jp/dp/B06Y48V9DL/ref=cm_sw_em_r_mt_dp_VYC77WFJTDEBWVA2X47F?_encoding=UTF8&psc=1) | センサーの回路 |990 | | モバイルバッテリー(給電できる場合はいらない) | センサーの回路 | 3000弱 | # ソースコード obniz自体を動かしているnodejsのソースコードは以下です。 ```obniz:index.js "use strict"; require("dotenv").config(); const Obniz = require("obniz"); const axios = require("axios"); const obniz = new Obniz(process.env.OBNIZ_ID); obniz.onconnect = async () => { const hcsr04 = obniz.wired("HC-SR04", { gnd: 0, echo: 1, trigger: 2, vcc: 3, }); let isOpen = false; let startTime; let totalTime; while (true) { let avg = 0; let count = 0; for (let i = 0; i < 3; i++) { // measure three time. and calculate average const val = await hcsr04.measureWait(); if (val) { count++; avg += val; } } if (count > 1) { avg /= count; } console.log(avg, isOpen); if (avg > 100 && isOpen === false) { isOpen = true; startTime = new Date().getTime(); console.log("冷蔵庫があきました!"); } if (avg < 100 && isOpen === true) { //しまった時 isOpen = false; console.log(new Date().getTime()); console.log(startTime); totalTime = new Date().getTime() - startTime; totalTime = totalTime / 1000; const hour = Math.floor(totalTime / 3600); const hour_wari = Math.floor(totalTime % 3600); const min = Math.floor(hour_wari / 60); const min_wari = Math.floor(hour_wari % 60); const sec = min_wari; console.log(`${hour}時間${min}分${sec}秒開いていた`); //totalTimeをAPIに送る axios.post(process.env.API_URL + "/register", { totalTime: Math.floor(totalTime), }); } await obniz.wait(100); } }; ``` [ソースコードの参考にした記事](https://protoout.studio/posts/obniz-nodejs-hcsr04) ハード以外のAPIやLINEBotのソースコードなどはこちらの[リポジトリ](https://github.com/inoue2002/refrigeratormonitoring-obniz)に公開されています # 開発過程 ・NotionAPIの学習 ・ハード開発 ・API開発 ・LINEBot開発 ・設置・検証 ・サーバーに移行し永続化 の順に工夫したポイント・難しいところをまとめていきます ### NotionAPIの学習 NotionAPIは3日ほど前に出たばかりで、全くと言っていいほど知見も記事もありませんでした。ただひたすらに公式の英語ドキュメントを読んで理解しました。最低限の書き出しや特殊な概念(Notion特有の概念)を理解してAPIで操作し、現時点でできることできないことを把握するのに結構時間を取られました。 APIの習得する過程は[こちら](https://scrapbox.io/inouenote/notionAPI%E3%82%92%E3%81%84%E3%81%98%E3%81%84%E3%81%98)の記事にてまとめてあります。 ここではこのプロダクトに利用できた知見のみ共有します。 まず、NotionAPIはJavaScriptのSDKが公開されています。[こちら](https://github.com/makenotion/notion-sdk-js)を使うことで簡単にAPIを利用することができます。 今回使ったAPIはDBから値を取得するものと値を更新するものです。とりあえずこれが使えたらDBとして使うことができるので何かものが作れるかと思います。 nodejsでnpmモジュールを用いてSDKを利用するには以下のように呼び出します ```api:index.js const { Client } = require("@notionhq/client"); // Initializing a client const notion = new Client({ auth: process.env.NOTION_TOKEN, }); ``` データを取得したい場合は以下のようにリクエストを送ります。今回はクエリのAPIを利用していますが、全項目(回数と時間)を取得しているのでクエリ式を入れておりません。 ```api:index.js //今のセンサーのあたいを取得しにいく const request_payload = { path: "databases/" + "データベースのID(databaseId)" + "/query", method: "POST", }; const current_pages = await notion.request(request_payload); console.log(current_pages.results) ``` データを書き込みたい時は以下のようにリクエストを送ります。 ```api/:index.js const = todayCount = { id : "データベースの項目のID(pageId)", count : 2 //更新したい値を入力する } const request_payload = { path: "pages/" + todayCount.id, method: "patch", body: { parent: { database_id: "データベースのID(databaseId)", }, properties: { 冷蔵庫: { title: { text: { content: "本日開いた回数", }, }, ], }, count: { number: todayCount.count, }, id: { rich_text: [ { text: { content: todayCount.id, }, }, ], }, }, }, }; await notion.request(request_payload); ``` このような記述で値の更新を行います。NotionAPIは他にもアクセスしているユーザーの情報を取得したり、項目の新規追加(更新では無い)のようなことも、クエリやソートも柔軟にできるようです。しっかり使いこなすことでもっといろいろな機能を実装することができます。ただ、まだ対応していないブロックなどもあり、パブリックベータ(記事執筆時点)であることを忘れずに使う必要があると感じました。 ### ハードウェアの開発 初めてobnizに電源を入れ、距離センサーなどを繋いだ時のまとめは[こちら](https://scrapbox.io/inouenote/obniz%E3%80%80%E9%9B%BB%E5%AD%90%E5%B7%A5%E4%BD%9C%E5%85%A5%E9%96%80)に書かれています。こちらを参考にし、上記で書いたobnizと距離センサーを接続し、電源を入れインターネットに繋ぐセットアップを行いました。初めはソースコードの開発のところに記したサンプルコードを起動すると簡単に動いたので、特に開発面で難しいことはないと思います。 ①obnizとジャンプワイヤーとブレッドボードと距離センサーを準備 ②公式ドキュメントを参考につなげる ![キャプションを入力できます](https://camo.elchika.com/fcadc938b86918d24469508f888ff3f78becb6e8/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f62613339333662652d333566612d343562372d623638332d6537393063616562613333342f61646133616536302d653536632d346131302d386165352d303837663237626333623763/) ③ソースコードを起動 ④動いた @[twitter](https://twitter.com/inoue2002/status/1387318838027382790?s=20) ### API開発 obnizから取得し、更新された値をNotionのデータベースに書き込みに行くAPIです。obnizから直接書き込みに行っても良かったのですが、他の役割を持つAPI(日が変わるとデータを初期化する)も実装しないといけないので作成しました。構成図に書いた通り、AWSのLambdaくを用いて開発を行いました。ソースコードはこちらです。 ```api:index.js //Notionに記録するAPI //Lambda関数 const { Client } = require("@notionhq/client"); const axios = require("axios"); const querystring = require("querystring"); // Initializing a client const notion = new Client({ auth: process.env.NOTION_TOKEN, }); exports.handler = async (e) => { console.log(e.path); console.log(e.body); const event = e.body; let message = ""; switch (e.path) { case "/register": message = "ok"; await registerFunc(JSON.parse(event)); break; case "/cron": await cronFunc(); break; } const response = { statusCode: 200, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", }, body: JSON.stringify(message), }; return response; }; async function registerFunc(event) { const request_payload = { path: "databases/" + "fb9bd269-8888-4fa1-a07a-83817ce9ffb9" + "/query", method: "POST", }; const current_pages = await notion.request(request_payload); let todayCount = {}; let todayTime = {}; for (const page of current_pages.results) { if (page.id === "07ce4234-d2d3-43be-ba8d-18be7bec2b58") { todayCount = { id: page.id, count: page.properties.count.number }; } if (page.id === "9217cbb3-0a20-4fc0-9778-31d0002f200c") { todayTime = { id: page.id, count: page.properties.count.number }; } } todayCount.count = todayCount.count + 1; todayTime.count = todayTime.count + event.totalTime; await notionUpdateCount(todayCount, todayTime); } async function notionUpdateCount(todayCount, todayTime) { const request_payload = { path: "pages/" + todayCount.id, method: "patch", body: { parent: { database_id: "fb9bd269-8888-4fa1-a07a-83817ce9ffb9fb9bd269-8888-4fa1-a07a-83817ce9ffb9", }, properties: { 冷蔵庫: { title: [ { text: { content: "本日開いた回数", }, }, ], }, count: { number: todayCount.count, }, id: { rich_text: [ { text: { content: todayCount.id, }, }, ], }, }, }, }; await notion.request(request_payload); const request_payload2 = { path: "pages/" + todayTime.id, method: "patch", body: { parent: { database_id: "fb9bd269-8888-4fa1-a07a-83817ce9ffb9fb9bd269-8888-4fa1-a07a-83817ce9ffb9", }, properties: { 冷蔵庫: { title: [ { text: { content: "本日開いた時間", }, }, ], }, count: { number: todayTime.count, }, id: { rich_text: [ { text: { content: todayTime.id, }, }, ], }, }, }, }; await notion.request(request_payload2); } async function cronFunc() { //今のセンサーのあたいを取得しにいく const request_payload = { path: "databases/" + "fb9bd269-8888-4fa1-a07a-83817ce9ffb9" + "/query", method: "POST", }; const current_pages = await notion.request(request_payload); let todayCount = {}; let todayTime = {}; for (const page of current_pages.results) { if (page.id === "07ce4234-d2d3-43be-ba8d-18be7bec2b58") { todayCount = { id: page.id, count: page.properties.count.number }; } if (page.id === "9217cbb3-0a20-4fc0-9778-31d0002f200c") { todayTime = { id: page.id, count: page.properties.count.number }; } } //今家で必要なものを取得しにいく const request_payload2 = { path: "databases/" + "c98d166221914e1a92f4e7a90761c0da" + "/query", method: "POST", body: { filter: { property: `want`, checkbox: { equals: true, }, }, }, }; const current_pages2 = await notion.request(request_payload2); let wantItems = "\n【現在この家に必要なものリスト】"; for (const item of current_pages2.results) { wantItems = wantItems + "\n" + "・" + item.properties.item.title[0].plain_text; } await axios.post( "https://notify-api.line.me/api/notify", querystring.stringify( { message: `本日は冷蔵庫が!${todayCount.count}回開けられ、開いていた時間は${todayTime.count}秒でした。明日はもっと短くなるように心がけましょう。`, }, ), { headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: `Bearer ${process.env.NOTIFY_TOKEN}`, }, } ); await axios.post( "https://notify-api.line.me/api/notify", querystring.stringify( { message: `${wantItems}\n編集:https://lin.ee/PgZiEkV`, }, ), { headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: `Bearer ${process.env.NOTIFY_TOKEN}`, }, } ); todayCount.count = 0; todayTime.count = 0; await notionUpdateCount(todayCount, todayTime); //LINEに投げる } ``` 日が変わるとcronを回してデータの初期化LINENotifyを使って報告を行うようにしました。 ![キャプションを入力できます](https://camo.elchika.com/8b4f61d6363b9afbbcc05cb1092f7cacf92ccd98/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f62613339333662652d333566612d343562372d623638332d6537393063616562613333342f38356166323739342d663731342d343632332d396534342d363334356566353563626434/) cronnの設定は調べると沢山出てきますが、毎日24じになったら起動するというルールは以下のように定義することでいけます。 今回はGMT基準で動かしているので時差計算をして15じになったら起動するようにしています。 ![キャプションを入力できます](https://camo.elchika.com/d7eee658af596e1a8aef5f2b10801c71eabbc0e1/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f62613339333662652d333566612d343562372d623638332d6537393063616562613333342f32393536613335302d613834362d346465642d626564622d656534343865393337393935/) ### LINEBot開発 LINEBotを"作る"過程は以前詳しく記事を書いて言及しておりますのでこちら「[LINEBotをみんなで作ろう〜環境構築編〜](https://qiita.com/inoue2002/items/a87df2b520f8b6e37f42)」をご覧ください。 LINEBotを作った理由は、Notionのデータベースをもっと活用したかったからです。毎日の冷蔵庫の結果報告と同時に、家族が今家に欲しいものを登録していると、通知してくれるシステムがあるといいなと思いました。例えば「卵がなくなったし明日買いに行かないと!」とか「ティッシュがもうすぐなくなるわー」とか。家族の誰かが必要としているけれど誰かがまとめて買いに行けばいい。みたいなニーズ。買い物に行ったけど、そんなの買ってこないといけないなんて知らなかった。みたいなことを解決したいなと思いました。 @[youtube](https://www.youtube.com/watch?v=7Metw4cl-O4) ```linebot:index.js //LINEBot //Lambda関数 "use strict"; // モジュール呼び出し const crypto = require("crypto"); const line = require("@line/bot-sdk"); const { Client } = require("@notionhq/client"); const notion = new Client({ auth: process.env.NOTION_TOKEN, }); // インスタンス生成 const lineClient = new line.Client({ channelAccessToken: process.env.ACCESSTOKEN, }); exports.handler = (event) => { let signature = crypto .createHmac("sha256", process.env.CHANNELSECRET) .update(event.body) .digest("base64"); let checkHeader = (event.headers || {})["X-Line-Signature"]; if (!checkHeader) { checkHeader = (event.headers || {})["x-line-signature"]; } const body = JSON.parse(event.body); const events = body.events; console.log(events); // 署名検証が成功した場合 if (signature === checkHeader) { events.forEach(async (event) => { let message; switch (event.type) { case "message": message = await messageFunc(event); break; case "postback": message = await postbackFunc(event); break; case "follow": message = { type: "text", text: "追加ありがとうございます!" }; break; } // メッセージを返信 if (message != undefined) { await sendFunc(body.events[0].replyToken, message); // .then(console.log) // .catch(console.log); return; } }); } // 署名検証に失敗した場合 else { console.log("署名認証エラー"); } }; async function sendFunc(replyToken, mes) { const result = new Promise(function (resolve, reject) { lineClient.replyMessage(replyToken, mes).then((response) => { resolve("送信完了"); }); }); return result; } async function messageFunc(event) { let message = ""; message = { type: "text", text: `メッセージイベント` }; let headerMes = event.message.text.split("/"); if (event.message.text === "買いました報告") { //現在wantになっている商品をカルーセルで一覧にする const request_payload = { path: "databases/" + "データベースID" + "/query", method: "POST", body: { filter: { property: `want`, checkbox: { equals: true, }, }, }, }; const current_pages = await notion.request(request_payload); const wantItemsArry = []; for (const item of current_pages.results) { wantItemsArry.push({ type: "bubble", direction: "ltr", header: { type: "box", layout: "vertical", contents: [ { type: "text", text: `${item.properties.item.title[0].plain_text}`, weight: "bold", size: "xxl", color: "#626C62FF", align: "center", wrap: true, contents: [], }, { type: "separator", }, ], }, footer: { type: "box", layout: "horizontal", contents: [ { type: "button", action: { type: "postback", label: "購入した", data: `${item.properties.item.title[0].plain_text}/${item.id}`, }, style: "primary", }, ], }, }); } message = { type: "flex", altText: "現在買って欲しいリストです", contents: { type: "carousel", contents: wantItemsArry, }, }; //ポストバックに 項目名/id } else if (event.message.text === "欲しいです報告") { } else if (headerMes.length === 2) { if (headerMes[0] === "追加") message = { type: "text", text: `${headerMes[1]}を追加しました` }; // notionに追加しにいく await notion.request({ path: "pages", method: "POST", body: { parent: { database_id: "データベースID" }, properties: { item: { title: [ { text: { content: headerMes[1], }, }, ], }, want: { checkbox: true, }, }, }, }); } else { message = { type: "text", text: "「買いました報告」か「欲しいです報告」のどちらかを送ってください。", }; } return message; } const postbackFunc = async function (event) { let message = ""; message = { type: "text", text: "ポストバックイベント" }; const headerData = event.postback.data.split("/"); message = { type: "text", text: `${headerData[0]}の購入を保存しました!` }; const request_payload = { path: "pages/" + headerData[1], method: "patch", body: { parent: { database_id: "データベースID" }, properties: { item: { title: [ { text: { content: headerData[0], }, }, ], }, want: { checkbox: false, }, }, }, }; await notion.request(request_payload); return message; }; ``` LINEBotは作成したものの、アイテムを追加する時に`追加/{{欲しいもの}}`と入力しなければいけず、ユーザーにとって不便だなと思ったのでLIFFを使って簡単に登録できるようにしました。LIFFというは上記DEMO動画のLINEの画面で表示される下からうにゅっと出てくるフロント画面です。LINE Front-end Frameworkていって、自分の作ったサイトとかをLINE上でめっちゃいい感じにしてくれるやつです。今回はフロントエンドをnuxtで作成し、netlifyにデプロイしてあります。入力した値を取得し、登録ボタンが押したらトーク上に「追加/{{入力された値}}」を変わりに送信して画面を閉じてくれます。UX爆上がりですね😘 vueファイルのコードにはなりますが参考までに使ってみてください。 ```:index.vue <template> <div> <v-row justify="center" style="margin-top: 50px"> <div class="mt-6" align="center"> <h3>欲しいものをリスト追加する</h3> <v-text-field label="商品名" v-model="item" style="margin-top: 40px" solo > </v-text-field> <v-btn color="success" elevation="3" @click="register" large >追加</v-btn > </div> </v-row> </div> </template> <script> export default { data: () => ({ item: "", }), async mounted() { liff .init({ liffId: "1655989367-pax2zN0P", }) .then(() => { this.isLoggedIn = liff.isLoggedIn(); if (!liff.isInClient() && !liff.isLoggedIn()) { liff.login(); } }); }, methods: { register() { liff .sendMessages([ { type: "text", text: `追加/${this.item}`, }, ]) .then(() => { console.log("message sent"); liff.closeWindow(); }) .catch((err) => { console.log("error", err); }); }, }, }; </script> ``` ### 設置・検証 ![キャプションを入力できます](https://camo.elchika.com/2efb6c896289383223bd8e3341c05d727a32fabf/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f62613339333662652d333566612d343562372d623638332d6537393063616562613333342f39313632633235322d393630392d346631392d393536352d633437353335633666646564/) 実際に冷蔵庫に設置して検証してみました。 @[youtube](https://youtu.be/Ut0TgOBggWg) 上記の検証でいい感じの計測ができています。検証成功です! ### サーバーに移行し永続化 僕のパソコンでずっとスクリプトを動かしておくわけにはいかないので、常時起動できるサーバーを探しました。個人てきに以前から興味があったAWS Lightsailを使ってみました。月額400円ぐらいで簡単なサーバーを固定金額で借りられるのはいいですね。(それほどいいサーバーではないが) ![キャプションを入力できます](https://camo.elchika.com/bce9913e24804a8d98b9dff1e12efede2f9fa748/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f62613339333662652d333566612d343562372d623638332d6537393063616562613333342f31636264643963382d376337662d343431662d613935322d653463656466313738353264/) ずっとコードを動かし続けることに関しての知見はあまり少ないのですが、すこい調べると`foreever`コマンドというものを利用するとずっと動かすことができるそうなのでインストールしてやってみました。すると無事常時起動で動かすことができました✨ `foreever`コマンドの利用の仕方は[こちら](https://qiita.com/naga3/items/2b069bbf12087ba75bac)の記事を参考にし、`$ forever -w indecx.js`で起動しました。止める時は`forever stop index.js`で良いそうです!この辺りはまだよくわかっていないのでこれから勉強していきます。 # 最後に とっても楽しかったです。 @[twitter](https://twitter.com/inoue2002/status/1393480311313371138?s=20)咄嗟に書いたアーキテクチャの反応が多くて嬉しかった! 結構時間なくて締め切り前日に徹夜ハッカソンして開発したのはいい思い出です!