星野源さんの『うちで踊ろう』。LINEメッセージを送るとIot(スピーカー)がメロディを奏でるbotをうちでプログラムしました。
基本的な開発方法は前回の記事の『きらきら星』と同じです。
今年のGWは難しいですが、
状況が落ち着いたら以下のようにしてピクニックに持っていけます。
##環境
- Windows 10
- Node.js 12.16.1
##必要なもの
-
スピーカー(30円で購入できました)
##事前準備
前回の記事と同じです
##BOTの動き
画像をクリックするとYOUTUBEに飛びます
##開発コード
'use strict';
// obniz呼び出し
const Obniz = require('obniz');
const obniz = new Obniz("hogehoge1"); // Obniz_ID に自分のIDを入れます
const express = require('express');
const line = require('@line/bot-sdk');
const axios = require('axios');
const PORT = process.env.PORT || 3000;
const config = {
channelAccessToken: 'hogehoge2',
channelSecret: 'hogehoge3'
};
const app = express();
app.post('/webhook', line.middleware(config), (req, res) => {
console.log(req.body.events);
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result));
});
const client = new line.Client(config);
// obniz接続
obniz.onconnect = async function () {
obniz.display.clear();
obniz.display.print("obniz meets LINE Bot!");
}
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
return Promise.resolve(null);
}
let mes = ''
if(event.message.text === 'うちでおどろうをかけて'){
mes = '少々お待ちください'; //待ってねってメッセージだけ先に処理
getAskObnizSpeaker(event.source.userId); //スクレイピング処理が終わったらプッシュメッセージ
}else{
mes = event.message.text;
}
return client.replyMessage(event.replyToken, {
type: 'text',
text: mes
});
}
const getAskObnizSpeaker = async (userId) => {
// スピーカーを呼び出す
const speaker = obniz.wired("Speaker", {signal:0, gnd:1});
console.log("Speaker")
await client.pushMessage(userId, {
type: 'text',
text: "音を鳴らしています",
});
// 音階とhzの参考サイト https://tomari.org/main/java/oto.html
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(1200);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(600);
await speaker.play(285.665); // C5 レ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150)
await speaker.stop();
await obniz.wait(80)
await speaker.stop();
await obniz.wait(1580);
//
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(579.33); // C5 レ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(579.33); // C5 レ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(579.33); // C5 レ
await obniz.wait(750);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(1050);
await speaker.stop();
await obniz.wait(1920);
//
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(300);
//
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(300);
await speaker.play(383.995); // C5 ソ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(300);
//
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(579.33); // C5 レ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(300);
//
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(240);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(485.883); // C5 シ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(300);
await speaker.play(383.995); // C5 ソ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(321.628); // C5 ミ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(150);
await speaker.stop();
await obniz.wait(80);
await speaker.play(432); // C5 ラ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
await speaker.play(383.995); // C5 ソ
await obniz.wait(300);
await speaker.stop();
await obniz.wait(80);
speaker.stop();
}
app.listen(PORT);
console.log(`Server running at ${PORT}`);
以下の部分はご自身のObniz_IDとLINE BOTの情報で更新します。
※Obniz_IDは、Wi-Fi接続が完了するとobniz画面に表示されるものです。
var obniz = new Obniz("hogehoge1"); // Obniz_ID に自分のIDを入れます
channelAccessToken: 'hogehoge2',
channelSecret: 'hogehoge3'
##終わりに
・『うちで踊ろう』はいい曲です。