醬是創客的ESP32教學主題第19篇,以NodeMCU-32S(使用Arduino語言)來實作教學,本篇教學Arduino播放Micro SD MP3/WAV音樂語音檔案,還可以撥放指定MP3音樂

以下是我們今天的目標

  • 播放指定資料夾位置音樂
  • 調高/調低聲音
  • 暫停/播放/停止音樂

設備:
安信可原廠/副廠NodeMCU-32S #露天拍賣 #蝦皮購物
醬是創客MP3模組 #露天拍賣 #蝦皮購物
Micro SD卡 32GB(FAT32檔案格式)

接線方式:
NodeMCU-32S(GPIO17)–TTL訊號–醬是創客MP3模組(RX)
NodeMCU-32S(GPIO16)–TTL訊號–醬是創客MP3模組(TX)
DC 5V(+)–醬是創客MP3模組(VCC)
DC 0V(-)–醬是創客MP3模組(GND)

Ai-Thinker安信可NodeMCU-32S的腳位圍(資料來源:安信可)

醬是創客MP3模組相關資料

檔案位置須依指定位置擺放,XXXX請使用英文,不要使用中文或數字,最大只能用到32G的大小,僅建議使用FAT32檔案格式
EX: 第1資料夾第2首 -> /01/002XXXX.mp3
EX: 第1資料夾第3首 -> /01/003XXXX.mp3
EX: 第2資料夾第5首 -> /02/005XXXX.mp3
更多規格資訊請參考https://github.com/0xcafed00d/yx5300/blob/master/docs/Serial%20MP3%20Player%20v1.0%20Manual.pdf

Arduino 範例程式碼如下

//醬是創客 開發實作的好夥伴
#include <WiFi.h>
#define RXD2 16
#define TXD2 17
 
byte serial_8_byte_TX_payload[8];

 
void setup(void) {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);

  //重設晶片
  serial2_8byte_tx(0x0c, 0x00, 0x00);

  //選擇系統讀取TF卡資料
  serial2_8byte_tx(0x09, 0x00, 0x02);

  //讀取資料夾位置/01/002XXXX.mp3檔案
  serial2_8byte_tx(0x0f, 1, 2);

  //讀取資料夾位置/01/001XXXX.mp3檔案
  serial2_8byte_tx(0x0f, 1, 1);

  //讀取資料夾位置/01/003XXXX.mp3檔案
  serial2_8byte_tx(0x0f, 1, 3);
  
  //讀取資料夾位置/02/005XXXX.mp3檔案
  serial2_8byte_tx(0x0f, 2, 5);
  
  //讀取資料夾位置/02/006XXXX.mp3檔案
  serial2_8byte_tx(0x0f, 2, 6);
  
  //調低聲音一階
  serial2_8byte_tx(0x05, 0x00, 0x00);
  
  //調高聲音一階
  serial2_8byte_tx(0x04, 0x00, 0x00);
  
  //設定音量為30階(預設)
  serial2_8byte_tx(0x06, 0x00, 30);
  
  //設定音量為5階
  serial2_8byte_tx(0x06, 0x00, 5);

  //設定音量為30階
  serial2_8byte_tx(0x06, 0x00, 30);

  //暫停撥放音樂
  serial2_8byte_tx(0x0E, 0x00, 0x00);
  
  //繼續撥放音樂 
  serial2_8byte_tx(0x0D, 0x00, 0x00);
 
  //停止撥放音樂
  serial2_8byte_tx(0x16, 0x00, 0x00);
  
  //繼續撥放音樂(重新)
  serial2_8byte_tx(0x0D, 0x00, 0x00);    
}
 
void loop(void) {
}
 
void serial2_8byte_tx(int8_t command, int8_t data_1, int8_t data_2){
  if (command == 0x0c or command == 0x09) {
    delay(1500);
  }
  else {
    delay(500);
  }
  serial_8_byte_TX_payload[0] = 0x7e; //starting byte
  serial_8_byte_TX_payload[1] = 0xff; //version 
  serial_8_byte_TX_payload[2] = 0x06; //the number of bytes of the command without starting byte and ending byte 
  serial_8_byte_TX_payload[3] = command;
  serial_8_byte_TX_payload[4] = 0x00;//0x00 = no feedback, 0x01 = feedback 
  serial_8_byte_TX_payload[5] = data_1;
  serial_8_byte_TX_payload[6] = data_2;
  serial_8_byte_TX_payload[7] = 0xef; //ending byte
  Serial2.write(serial_8_byte_TX_payload, 8);
  Serial.print("tx:");
  for( byte b : serial_8_byte_TX_payload )Serial.print( b, HEX );
  Serial.println(" created by chosemaker.com"); 
}

Arduino 序列埠監控視窗 輸出如下,我們播放/01/002XXXX.mp3檔案、/01/001XXXX.mp3檔案、/01/003XXXX.mp3檔案、/02/005XXXX.mp3檔案、/02/006XXXX.mp3檔案、調低聲音一階、調高聲音一階、設定音量為30階(預設)、設定音量為5階、設定音量為30階、暫停撥放音樂、繼續撥放音樂、停止撥放音樂、繼續撥放音樂(重新)

load:0x40078000,len:8896
load:0x40080400,len:5816
entry 0x400806ac
tx:7EFF6C000EF created by chosemaker.com
tx:7EFF69002EF created by chosemaker.com
tx:7EFF6F012EF created by chosemaker.com
tx:7EFF6F011EF created by chosemaker.com
tx:7EFF6F013EF created by chosemaker.com
tx:7EFF6F025EF created by chosemaker.com
tx:7EFF6F026EF created by chosemaker.com
tx:7EFF65000EF created by chosemaker.com
tx:7EFF64000EF created by chosemaker.com
tx:7EFF66001EEF created by chosemaker.com
tx:7EFF66005EF created by chosemaker.com
tx:7EFF66001EEF created by chosemaker.com
tx:7EFF6E000EF created by chosemaker.com
tx:7EFF6D000EF created by chosemaker.com
tx:7EFF616000EF created by chosemaker.com
tx:7EFF6D000EF created by chosemaker.com