Obnizの時間起動のテストとして、ドラクエの宿屋の曲を圧電スピーカーで流してみた。起動したい時間をDB登録しておいて、その時間になったら曲を流す。(曲のチョイスはただの遊び。あと、音飛びはただのテストなので勘弁して。)
#obniz #protoout
— @Jis (@PmanRabbit) November 30, 2020
時間起動の試作で、ドラクエの宿屋の曲を圧電スピーカーで。(テストなので音飛びは勘弁…) pic.twitter.com/HtZ2sgG4BB
前回記事でPCの起動時間をカスタマイズしたいという、俺得アプリ作成の続きだったりする。
自作PC特化だけど、リモート起動や自動起動を好きにカスタムできるWebアプリをobnizで試作してみた
#今回の記事でやったこと
バックエンド側の実装をローカルで試作。定期実行はnode-cronを利用してみた。
曜日別の起動時間をFirebase(DB)に保存しておき、node-cronで1分間隔に実行。Firebaseに保存した曜日別の起動時間と一致する場合にObnizの圧電スピーカーから音を出す。
node-cron - npm
Node.jsで定期実行メモ -Qiita
実行時のConsole.log。
> node obniz_Periodic.js
2020-11-30(Mon) 23:30
2020-11-30(Mon) 23:31
2020-11-30(Mon) 23:32
connected
目覚めよ。
2020-11-30(Mon) 23:33
#事前準備
DBはCloud Firestoreを利用。以下の通り保存しておいた。
VSCodeのコンソール上でnode.js、Firebase、node-cron、obnizをインストール。
npm init -y
npm i firebase
npm i node-cron
npm i obniz
#コード
'use strict'; // 厳格モード
const cron = require('node-cron'); // node-cron
const Obniz = require('obniz'); // obniz
const firebase = require("firebase/app"); // firebase
require("firebase/firestore"); // cloud firestore
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
authDomain: "xxxx.firebaseapp.com",
databaseURL: "https://xxxx.firebaseio.com",
projectId: "xxxx",
storageBucket: "xxxx.appspot.com",
messagingSenderId: "999999999999",
appId: "1:999999999999:web:xxxxxxxxxxxxxxxxxxxxxx"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore().collection('hpcaccede');
cron.schedule('* * * * *', async function() { // Node Cron 1分間隔
// 日時を取得
let date = new Date () ;
let ymd = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
let hm = `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
let WeekStr = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ][date.getDay()];
console.log(`${ymd}(${WeekStr}) ${hm}`);
// cloud firestore から読み込み
let ObnizID = '';
let OnWeekTime = [{'Sun':''}, {'Mon':''}, {'Tue':''}, {'Wed':''}, {'Thu':''}, {'Fri':''}, {'Sat':''}];
let docRef = db.doc('User01');
await docRef.get().then(function(doc) {
if (doc.exists) {
ObnizID = `${doc.data().ObnizID1}-${doc.data().ObnizID2}`;
OnWeekTime['Sun'] = doc.data().SunTime;
OnWeekTime['Mon'] = doc.data().MonTime;
OnWeekTime['Tue'] = doc.data().TueTime;
OnWeekTime['Wed'] = doc.data().WedTime;
OnWeekTime['Thu'] = doc.data().ThuTime;
OnWeekTime['Fri'] = doc.data().FriTime;
OnWeekTime['Sat'] = doc.data().SatTime;
} else {
console.log('No such Cloud Firestore document!');
}
});
if (hm === OnWeekTime[WeekStr]) {
// cloud firestore の曜日別実行時間と比較して等しければ実行する
const obniz = new Obniz(ObnizID); // Obniz_ID
obniz.onconnect = async function () {
// Obnizに接続
console.log(obniz.connectionState);
const speaker = obniz.wired('Speaker', { signal: 0, gnd: 1 }); // スピーカー
obniz.display.clear(); // ディスプレイ表示(初期画面)
obniz.display.print(`${ymd}(${WeekStr}) ${hm}`);
// スピーカーを鳴らす
speaker.play(1174.659); await obniz.wait(500); speaker.stop();
speaker.play(1108.731); await obniz.wait(500); speaker.stop();
speaker.play(1046.502); await obniz.wait(500); speaker.stop();
speaker.play(987.767); await obniz.wait(500); speaker.stop();
speaker.play(880.000); await obniz.wait(400); speaker.stop(); await obniz.wait(50);
speaker.play(587.330); await obniz.wait(400); speaker.stop(); await obniz.wait(50);
speaker.play(1174.659); await obniz.wait(1200); speaker.stop();
console.log('目覚めよ。');
obniz.close(); // Obnizの切断
}
}
});
#次の課題
デプロイ先をどうしようか調査中。
Herokuだとクレジットカード登録さえすれば常時稼働で1000時間/月使えるっぽい。
(登録しないと550時間/月なので、1ヶ月常時稼働させようと思うと足りない。)