###「素振りを頑張った君へ」最高のプレゼント!!
素振りの練習を頑張ったあなたへ最高のサプライズプレゼントを用意しました。
LINEから、「素振りの練習終わったよ!」と伝えると返信がきて、サプライズの音楽が流れるシステムを制作しました。
##開発環境
・LINEBot公式アカウント解説
・Visual Studio Code基本操作
・LINEBot参考ページ
・obniz公式ドキュメント(起動とWi-Fi設定)
・obniz公式ドキュメント(Speaker)
##制作物
##ソースコード「赤鼻のトナカイ」「Joy to the world」
ソースコード **(クリックで表示)**
'use strict';
// obniz呼び出し
const Obniz = require('obniz');
const obniz = new Obniz("自分のID"); // 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: '##########################',
channelSecret: '###################'
};
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 () {
var servo = obniz.wired("ServoMotor", {gnd:0, vcc:1, signal:2});
// LED 赤
var led = obniz.wired("LED", {anode:6, cathode:7});
led.blink(210); // 210msec
// LED 青
var led = obniz.wired("LED", {anode:8, cathode:9});
led.blink(210); // 210msec
// LED 黄
var led = obniz.wired("LED", {anode:10, cathode:11});
led.blink(210); // 210msec
let count = 0;
//
let angles = [10, 50, 10, 50, 10, 180, 10, 50, 10, 50, 10, 180, 10, 50, 10, 50, 10, 50, 10, 50, 10, 180, 10, 50, 10, 50, 10, 50, 10, 50, 10, 50, 10, 180, 10, 50, 10, 180, 10];
let timer = setInterval(() => {
count++;
servo.angle(angles[count]);
//
if (count >= 100) {
clearInterval(timer);
let degrees = 300.0;
}
}, 1200);//500msec
obniz.display.clear();
obniz.display.print("BestPractice Merry Christmas!");
}
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:4, gnd:5});
console.log("Speaker")
await client.pushMessage(userId, {
type: 'text',
text: "プレゼントForYou!",
});
// 音階とhzの参考サイト https://tomari.org/main/java/oto.html
await speaker.play(391.995);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(600);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(1300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(493.883);
await obniz.wait(1500);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(293.665);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(493.883);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(1600);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(200);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(1400);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(493.883);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(293.665);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(261.626);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(493.883);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(493.883);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(493.883);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(493.883);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(293.665);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(293.665);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(293.665);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(293.665);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(293.665);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(261.626);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(523.251);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(440.000);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(391.995);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(349.228);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(329.628);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(293.665);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
await speaker.play(261.626);
await obniz.wait(300);
await speaker.stop();
await obniz.wait(100);
}
app.listen(PORT);
console.log(`Server running at ${PORT}`);
##まとめ
音階もいちから制作したので、皆さんぜひ使ってみてくださいませ!!!
動画は、早送りしてますが、音楽は、ゆっくり流れます!!
クラッカーを最後鳴らすところまで制作したかったのですが、出来ませんでした!!
次への課題です!