Subscribe     RSS   MOBILE   EMAIL  
Subscribe to Workshop! It's fun.
Go ahead, pick a reader:
Add to Google
Add to Yahoo
Add to Bloglines
Add to Rojo
Add to Newsgator
Add to Netvibes
RSS Feed
Feed URL:
http://workshop.nseries.com/feed
Scan this code or type this link in your browser for the mobile RSS feed:
http://workshop.nseries.com/feed
Sign up for Workshop emails!
Categories
Archives
Loading
 
17 Dec 2007
 
Tags
 

big pic

Put your N800 into developer mode and you’ll be able to write and run your own Python scripts. Here’s a step-by-step guide to how it works.

One of the cool things about the N800 Internet Tablet is the fact that it ships with Python installed. With this you can fairly painlessly write your own applications for your N800. In this article I’m going to show you how to do this. We’ll get a quick overview of the Python language and we’ll also dig into a couple of nifty N800 tricks.

So what is Python? Python is a programming language created by Guido van Rossum in 1991. If you’re used to coding in Java or PHP, you’ll notice that Python looks a bit different. It’s designed to be concise and very readable, without losing any power. And you don’t have to compile Python code to run it, unlike Java, which makes it great for writing quick scripts. It also comes pre-installed on your N800! (Java does not.)

Our Python Script

To get started, I’m just going to give you a chunk of code and I’ll show you how to get it onto your N800 and run in. After this, I’ll come back to the code and go through it to explain what it does. For now, just accept the code as-is.

Here’s a simple “Hello World!” Python script that will run on the N800:

#!/usr/bin/env python2.5

import gtk
import hildon

window = hildon.Window()
window.connect(”destroy”, gtk.main_quit)
label = gtk.Label(”Hello World!”)A Closer Look
window.add(label)

label.show()
window.show()

gtk.main()

When you run this, it will open up a window and display the text “Hello World!”

Setting Up Your N800

