経緯
洗面台の下にタンクがある場合があり、基本的には溢れる事はありませんが、
水溢れ防止穴から大量の水が流れ込んだ場合や、洗面台シャワーの不具合で溢れる事があります。
先日溢れて散々な思いをしたので事前に防止したいと思います。
デモ動画
※撮影の都合上、実タンクでの映像ではありませんが機能を満足に確認できる動画です。
環境/要件
動作環境
- nodejs v14.15.1
要件
- 起動時にブザーがなり、LINE Notifyに通知が飛ばす
- 水位が一定以上になった時にLINE Notifyにより通知を行い、ブザーを鳴らす
- 水位が一定以下に戻ったら、ブザーを止める
部品
部品名 | 個数 |
---|---|
obniz Board 1Y | 1 |
Grove Water Level Sensor | 1 |
Grove Buzzer | 1 |
Grove - 4ピン(オス) ジャンパケーブル | 2 |
回路図
ソースコード
index.ts (entry point)
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
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.ts
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
export type LineNotifySendRequest = {
token: string
message: string
notificationDisabled?: boolean
}
export type GroveBuzzerAlertStopHandler = { (): void }
1
1
-
kou0179
さんが
2021/04/16
に
編集
をしました。
(メッセージ: 初版)
-
kou0179
さんが
2021/04/16
に
編集
をしました。
(メッセージ: 微修正)
ログインしてコメントを投稿する