Mobile Me(dia)

Shawn Van Every Shawn.Van.Every@nyu.edu
Spring 2008
H79.2690.1

Week 8 - Nokia Mobile Web Server

Nokia has created a nifty application/service call Mobile Web Server. In short, it is an Apache Webserver that runs on the phone. This opens up a world of possibilities for programming the phone to be accessed remotely from another application, a physical device or simply a web browser.

The Mobile Web Server is an open source project with a description and source code available here: http://opensource.nokia.com/projects/mobile-web-server/

Nokia has taken a further step and offered a version of it available as a service called mymobilesite.net. From there you can download it to your computer or use this url (http://download.mymobilesite.net) on your mobile phone.

Following downloading and installing the software, you must register in order to use the mymobilesite.net service. This service provides quite a few things for you, one of the main things being a "proxy" and a hostname so that your phone can be found and accessed on the internet.

https://secure.mymobilesite.net/register/

After you register and login you will come to a status page which shows the status of your server.

You can then finish the installation of the MWS on your phone by entering your username and password (by selecting "next" on the screen that asks you to "register").

Now if you reload the page on the site, you will see that you are online and you can visit the site.

If you didn't make your server public, you will have to enter your username and password once again.

The Mobile Web Server runs applications on your phone to provide the functionality. These applications are written in Python for S60 (http://opensource.nokia.com/projects/pythonfors60/)

It uses an Apache module called mod_python is used to use these applications with the server. (That is probably a detail that you don't need to consider but it is nice to know.)

There are quite a few existing applications that come bundled with the mymobilesite.net version which I will let you test out yourself. For now, we are going to concentrate on writing our own applications.

Developing Applications for MWS

First we need to learn a little bit about Python and Python for S60. (Over the next couple of weeks we will get more in-depth)

Jurgen Scheilble has written a nice set of tutorials and has some great samples online: Python for Series 60 tutorial

Of particular interest are the Syntax lessons: Syntax issues, lists, tuples, dictionary, string handling, for loop and while loop.

He has also co-authored a book: Mobile Python: Rapid Prototyping of Applications on the Mobile Platform of which we have a couple of copies.

His intro is a good place to get started if you are interested in writing GUI based applications but not completely applicable to working with the Mobile Web Server.

We, on the other hand are going to look at doing things like accessing the camera through the Web Server.

The first thing that we need to do is create a folder on our phones to hold our application. This folder has to be within the root of the web server.

To do this on the Mac, use bluetooth and "browse" your phone. Navigate to the drive on which you installed the web server (C: is the phone memory and E: is the memory card). You should see a folder called "Data" and a folder called "Web server" inside of that and a folder called "htdocs" inside of that.

Here is where you will create the folder to hold your application.

Let's create one called "test".

The first thing we need to do to start developing is create an "ht.acl" file. This file tells the webserver what to do when accessing this directory.

Here are the default contents of an ht.acl file for launching a Python script:
# 1. Register Python file extension 
AddHandler mod_python .py 
# 2. Add our own handler 
# Basically this means that test.py will be executed when files with .py 
# extension are requested from this directory 
PythonHandler test 
# 3. Toggle debug on to make life easier in error situation 
PythonDebug On 
# 4. Turn all options off 
Options None 
# 5. Deny access to Python compiled files 
Order Deny,Allow 
Allow from all  
<FilesMatch "\.(pyc)$"> 
 Deny from all 
</FilesMatch>
Now we can write the actual python script that will be called. As specified in ht.acl file, this script should be called "test.py" and placed in the same directory.
# S60 System Stuff
import e32

# 1. Define request handler 
def handler(req): 

	# 2. Import necessary Apache modules 
	from mod_python import apache 

	# 3. Set correct content type to request 
	req.content_type = 'text/html' 

	# 4. Generate simple HTML page 
	req.write("<html>Hello World")
		
	req.write("</html>")
	
	# 5. Return apache.OK to indicate that page handling was successful 
	return apache.OK 
Now you should be able to visit your application via the world wide web.

https://yourusername.mymobilesite.net/test/.py

Because the Mobile Web Server is serving up pages via HTTP and we have the full power of Python for S60 (signed) running on the phone the possibilities are pretty endless. We can easily write a script to control any aspect of the phone from just about any application or object (even those running on the phone itself through Flash or Mobile Processing).

Here is an example which uses the camera library with Python S60 to capture an image.
# S60 System Stuff
import e32

# Import camera library
import camera


# 1. Define request handler 
def handler(req): 

	# 2. Import necessary Apache modules 
	from mod_python import apache 

	# 3. Set correct content type to request 
	req.content_type = 'text/html' 

	# take a photo in of the size 160x120 pixels
	screen_picture = camera.take_photo(size = (640,480))

	# define a filename under which teh picture shall be saved
	filename=u'e:\\Data\\Web server\\htdocs\\test\\picture.jpg'
	# save the newly taken in picture
	screen_picture.save(filename)
                
	# 4. Generate simple HTML page 
	req.write("<html>Hello World <img src='picture.jpg'>")		
	req.write("</html>")
	
	# 5. Return apache.OK to indicate that page handling was successful 
	return apache.OK 



More infomration:
PyS60 Library Reference
Much more Documentation on MWS
How to develop content for the MWS (Chapter 4 in the above PDF is the most interesting for us)

Much more:
PAMP (Personal Apache MySQL and PHP) coming to S60
Mobile Web Server Blog