First, you need to put your N800 in developer mode, a.k.a. “Red Pill Mode.” Open up the Application Manager (in the Tools folder). Then pull down the app menu and choose “Tools > Application Catalog…” Select “New”, put “matrix” in the Web Address field (with no “http://”). Hit “Cancel.” Ooh! What’s that? Your N800 should suddenly be acting like Morpheus, asking you whether or not you want to go down the rabbit hole. You do. So take that Red Pill. (Do this same process but choose “Blue Pill” to get back into normal user mode.) Don’t close the Application Manager quite yet.

Okay. Now we’re going to go into command line mode on the N800. To do this, we need to get a terminal program and SSH. (SSH stands for “Secure SHell” and is a tool that lets you securely access other computers across a network.)

So. Go back to “Tools > Application Catalog…”. Hit “New” and fill in the fields like this:

Catalog name: Maemo Official
Web address: http://repository.maemo.org/
Distribution: bora
Components: free non-free

And hit “OK”. Close the Application Catalog box and refresh your application list. You will need to be connected to your wifi network, of course.

A quick aside: What is Maemo, exactly? The word pops up all over the place when using the N800. Maemo is the Linux-based development platform that runs on your N800. It sits on top of a version of Debian Linux. The actual user interface is called “Hildon.” If you look at the Python code above you’ll see a reference to Hildon (”window = hildon.Window()”). We’ll get more into this in a second. What it really means, though, is that you’ve got a full-blown Linux PC inside your N800, so many things you’re used to doing with your laptop or PC you’ll also be able to do with your N800.

Back to the Application Manager. Go back to the Main View and select “Browse Installable Applications.” You should see “openssh” and “osso-xterm” in there. Install both of those. If you don’t see one, go back to the Main View and select “Show Installed Applications.” One or both might already be installed. Regardless, to continue both need to be installed.

Now you’re set to go.

Installing and Running Your Script

(Note: I use a Mac, so my instructions will be Mac-centric. You should easily be able to figure out how to do them on your Windows PC or Linux box.)

Now we’re going to get the script that I showed above ready and we’ll put it on the N800 and run it.

First, copy the code again and save it into a file on your computer called “hello_world.py”.

Second, copy that file it to your N800. I would recommend using Bluetooth. You may have to open up the Control Panel on your N800 and make sure Bluetooth is on and that you know the name of your device. I use Bluetooth Flie Exchange on my Mac. When you transfer your file this way, a dialog box will pop up on the N800 asking you what you want to do. Choose “Save.” Then when it asks you where, choose “Change folder.” I would recommend making a “Python” folder inside of your “Documents” folder to put all of your Python stuff in. You certainly don’t have to, but keeping organized is always a good thing. I am going to assume that you put your file called “hello_world.py” in a folder called “Python” within your “Documents” folder on your N800. The paths I use below will rely on that. If you put the file elsewhere, just adjust the paths to fit what you did.

Great! So now your Python script is on your N800, ready for action.

Now we he have two options as far as controlling the N800 from the command line. The first option: We can run “X Terminal” (in the “Extras” folder in your application menu). We can run that and type out our commands using the N800’s little on-screen keypad. This is fine, except I prefer to use an actual keyboard to type, not this little on-screen one. So I would recommend you use SSH to log into your N800 from your own computer’s command line. Then you can actually type on your own computer and control your N800 right there! Remember: Your N800 is a full-blown Linux box.

To log on to your N800 from your computer, open up your Terminal program. (Again, I’m on a Mac, so you may need to find your Windows or Linux equivalent.)

Now, you need to figure out what the IP number of your N800 is. To do this, on your N800 load “X Terminal” as described above. Start SSH by typing “ssh root@localhost” into the command line. The password is “rootme” (unless you’ve changed it). Then type “ifconfig” into the command line. You’ll see a bunch of stuff. Look for the text “inet_addr:”. The numbers right after that are the IP number of the N800. (On mine, at the moment, it’s 10.0.1.12.)

Use those numbers to connect to your N800 via SSH by typing this on your computer:

ssh root@10.0.1.12 [Remember to replace the IP number with whatever your N800 said!]

The password for your device is “rootme”, unless you’ve already changed it.

Check it out! You’re in your N800! I think this is pretty cool.

Get into your Python directory:

cd /home/user/MyDocs/.documents/Python/

Type “ls” and your Python script should be in there.

Now do this:

python hello_world.py

And take a look at your N800. If you did everything right, your N800 now has a white screen that says plainly “Hello World!” in the middle. Congratulations, you’ve run your Python script.

Remember, now, that all of this stuff you’re doing on your computer’s Terminal you can also do on your N800’s X Terminal. But because of permission issues, you’ll need to SSH in to the N800 from the N800’s X Terminal to do it. Everything after that is the same.

I know that it’s not the greatest thing to have to fire up X Terminal just to launch your simple Python app, but we’ll have to wait to learn how to package up your Python script into a full N800 application with an icon and everything. If you can’t wait, Google around for it. Instructions can be found around online.

A Closer Look

So let’s take a closer look at that Python script. I’m assuming you know basic object-oriented programming techniques, maybe via some experience with PHP or Java. Again, just search online and you’ll find plenty of more in-depth Python primers. I’m just going to explain what our simple hello_world.py script does.

#!/usr/bin/env python2.5

This lets your script know where to find your Python installation.

import gtk
import hildon

These imports are graphics user interface toolkits: “gtk” is a more generally used toolkit; “hildon” is the GUI toolkit designed specially for the N800/Maemo.

window = hildon.Window()
window.connect(”destroy”, gtk.main_quit)

The first line creates a new application window using the Hildon theme. The second line tells the window to destroy itself when the application is quit.

label = gtk.Label(”Hello World!”)
window.add(label)

This should be self-explanatory. We create a label object and attach it to the window. This is our “Hello World!” text.

label.show()
window.show()

Then we display both the window and the label.

gtk.main()

This just tells Python to run your script. Until “main_quit” is called by doing something like closing the window.

This isn’t the most elaborate Python script ever. But I hope it gives you a basic idea of what Python looks like. It’s very concise compared to Java or J2ME.

Summing Up

Great! So here’s what we’ve learned:

We’ve run our own Python script on our N800.

We’ve learned about the N800’s “Red Pill” mode.

We’ve learned how to access apps on the N800 using the command line, either from the N800 itself or by sshing in from another computer.

That’s a bunch. The N800 is a powerful little device. Next time we’ll dig further into some really cool stuff you can make happen with Python and your N800.

 
share          
email   share        print   print       
No Comments
 
Enter comment
Name


Email (not displayed publicly)
 
Related Posts
feature mini
Useful applications for your N96
feature mini
How to use Mobile Web Server: Part 3
feature mini
Q&A with Chris Bennett from America’s Emergency Network
 
Workshop Mobile Code
 
Location:         Language: English