- Playing Serial MP3
[Hardware Components]
- Arduino Uno
- PHPoC Shield for Arduino
- Serial MP3 Player(CATALEX)
- 32GB microSD memory card
[Software Library]
- AltSoftSerial Library(download : https://github.com/PaulStoffregen/AltSoftSerial )
- PHPoC library(WebRemotePush)
[Connection Diagram]
[Operation Movie]
[Source Code]
PHP Code:
#include "SPI.h"
#include "Phpoc.h"
#include <AltSoftSerial.h>
#define ARDUINO_RX 8 // should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 9 // connect to RX of the module
AltSoftSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;
#define CMD_PLAY_W_INDEX 0X03
#define CMD_SET_VOLUME 0X06
#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_SINGLE_CYCLE 0X19
#define SINGLE_CYCLE_ON 0X00
#define SINGLE_CYCLE_OFF 0X01
#define CMD_PLAY_W_VOL 0X22
// Arduino web server
PhpocServer server(80);
void setup() {
mySerial.begin(9600);
delay(500); // wait chip initialization is complete
sendCommand(CMD_SEL_DEV, DEV_TF); // select the TF card
delay(200); // wait for 200ms
Serial.begin(9600);
while(!Serial)
;
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
//Phpoc.begin();
server.beginWebSocket("remote_push");
Serial.print("WebSocket server address : ");
Serial.println(Phpoc.localIP());
}
void loop() {
// wait for a new client:
PhpocClient client = server.available();
if (client) {
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
if(thisChar == 'A')
{
Serial.println("button A press");
sendCommand(CMD_PLAY_W_VOL, 0X1e01);
}
if(thisChar == 'B')
{
Serial.println("button B press");
sendCommand(CMD_PLAY_W_VOL, 0X1e02);
}
if(thisChar == 'C')
{
Serial.println("button C press");
sendCommand(CMD_PLAY_W_VOL, 0X1e03);
}
}
}
}
void sendCommand(int8_t command, int16_t dat)
{
delay(20);
Send_buf[0] = 0x7e; // starting byte
Send_buf[1] = 0xff; // version
Send_buf[2] = 0x06; // the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8); // datah
Send_buf[6] = (int8_t)(dat); // datal
Send_buf[7] = 0xef; // ending byte
for(uint8_t i=0; i<8; i++)
{
mySerial.write(Send_buf[i]) ;
}
}