※Raspberry Pi 3では動作しません。
○下準備
・シリアルコンソールの無効化
次のように操作して、シリアルコンソールを無効に設定します。
①[Menu]→[設定]→[Raspberry Piの設定]を選択します。
②「インターフェイス」タブを選択し、シリアルを無効に設定し、[OK]をクリックします。
・「serialport」「socket.io」モジュールのインストール
次のように操作して、「serialport」と「socket.io」モジュールをインストールします。
①ターミナルを起動し、作業用のフォルダに移動します。
②「npm install serialport socket.io[Enter]」と入力します
○配線
・シリアルコンソールの無効化
次のように操作して、シリアルコンソールを無効に設定します。
①[Menu]→[設定]→[Raspberry Piの設定]を選択します。
②「インターフェイス」タブを選択し、シリアルを無効に設定し、[OK]をクリックします。
・「serialport」「socket.io」モジュールのインストール
次のように操作して、「serialport」と「socket.io」モジュールをインストールします。
①ターミナルを起動し、作業用のフォルダに移動します。
②「npm install serialport socket.io[Enter]」と入力します
○配線
モジュール
|
Raspberry Pi
|
RX
|
8
|
TX
|
10
|
VCC
|
4(または2)
|
GND
|
6
|
○プログラム
・サーバー
次のJavaScriptコードを入力し、「mp3.js」と名前を付けて作業用フォルダに保存します。
・サーバー
次のJavaScriptコードを入力し、「mp3.js」と名前を付けて作業用フォルダに保存します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var serialport = require('serialport'); | |
var port = new serialport.SerialPort( | |
'/dev/ttyAMA0', { | |
baudrate: 9600, | |
parser: serialport.parsers.readline('\r\n') | |
} | |
); | |
var html = require('fs').readFileSync('index.html'); | |
var http = require('http').createServer(function(req, res) { | |
res.writeHead(200, { | |
'Content-Type': 'text/html' | |
}); | |
res.end(html); | |
}); | |
var io = require('socket.io')(http); | |
http.listen(3000); | |
var eqmode = 0; | |
port.on('open', function () { | |
SelectPlayerDevice(0x02); | |
SetVolume(1); | |
SetEQ(0); | |
io.on('connection', function(socket) { | |
socket.on('msg', function(data) { | |
console.log(data); | |
if(data == "play"){ | |
SpecifyMusicPlay(1); | |
} | |
if(data == "pause"){ | |
PlayPause(); | |
} | |
if(data == "resume"){ | |
PlayResume(); | |
} | |
if(data == "prev"){ | |
PlayPrevious(); | |
} | |
if(data == "next"){ | |
PlayNext(); | |
} | |
if(data == "loop"){ | |
PlayLoop(); | |
} | |
if(data == "down"){ | |
DecreaseVolume(); | |
} | |
if(data == "up"){ | |
IncreaseVolume(); | |
} | |
if(data == "repon"){ | |
StartRepeat(); | |
} | |
if(data == "repoff"){ | |
StopRepeat(); | |
} | |
if(data == "eq"){ | |
eqmode ++; | |
if (eqmode > 5) eqmode = 0; | |
SetEQ(eqmode); | |
} | |
}); | |
}); | |
}); | |
function SelectPlayerDevice(device) { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x09,0x00,0,device,0xEF]); | |
setTimeout(port.write(buf),100); | |
} | |
function SpecifyMusicPlay(index) { | |
var hbyte = parseInt(index / 256); | |
var lbyte = parseInt(index % 256); | |
var buf = new Buffer([0x7E,0xFF,0x06,0x03,0x00,hbyte,lbyte,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function PlayPause() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x0E,0x00,0x00,0x00,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function PlayResume() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x0D,0x00,0x00,0x00,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function PlayNext() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x01,0x00,0x00,0x00,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function PlayPrevious() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x02,0x00,0x00,0x00,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function PlayLoop() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x11,0x00,0x00,0x01,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function SetVolume(volume) { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x06,0x00,0x00,volume,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function IncreaseVolume() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x04,0x00,0x00,0x00,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function DecreaseVolume() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x05,0x00,0x00,0x00,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function StartRepeat() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x19,0x00,0x00,0x00,0xFE,0xE2,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function StopRepeat() { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x19,0x00,0x00,0x01,0xFE,0xE1,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} | |
function SetEQ(mode) { | |
var buf = new Buffer([0x7E,0xFF,0x06,0x07,0x00,0x00,mode,0xEF]); | |
setTimeout(port.write(buf),1000); | |
} |
・クライアント
次のHTMLコードを入力し、「index.html」と名前を付けて作業用フォルダに保存します。
次のHTMLコードを入力し、「index.html」と名前を付けて作業用フォルダに保存します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>MP3 TEST</title> | |
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> | |
</head> | |
<body> | |
<script> | |
var socket = io(); | |
function play() { | |
socket.emit('msg', 'play'); | |
} | |
function pause() { | |
socket.emit('msg', 'pause'); | |
} | |
function resume() { | |
socket.emit('msg', 'resume'); | |
} | |
function prev() { | |
socket.emit('msg', 'prev'); | |
} | |
function next() { | |
socket.emit('msg', 'next'); | |
} | |
function up() { | |
socket.emit('msg', 'up'); | |
} | |
function down() { | |
socket.emit('msg', 'down'); | |
} | |
function loop() { | |
socket.emit('msg', 'loop'); | |
} | |
function seteq() { | |
socket.emit('msg', 'eq'); | |
} | |
function repon() { | |
socket.emit('msg', 'repon'); | |
} | |
function repoff() { | |
socket.emit('msg', 'repoff'); | |
} | |
</script> | |
<form> | |
<input type="button" value="PLAY" onclick="play()"/> | |
<input type="button" value="PAUSE" onclick="pause()"/> | |
<input type="button" value="RESUME" onclick="resume()"/> | |
<input type="button" value="<<" onclick="prev()"/> | |
<input type="button" value=">>" onclick="next()"/> | |
<input type="button" value="<" onclick="down()"/> | |
<input type="button" value=">" onclick="up()"/> | |
<input type="button" value="LOOP" onclick="loop()"/> | |
<input type="button" value="EQ" onclick="seteq()"/> | |
<input type="button" value="REP ON" onclick="repon()"/> | |
<input type="button" value="REP OFF" onclick="repoff()"/> | |
</form> | |
</body> | |
</html> |
○動作の確認
次のように操作して、動作を確認します。
①ターミナルを起動し、作業用フォルダに移動します。
②「sudo node mp3.js」と入力します。
③ブラウザで「http://localhost:3000」にアクセスします。
④各ボタンをクリックし、動作を確認します。
0 件のコメント:
コメントを投稿