Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8691

時間起動を試作。ドラクエの宿屋の曲をObnizの圧電スピーカーで流してみた。

$
0
0

Obnizの時間起動のテストとして、ドラクエの宿屋の曲を圧電スピーカーで流してみた。起動したい時間をDB登録しておいて、その時間になったら曲を流す。(曲のチョイスはただの遊び。あと、音飛びはただのテストなので勘弁して。)

前回記事でPCの起動時間をカスタマイズしたいという、俺得アプリ作成の続きだったりする。
自作PC特化だけど、リモート起動や自動起動を好きにカスタムできるWebアプリをobnizで試作してみた

今回の記事でやったこと

バックエンド側の実装をローカルで試作。定期実行はnode-cronを利用してみた。
曜日別の起動時間をFirebase(DB)に保存しておき、node-cronで1分間隔に実行。Firebaseに保存した曜日別の起動時間と一致する場合にObnizの圧電スピーカーから音を出す。
node-cron - npm
Node.jsで定期実行メモ -Qiita

実行時のConsole.log。

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をインストール。

console
npm init -y
npm i firebase
npm i node-cron
npm i obniz

コード

.js
'use strict';// 厳格モードconstcron=require('node-cron');// node-cronconstObniz=require('obniz');// obnizconstfirebase=require("firebase/app");// firebaserequire("firebase/firestore");// cloud firestore// Your web app's Firebase configurationvarfirebaseConfig={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 Firebasefirebase.initializeApp(firebaseConfig);constdb=firebase.firestore().collection('hpcaccede');cron.schedule('* * * * *',asyncfunction(){// Node Cron 1分間隔// 日時を取得letdate=newDate();letymd=`${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;lethm=`${date.getHours().toString().padStart(2,'0')}:${date.getMinutes().toString().padStart(2,'0')}`;letWeekStr=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][date.getDay()];console.log(`${ymd}(${WeekStr}) ${hm}`);// cloud firestore から読み込みletObnizID='';letOnWeekTime=[{'Sun':''},{'Mon':''},{'Tue':''},{'Wed':''},{'Thu':''},{'Fri':''},{'Sat':''}];letdocRef=db.doc('User01');awaitdocRef.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 の曜日別実行時間と比較して等しければ実行するconstobniz=newObniz(ObnizID);// Obniz_IDobniz.onconnect=asyncfunction(){// Obnizに接続console.log(obniz.connectionState);constspeaker=obniz.wired('Speaker',{signal:0,gnd:1});// スピーカーobniz.display.clear();// ディスプレイ表示(初期画面)obniz.display.print(`${ymd}(${WeekStr}) ${hm}`);// スピーカーを鳴らすspeaker.play(1174.659);awaitobniz.wait(500);speaker.stop();speaker.play(1108.731);awaitobniz.wait(500);speaker.stop();speaker.play(1046.502);awaitobniz.wait(500);speaker.stop();speaker.play(987.767);awaitobniz.wait(500);speaker.stop();speaker.play(880.000);awaitobniz.wait(400);speaker.stop();awaitobniz.wait(50);speaker.play(587.330);awaitobniz.wait(400);speaker.stop();awaitobniz.wait(50);speaker.play(1174.659);awaitobniz.wait(1200);speaker.stop();console.log('目覚めよ。');obniz.close();// Obnizの切断}}});

次の課題

デプロイ先をどうしようか調査中。
Herokuだとクレジットカード登録さえすれば常時稼働で1000時間/月使えるっぽい。
(登録しないと550時間/月なので、1ヶ月常時稼働させようと思うと足りない。)


Viewing all articles
Browse latest Browse all 8691

Trending Articles