I have recently programmed my Nokia N95 phone to control my music player (the rather marvellous snackAmp) remotely.
SnackAmp sits on my Linux-based server box. It has a remote control feature (details here) which accepts control strings from a TCP socket. I wrote a Python script which runs on the phone to open a Wi-Fi connection to my music server and send simple commands to control it.
I was pleasantly surprised at just how easy all this turned out to be
First, I installed Python. This is available from the Nokia Opensource site. The N95 uses the 3rd edition of the S60 operating system, so I downloaded these files:
- PythonForS60_1_4_1_3rdEd.SIS
- PythonScriptShell_1_4_1_3rdEd.sis
Then I wrote a small Python script to open a TCP socket on the music server and send the appropriate text strings to control the music player.
The script (see below) first defines some handy functions. send_command actually sends the specified command to the music player. play, pause, stop, next and prev call the send_command function with the appropriate command text.
The main section of the script first checks if there are any command line parameters. If there are, it tries to immediately send the appropriate command and then exits.
If not, it discovers which operating system it is running on, and uses that information to execute one of two sections. The S60 section uses a standard Nokia list GUI widget to enable the user to select a command. The script sends the command and exits. Note that the Nokia GUI requires you to specify all strings as Unicode. Python does this by prefixing each string with ‘u’ - for example, u’Pause’.
The Linux section uses the standard Python Tkinter GUI library to generate a small X-windows based window with push buttons to select commands. This part of the script uses a loop to allow multiple commands to be sent. The window is active until you select the Quit option.
Here is the script:
#!/usr/bin/python
import socket
import sys
import string
def send_command(command):
# opens a socket to snackAmp, sends the appropriate command and
# closes the socket.
# create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to server
host = '192.168.1.20' # server address - bletchley is 192.168.1.20
port = 8775 # server port
s.connect((host, port))
s.send(command) # send command
# close the connection
s.close()
def prev():
print 'previous...'
send_command('xmms_remote_playlist_prev')
def pause():
print 'pause...'
send_command('xmms_remote_pause')
def play():
print 'play...'
send_command('xmms_remote_play')
def stop():
print 'stop...'
send_command('xmms_remote_stop')
def next():
print 'next...'
send_command('xmms_remote_playlist_next')
args = sys.argv[1:] # don’t keep the program name
if len(args) > 0:
for a in args:
found = 0
a = string.lower(a)
if a == ‘pause’:
found = 1
pause()
if a == ‘prev’:
found = 1
prev()
if a == ‘play’:
found = 1
play()
if a == ’stop’:
found = 1
stop()
if a == ‘next’:
found = 1
next()
if found == 0:
print “Command not recognised. Currently implemented are pause, prev, play, stop, next.”
else:
if sys.platform == ’symbian_s60′:
# we are running on a Nokia phone
import appuifw # access to the user interface framework
menu = [u'Pause', u'Prev', u'Next', u'Play', u'Stop']
index = appuifw.selection_list(choices=menu, search_field=0)
if index == 0:
pause()
if index == 1:
prev()
if index == 2:
next()
if index == 3:
play()
if index == 4:
stop()
else:
# we are running on a standard computer (i.e. not a phone)
from Tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.pauseButton = Button (self, text=”Pause”, underline = 0, command=pause)
self.pauseButton.grid(row=0, column=0, columnspan=4, sticky=E+W)
self.prevButton = Button (self, text=”Prev”, underline = 1, command=prev)
self.prevButton.grid(row=1, column=0)
self.playButton = Button (self, text=”Go”, underline = 0, command=play)
self.playButton.grid(row=1, column=1)
self.stopButton = Button (self, text=”Stop”, underline = 0, command=stop)
self.stopButton.grid(row=1, column=2)
self.nextButton = Button (self, text=”Next”, underline = 0, command=next)
self.nextButton.grid(row=1, column=3)
self.quitButton = Button (self, text=”Quit”, underline = 0, command=self.quit)
self.quitButton.grid(row=2, column=0, columnspan=4, sticky=E+W)
app = Application()
app.master.title(”snackAmp Control”)
app.mainloop()
You’ll need to create the script in your favourite text editor on your PC and then transfer it to the phone. I used a USB cable to connect the phone to the PC and selected the mass storage option on the phone. Once the file is transferred, you’ll need to disconnect the USB cable so the phone can access the memory card again.
In theory you can use Bluetooth to transfer the script, but I’ve seen reports that it doesn’t work with S60 3rd edition. I don’t have Bluetooth on my Linux server, so I can’t tell you
In use, start Python on the phone, then run a script. Select the script name and the command list appears. Choose a command. The ‘Select access point’ screen appears and I choose my Wi-Fi router connection. Then magic happens and the music obeys my commands 