Building a smart music system from scratch !

Ever wondered if there could be a system that starts playing soothing music when you reach home after a hectic day of work to lighten up your stress. Then you have come to the right place my friend.

Smart Music System Image

It is absolutely possible and the exciting part about it is everyone can do it. The things you need for this are:

  1. A Wi-Fi router.
  2. A computer connected to the router.
  3. A mobile phone.

If you are surviving in this planet then you should have the above things. Yes! It is that simple. Nothing more needed just a few steps and the system will be up and running.

First things first. Connect your “Smart” phone to your home Wi-Fi router. Then you need to get the IP Address assigned to the mobile by the router. To get the IP goto Settings > Status on your phone. This may vary from phone to phone so please do a google search on that for your device. If your mobile and the computer aren’t the only devices connecting to the router, you might want to make this IP static as the router may assign this IP to other devices. Setting a static IP is super easy. Go to Wi-Fi settings > long tap on the connected network > Modify Network >  Check Show Advanced Options >  Choose Static from DHCP / IPv4 options and set the IP. For those who are still having trouble, I have covered a separate article on how to assign a static IP to your Android device.

Now that we got an IP address for our mobile, the real fun begins. Create a new folder on a sweet location of your computer. Next save your favorite melody on that location, say a.mp3. Open any text editor and save the following code as smartmusic.bat (You are free to use any filename as long as it has a .bat extension) on the same location.

@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=up
ping -n 1 !ipaddr! | find "TTL=" >nul: 2>nul:
if not !errorlevel!==0 set state=down
if not !state!==!oldstate! (
echo.Link is !state!
if !state! == up (
a.mp3
)
if !state! == down (
taskkill /f /im "AIMP3.exe"
)
set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal

Note that I use AIMP3 as my default music player. So in the code replace “AIMP3.exe” with the process name of your default music player. (You can get the process name from Task Manager. Most common being wmplayer.exe / vlc.exe / KMPlayer.exe)

Explaining this script is beyond the scope of this article. May be I will cover it in my future posts on demand. Just think it this way, the computer constantly pings your phone and if your phone is there it plays music and if not it stops.

If you have made till this point then great job! Now time for some action. Fire up the command prompt on that location (You can achieve this by simultaneously pressing shift and right-click on an empty space in the explorer and select Open command window here). Then type  smartmusic.bat <ip_addr> and press enter. <ip_addr> is the IP address of your phone.

To test this just turn on/off Wi-Fi on your phone. You will notice the music turns on when the Wi-Fi is connected and turns off when disconnected.

Now some of the assumptions that I have made while writing this article:

  1. Your computer needs to be constantly powered on and connected to the router.
  2. Your phone should auto-connect to your home Wi-Fi when you reach home.
  3. Your phone Wi-Fi state should be active all the time.
  4. You are on Windows 😀

P.S – Trying this in an organization (such as Hostels) may be a bad idea(I once tried this at my hostel and the music started playing whenever I came across a switch on the same subnet. :D). But don’t worry I will be covering this on my future articles.

So, this was my first blog post. Please Like / Share and leave a comment. Until then…