2016年10月14日金曜日

秋の夜長の電子工作 - bluetooth通信で制御するMP3プレーヤー


 














○パーツリスト
1
bluetoothモジュール(HC06など)
1
1
1
1
抵抗20kΩ
1
M2タッピングネジ
2

その他、配線用ワイヤー、ホットボンドなど。

○回路図

○配線・レイアウト


bluetoothモジュールはMP3モジュールの下にあります。

TP4056 充電器モジュールの改造手順は、こちらを参考にしてください。

○制御用プログラム

・Androidアプリ
 こちらから、apkファイルをダウンロードして、インストールしてください。
 

 MIT App Inventorのプロジェクトファイルは、こちらからダウンロードできます。

・PC(Node.js)
  必要モジュール soket.io、serialport
 次のHTMLとJavaScriptコードを入力し、作業用フォルダに保存します。
<!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="Vol Down" onclick="down()"/>
<input type="button" value="Vol Up" 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>
view raw index.html hosted with ❤ by GitHub
var serialport = require('serialport');
var port = new serialport.SerialPort(
'COM6', {
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);
}
view raw mp3.js hosted with ❤ by GitHub
 COMポートには、bluetoothモジュールのCOMポートを指定(コード中では「COM6」を設定しています)してください。
 PC(Windows)とbluetoothモジュールのペアリング、COMポートの確認方法は、「IchigoJamとPCの通信をBluetoothで無線化する」を参照してください。

 以下の手順で動作を確認します。
①コマンドプロンプトを起動し、作業用フォルダに移動します。
②「node mp3.js」と入力します。
③ブラウザで「http://localhost:3000」にアクセスします。
④各ボタンをクリックし、動作を確認します。

○参考
 ケースの3Dプリントのデータは、こちらからダウンロードできます。

0 件のコメント:

コメントを投稿