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

kou0179 が 2021年04月16日02時35分22秒 に編集

微修正

記事種類の変更

+

製作品

本文の変更

# 経緯 洗面台の下にタンクがある場合があり、基本的には溢れる事はありませんが、 水溢れ防止穴から大量の水が流れ込んだ場合や、洗面台シャワーの不具合で溢れる事があります。 先日溢れて散々な思いをしたので事前に防止したいと思います。 # デモ動画 @[youtube](https://www.youtube.com/watch?v=jshhUSFNJ_0) ※撮影の都合上、実タンクでの映像ではありませんが機能を満足に確認できる動画です。

-

# 要件/仕様

+

# 環境/要件

## 動作環境 - nodejs v14.15.1

-

-

-

##

+

## 要件

- 起動時にブザーがなり、LINE Notifyに通知が飛ばす - 水位が一定以上になった時にLINE Notifyにより通知を行い、ブザーを鳴らす - 水位が一定以下に戻ったら、ブザーを止める # 部品 | 部品名 | 個数 | |--------------------------------------|------| | obniz Board 1Y | 1 | | Grove Water Level Sensor | 1 | | Grove Buzzer | 1 | | Grove - 4ピン(オス) ジャンパケーブル | 2 | # 回路図 ![回路図](https://camo.elchika.com/463825c6300af4b237f751fa8174f4a358914030/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f30363937363262652d396234652d343933632d393333662d3030366537363337386337382f30363364646365612d623566642d343836342d393065632d343738343065626465323566/) # ソースコード [GitHub](https://github.com/kou0179/WaterLevelNotifier) ## index.ts (entry point) ```typescript import Obniz from "obniz"; import { launchSound, startAlert } from "./Grove_Buzzer.extensions.js"; import { sendMessage } from "./lineNotifyClient.js"; import { GroveBuzzerAlertStopHandler } from "./types.js"; const OBNIZ_ID = "0000-0000"; // OBNIZ ID const LINE_TOKEN = ""; // LINE Notifyのトークン const ALERT_THRESHOLD = 30; const obniz = new Obniz(OBNIZ_ID); obniz.onconnect = async function () { const buzzer = obniz.wired("Grove_Buzzer", { gnd: 0, vcc: 1, signal: 3 }); const sensor = obniz.wired("Grove_WaterLevelSensor", { gnd: 4, vcc: 5, sda: 6, scl: 7, }); let alertStopHandler: GroveBuzzerAlertStopHandler | null = null; await launchSound(buzzer, obniz); await sendMessage({ token: LINE_TOKEN, message: "監視を開始します", notificationDisabled: true, }); // called while online. obniz.onloop = async function () {}; sensor.onchange = async function (value) { console.log(value); if (value >= ALERT_THRESHOLD) { await alertOn(); } else { await alertOff(); } }; async function alertOn() { if (alertStopHandler === null) { alertStopHandler = await startAlert(buzzer, obniz); sendMessage({ token: LINE_TOKEN, message: "水が溢れそうです!!!!!", notificationDisabled: false, }); } } async function alertOff() { if (alertStopHandler !== null) { await alertStopHandler(); } } }; // called on offline obniz.onclose = async function () {}; ``` ## Grove_Buzzer.extensions.ts ```typescript import Obniz from 'obniz' import Grove_Buzzer from "obniz/dist/src/parts/Grove/Grove_Buzzer" import { GroveBuzzerAlertStopHandler } from './types'; export const launchSound = async function (buzzer: Grove_Buzzer, obniz: Obniz): Promise<void> { buzzer.play(3200); await obniz.wait(300); buzzer.stop(); return }; export const startAlert = async function (buzzer: Grove_Buzzer, obniz: Obniz): Promise<GroveBuzzerAlertStopHandler> { let alertCancelRequest = false; (async () => { while (!alertCancelRequest) { buzzer.play(2800); //rec 3000 await obniz.wait(300); buzzer.stop(); await obniz.wait(200); } })(); return () => { alertCancelRequest = true; }; }; ```

-

## lineNotifyClient

+

## lineNotifyClient.ts

```typescript import { LineNotifySendRequest } from "./types.js"; import ky from "ky-universal"; import FormData from "form-data"; export const sendMessage = async (request: LineNotifySendRequest) => { const formData = new FormData(); formData.append("message", request.message); formData.append( "notificationDisabled", (request.notificationDisabled === undefined ? false : request.notificationDisabled ).toString() ); try { const ret = await ky.post("https://notify-api.line.me/api/notify", { body: formData, headers:{ 'Authorization': `Bearer ${request.token}` } }); } catch (err) { console.log(err); } }; ``` ## types.ts ```typescript export type LineNotifySendRequest = { token: string message: string notificationDisabled?: boolean } export type GroveBuzzerAlertStopHandler = { (): void } ```