LoginSignup
2
3

Node-REDのobniz functionからDFPlayer Miniを操作する

Posted at

はじめに

Node-REDのobniz node(obniz function)からDFPlayer Miniをシリアル通信で操作する時に少しハマったのでメモを残しておきます。

Nefry BTからDFPlayer Miniをシリアル通信で操作する

以前、Nefry BTを使用してDFPlayer Miniを操作した経験がありました。
その時はArduinoライブラリをインストールして操作しました。

ObnizからDFPlayer Miniをシリアル通信で操作する

既に試されている方がいましたので、obniz developer consoleからDFPlayer Miniを操作してみました。

プログラムがHTML側(index.html)とjavascript側(main.js)に分かれていましたが、javascript側(main.js)をHTML側(index.html)にマージして問題なく動かすことができました。

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>DFPlayer_Mini</title>
    <link rel="stylesheet" href="style.css" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://unpkg.com/obniz@3.x/obniz.js" crossorigin="anonymous"></script>
  </head>

  <body>
    <div id="obniz-debug"></div>
    <button class="btn btn-primary" id="button1">
      ボタン1(1曲目の音楽を再生)
    </button>
    <button class="btn btn-primary" id="button2">
      ボタン2(次の音楽を再生)
    </button>

    <script type="text/javascript">
    var obniz = new Obniz("OBNIZ-ID-HERE");

    obniz.onconnect = async function(){
      console.log("obniz開始");
      const WAITSEC = 1000; //1秒wait

      // UART通信コマンド
      // https://akizukidenshi.com/download/ds/dfrobot/DFPlayer_Mini_Manual.pdf
      // 
      // Start(0x7E),VER,Len,CMD,Feedback,para1,para2,$O(End bit:0xEF)
      // checksumは除く。
    
      // UART通信用:次の曲を再生
      let data_send  = [0x7E,0xFF,0x06,0x01,0x00,0x00,0x00,0xEF];
      // UART通信用:ファイル001を再生
      // let data_send2 = [0x7E,0xFF,0x06,0x03,0x00,0x00,0x01,0xEF];
      // UART通信用:フォルダ01のファイル001を再生
      // let data_send2 = [0x7E,0xFF,0x06,0x0F,0x00,0x01,0x01,0xEF];
      let data_send2 = [0x7E,0xFF,0x06,0x03,0x00,0x00,0x01,0xEF];
    
      // UART宣言
      var uart = obniz.getFreeUart();
      // ボーレート9600でUART通信開始, pin0:tx,pin1:rx,pin2:gnd
      obniz.uart0.start({tx:0,rx:1,gnd:2,baud:9600});

      //ボタン1クリック処理
      $("#button1").click(function(){
        // UARTコマンド送信:次の曲を再生する。
        obniz.uart0.send(data_send2);        //コマンド送信
        console.log(data_send2);
        console.log("uartコマンド送信");    //コンソールにuartコマンド送信を表示
      });
      // ボタン2クリック処理
      $("#button2").click(function(){
        // UARTコマンド送信:次の曲を再生する。
        obniz.uart0.send(data_send);        //コマンド送信
        console.log(data_send);
        console.log("uartコマンド送信");    //コンソールにuartコマンド送信を表示
      });
    }
  </script>
  </body>
</html>

Node-REDのobniz node(obniz function)からDFPlayer Miniをシリアル通信で操作する

初期化処理

obniz functionのプロパティの初期化処理でシリアル通信を有効にします。

1_1.png

obniz function (コード)

USB-シリアル変換モジュールを使用して、シリアル通信のデバッグをしました。

GChMdH0aQAAarPo.jpg

GChICBGbYAAYH0J.png

最初、HTMLのJavascriptコードと同じ記述で動かしたところ、コマンドが送信されていないことを確認しました。

let data_send  = [0x7E,0xFF,0x06,0x01,0x00,0x00,0x00,0xEF];
obniz.uart0.send(data_send);

obniz nodeのヘルプページのサンプルを例にASCIIコードは送信されることを確認しました。

let data_send  = "test";
obniz.uart0.send(data_send);

Google検索で調べたところ、同じような事象を見つけることができました。

Buffer関数を使用してHEXコードを送信することができました。

2_1.png

2
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3