Cloud Controlled Music Player

by ehudwill in Circuits > Wireless

588 Views, 9 Favorites, 0 Comments

Cloud Controlled Music Player

cloudControlledMusicPlayer.jpg

The Linkit One has many great applications right out of the box. One of my favorites is the ability to play mp3 files. Combined with the wifi and cloud sandbox I created this great music player.

Materials/Tools

FullSizeRender 7.jpg
FullSizeRender 16.jpg

Materials

1 - Linkit One

1 - Battery

1 - Wifi Cable

1 - Speaker with a 3.5mm cable

1 - Case (I used a plastic container with some foam taped to it to support the board.)

1 - Double sided tape

1 - Micro SD card (Size should be determined by the size/number of sounds you want.)

Tools

1 - Knife

1 - Computer

Code

Screen Shot 2015-11-04 at 7.50.42 PM.png

The code is messy but it works. Right now you can only play songs and stop them.

Some things to note. You will need to input your access point name and password into the code. Also your test device's device id and key. If you don't know how to do that check out this instructable where I explain those steps in more detail.

<p>//#include <br>#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define WIFI_AP ""
#define WIFI_PASSWORD ""
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
#define per 50
#define per1 3
#define DEVICEID "" // Input your deviceId
#define DEVICEKEY "" // Input your deviceKey
#define SITE_URL "api.mediatek.com"</p><p>LWiFiClient c;
unsigned int rtc;
unsigned int lrtc;
unsigned int rtc1;
unsigned int lrtc1;
char port[4]={0};
char connection_info[21]={0};
char ip[21]={0};             
int portnum;
int val = 0;
String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0";
String music_status;
String tcpcmd_play1_on = "play1,1";
String tcpcmd_play1_off = "play1,0";
String tcpcmd_play2_on = "play2,1";
String tcpcmd_play2_off = "play2,0";
String tcpcmd_play3_on = "play3,1";
String tcpcmd_play3_off = "play3,0";
String tcpcmd_play4_on = "play4,1";
String tcpcmd_play4_off = "play4,0";
String tcpcmd_play5_on = "play5,1";
String tcpcmd_play5_off = "play5,0";
String tcpcmd_play6_on = "play6,1";
String tcpcmd_play6_off = "play6,0";
String tcpcmd_play7_on = "play7,1";
String tcpcmd_play7_off = "play7,0";
String tcpcmd_play8_on = "play8,1";
String tcpcmd_play8_off = "play8,0";
String tcpcmd_play9_on = "play9,1";
String tcpcmd_play9_off = "play9,0";
String tcpcmd_play10_on = "play10,1";
String tcpcmd_play10_off = "play10,0";</p><p>LWiFiClient c2;
HttpClient http(c2);</p><p>void setup()
{
  
  LTask.begin();
  LWiFi.begin();
  Serial.begin(115200);
  /*while(!Serial) delay(1000); /* comment out this line when Serial is not present, ie. run this demo without connect to PC */</p><p>  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
  }
  
  Serial.println("calling connection");</p><p>  while (!c2.connect(SITE_URL, 80))
  {
    Serial.println("Re-Connecting to WebSite");
    delay(1000);
  }
  delay(100);</p><p>  getconnectInfo();
  connectTCP();
}</p><p>void getconnectInfo(){
  //calling RESTful API to get TCP socket connection
  c2.print("GET /mcs/v2/devices/");
  c2.print(DEVICEID);
  c2.println("/connections.csv HTTP/1.1");
  c2.print("Host: ");
  c2.println(SITE_URL);
  c2.print("deviceKey: ");
  c2.println(DEVICEKEY);
  c2.println("Connection: close");
  c2.println();
  
  delay(500);</p><p>  int errorcount = 0;
  while (!c2.available())
  {
    Serial.println("waiting HTTP response: ");
    Serial.println(errorcount);
    errorcount += 1;
    if (errorcount > 10) {
      c2.stop();
      return;
    }
    delay(100);
  }
  int err = http.skipResponseHeaders();</p><p>  int bodyLen = http.contentLength();
  Serial.print("Content length is: ");
  Serial.println(bodyLen);
  Serial.println();
  char c;
  int ipcount = 0;
  int count = 0;
  int separater = 0;
  while (c2)
  {
    int v = c2.read();
    if (v != -1)
    {
      c = v;
      Serial.print(c);
      connection_info[ipcount]=c;
      if(c==',')
      separater=ipcount;
      ipcount++;    
    }
    else
    {
      Serial.println("no more content, disconnect");
      c2.stop();</p><p>    }
    
  }
  Serial.print("The connection info: ");
  Serial.println(connection_info);
  int i;
  for(i=0;i</p><p>} //getconnectInfo</p><p>void uploadstatus(){
  //calling RESTful API to upload datapoint to MCS to report Music status
  Serial.println("calling connection");
  LWiFiClient c2;  </p><p>  while (!c2.connect(SITE_URL, 80))
  {
    Serial.println("Re-Connecting to WebSite");
    delay(1000);
  }
  delay(100);
  if(digitalRead(13)==1)
  music_status = "music_stat,,1";
  else
  music_status = "music_stat,,0";
  int thislength = music_status.length();
  HttpClient http(c2);
  c2.print("POST /mcs/v2/devices/");
  c2.print(DEVICEID);
  c2.println("/datapoints.csv HTTP/1.1");
  c2.print("Host: ");
  c2.println(SITE_URL);
  c2.print("deviceKey: ");
  c2.println(DEVICEKEY);
  c2.print("Content-Length: ");
  c2.println(thislength);
  c2.println("Content-Type: text/csv");
  c2.println("Connection: close");
  c2.println();
  c2.println(music_status);
  
  delay(500);</p><p>  int errorcount = 0;
  while (!c2.available())
  {
    Serial.print("waiting HTTP response: ");
    Serial.println(errorcount);
    errorcount += 1;
    if (errorcount > 10) {
      c2.stop();
      return;
    }
    delay(100);
  }
  int err = http.skipResponseHeaders();</p><p>  int bodyLen = http.contentLength();
  Serial.print("Content length is: ");
  Serial.println(bodyLen);
  Serial.println();
  while (c2)
  {
    int v = c2.read();
    if (v != -1)
    {
      Serial.print(char(v));
    }
    else
    {
      Serial.println("no more content, disconnect");
      c2.stop();</p><p>    }
    
  }
}</p><p>void connectTCP(){
  //establish TCP connection with TCP Server with designate IP and Port
  c.stop();
  Serial.println("Connecting to TCP");
  Serial.println(ip);
  Serial.println(portnum);
  while (0 == c.connect(ip, portnum))
  {
    Serial.println("Re-Connecting to TCP");    
    delay(1000);
  }  
  Serial.println("send TCP connect");
  c.println(tcpdata);
  c.println();
  Serial.println("waiting TCP response:");
} //connectTCP</p><p>void heartBeat(){
  Serial.println("send TCP heartBeat");
  c.println(tcpdata);
  c.println();
    
} //heartBeat</p><p>void loop()
//Red led 
{
  //Check for TCP socket command from MCS Server 
  String tcpcmd="";
  while (c.available())
   {
      int v = c.read();
      if (v != -1)
      {
        Serial.print((char)v);
        tcpcmd += (char)v;
        if (tcpcmd.substring(40).equals(tcpcmd_play1_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"001.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play1_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
        else if (tcpcmd.substring(40).equals(tcpcmd_play2_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"001.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play2_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
        else if (tcpcmd.substring(40).equals(tcpcmd_play3_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"001.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play3_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
        
        else if (tcpcmd.substring(40).equals(tcpcmd_play4_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"001.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play4_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
        
        else if (tcpcmd.substring(40).equals(tcpcmd_play5_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"001.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play5_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
        
        else if (tcpcmd.substring(40).equals(tcpcmd_play6_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"001.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play6_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }</p><p>         else if (tcpcmd.substring(40).equals(tcpcmd_play7_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"001.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play7_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
        
        else if (tcpcmd.substring(40).equals(tcpcmd_play8_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"001.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play8_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
        
        else if (tcpcmd.substring(40).equals(tcpcmd_play9_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"009.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play9_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
        
        else if (tcpcmd.substring(40).equals(tcpcmd_play10_on)){
          LAudio.setVolume(3);
          LAudio.playFile( storageSD,(char*)"010.mp3");
          Serial.print("Play Song 1");
          tcpcmd="";
        }
        else if(tcpcmd.substring(40).equals(tcpcmd_play10_off)){  
          LAudio.stop();
          Serial.println("stop");
          tcpcmd="";
        }
      }
   }</p><p>  LDateTime.getRtc(&rtc);
  if ((rtc - lrtc) >= per) {
    heartBeat();
    lrtc = rtc;
  }
  //Check for report datapoint status interval
  LDateTime.getRtc(&rtc1);
  if ((rtc1 - lrtc1) >= per1) {
    uploadstatus();
    lrtc1 = rtc1;
  }
  
}</p>

Setting Up Your Music/sound Effect

FullSizeRender 14.jpg
FullSizeRender 17.jpg

You will need your micro sd card and a computer for this step. Select the music that you want to use. You will need to rename the songs. I have a list below for the ten songs. More can be added but you will have to add lines of code.

  • 001.mp3
  • 002.mp3
  • 003.mp3
  • 004.mp3
  • 005.mp3
  • 006.mo3
  • 007.mp3
  • 008.mp3
  • 009.mp3
  • 010.mp3

If your music/sound files are not in the mp3 format you can use a program like audacity to convert them. Load the files onto your sd card. Then place the sd card in the slot under the usb port on the Linkit One.

Case

FullSizeRender 12.jpg
FullSizeRender 4.jpg
FullSizeRender 5.jpg
FullSizeRender 8.jpg
FullSizeRender 9.jpg
FullSizeRender 6.jpg
FullSizeRender 13.jpg
FullSizeRender 10.jpg

Select a plastic case that will fit the Linkit One well. My wifi antenna and battery both fit easily into the case with the board. I used some foam from another case to protect the board from any jostling. I used double sided tape to stick the foam down. Then I marked where the hole for the audio cable would go by placing the board inside the case. I used a knife to make the hole in the case. (Warning: Knives are sharp so be careful) Then I placed everything in the case to make sure it fit.

Test

FullSizeRender 15.jpg
563ab607937ddbc079000828.jpeg

Now you will test your device. Plug in your battery and your wifi antenna. Place the board in the case. Then plug in the audio jack and switch the board to battery power. The switch is next to the battery plug. Then close the case.

Login to your cloud sandbox. You should see your test device is online.Turn on whatever music/sound effects you want.

If it doesn't work check all connections by unplugging and plugging them back in. Then check your code that you have everything spelled correctly. You may want to take the comments off the code at line to check it through the serial monitor.

Applications

IMG_1478.JPG

I plan to use this just to play music, but this would be a great sound effect generator for a holiday display. Comment below any ideas you have for a project like this.