Win some, lose some

I’ve been working really hard the last weeks. I’ve started to develop the Flash web application. I started with the calendar part which gets all the different days from the database and presents a nice, scrollable and selectable list of days. The user can choose from which days she wants to see data. They are grouped by month and presented in a visual hierarchy. As you can hear I’m pretty happy with it and especially happy with how smooth the coding is going.
I must confess I’m very happy with ActionScript 3. What a robust language! Once you get the hang of it (and it is quite a difference from the first handful of methods and from AS1 too) it’s great to work with. So now I’m ready to start with Google maps and the projection of the data.

Design for the web application

Design for the web application

Onto the part where I lost some… I’d already done quite some work on the communication between the Arduino and the Nokia. Except for the formatting I could receive data from the Arduino on my phone. As I needed to  fill the database for my web app I tried running the old (working) scripts on my phone. I could make a Bluetooth connection but the communication didn’t work and I often got the message error(13, ‘Permission Denied’), a dreaded message which I thought I’d solved. So I tried installing a new version etc. No luck. As I’m running out of time for the presentation on the 19th I decided to fake the data so I could at least continue with my development.
So now I’ve manipulated the data from my collecting silence database. I wrote a Processing script which loops through all the lines of my database dump and creates new SQL queries from the existing data and writes them to a file. I could then import the queries and now I have a nice database on AQAb filled with almost 1000 rows of data. I do feel a little naughty but it’s the only way I can continue with my work at the moment. My knowledge of Python on mobile devices is too limited. And now I can’t even run the Scriptshell without errors, *sigh*

Oh yes, I’ve also done a video and photo shoot with Hans, my model. Pfff, filming is a true profession. I hope I’ll be able to make something presentable of the shots when editing. It’s a challenge to keep track of everything when you have to be director, producer and cameraperson at the same time.

I heart Python

I’m continuing my quest for my mobile app. It’s at the heart of sharing the data I capture with my sensors. After I’ve started the program it should:
Connect to the Arduino board very n minutes (2 for example)
 Arduino should start the sensors
 wait for a few seconds (they need heating up)
 loop through pins a, b, c, d (NO2, CO, O3, temperature)
 check smell flag
 turn of the sensors
Loop through the Arduino values
Store them as a Python dictionary
Get the latitude and longitude from the internal GPS
Store them in the same dictionary
Connect to the internet
Post the dictionary content to a PHP script (which writes them into a database)

Data returned after inserted into database

Data returned after inserted into database

I’m relieved that I’ve made some progress. Yesterday I made a connection to internet and posted variables to a (very basic) PHP script. Today figured out the elegant data type called dictionary. http://docs.python.org/tutorial/datastructures.html#dictionaries It’s a kind of array but you can store key:value pairs. So they’re ideal for creating POST variables to send to a PHP script and very easy and flexible to build. I build a little air quality table that stores all the sensor data, date and time and whether or not the smell was nice at the time of the measurement (more about that some other time). Once I’d manage to store all the values in the database it was time to look for code to read out the GPS values. I came across this great tutorial with a tiny bit of code. http://www.scribd.com/doc/8981028/Tutorial-PythonForS60 Enough for what I need.
Of course the code will have to include error handling. The next step is connecting to the Arduino.

Update 9-9-2009
After working with the program for a bit it’s clear that it sometimes takes quite some time for the GPS values to be found. Earlier I had a field in my database table which would insert the data and time at the moment the data was posted. This may very well not be the time the air quality is measured so now I use the phone time which I can also read and format easily with two lines of Python code. Love that language!

Day by day

I’ve just finished the interface for browsing the work day by day. It took much more time then anticipated. This was because as soon as I jumped to another day the program started slowing down to an unacceptable level. I had to figure out why that happened before I could carry on. Finding that out and fixing it took me about five hours!

I finally discovered that a drawing new heart-beat graph slowed down the program because it got bigger with every new day you clicked. This graph is build up from thousands of dots and lines. To make the animation go faster I had to remove those first. But I put them in the wrong place so I couldn’t remove them. I had to rewrite the code for building the graph so I knew where all the lines and dots were and then I could remove them. Now with every day you pick the old graph is removed and a new one is drawn and the speed stays the same.

On the net I found this really nice code for adding listeners to the seven buttons in a dynamic way:

addEventListener(MouseEvent.CLICK, mouse_click);
function mouse_click(event:MouseEvent):void {
var object_name=String(event.target.name);
for (var i:int = 0; i < 7; i++){
if (object_name == day_btn_array[i])
{
get_day_data(event);
}
}
}

So instead of writing all the listeners by hand I now only have three eventlisteners (only one is shown here.) With every click the function finds out what button was clicked. It calls the appropriate function and passes the original click event. In the called function I again retrieve the name of the button from that event using event.target.name again.

Lost my tracks

Yesterday I discovered something very disturbing. The XML file with the GPS track was missing 40 tracks in the log for the fourth. Not at the beginning or end but just, somewhere…?! This file has thousands of nodes and is almost 1 Mb big. A node looks likes this:
<trkpt lat=”52.387110000″ lon=”4.909017000″>
<ele>48,5</ele>
<time>2009-01-05T07:46:35Z</time>
</trkpt>
That’s very easy for a computer to read but not for a human.

screen dump of compare_times output

screen dump of compare_times output

I had to come up with a way to pinpoint the gaps. So I started to write a program called compare_times. I wanted a way to compare the times logged by the Suunto watch to the times stored by the GPS logger. Usually I don’t need the time node of a GPS track but now I could use it to see if the times weren’t matching at places. I output the times side by side to a file so I could compare them. And yes indeed, at some point they were not matching anymore.

Times in the GPS log that need my attention

Times in the GPS log that need my attention

So now I had to enhance my program so that it would point out to me exactly where things were going wrong. It’s impossible for a human to see for example two tracks missing at say 17:48:13 and 17:48:23 on a whole day. I decided that I would count the tracks in every minute. If there weren’t six tracks (logs) in one minute something was wrong. It sounds easy but it wasn’t all that easy to program. But I worked it out and ended up with a clean list of attention points so I knew where to look.
Mending the data was more work then writing the program. This was especially true for the log of the fifth. There 200 tracks were missing! Sometimes six minutes of data were just not there. Now I’ve got the day data of day 1 to 4. I hope the other days won’t be as bad.

Getting things on a map

Custom marker in Google maps for Flash

Custom marker in Google maps for Flash

Yesterday I continued with my Flash web app. I’d already done some tests with part of the application. For example displaying a map inside Flash. Only recently Google maps have released an API for working with Google maps inside Flash. This makes it very easy to include (interactive) maps inside your Flash app. Of course when you want different things it’s harder :)

I want to display two maps, one for Amsterdam and one for Breda. On the map you can follow me around for two weeks. I wanted to use a fitting icon to display where I have been. So I designed a heart to indicate where it was beating at a certain time. I thought it would be simple to display my custom marker. But I could only find difficult and not so logical examples explaining how this is done. So with a fresh mind in the morning I took a new look at the documentation and worked it out. As it took me quite some time to work this simple code out I’ll just repeat it here for those interested:

function onMapaReady(event:Event):void {
   map_a.setCenter(new LatLng(b_line_array[2],b_line_array[3]), 14, MapType.NORMAL_MAP_TYPE);
   var a_icon:Marker = new Marker(   new LatLng(b_line_array[2],b_line_array[3]),
   new MarkerOptions({
      icon: new my_icon()
   })
);
map_a.addOverlay(a_icon);
}

So in my library I have a movie clip called (linkage) my_icon. I use this for the Amsterdam and the Breda map, except that it’s called b_icon there, the rest of the code is the same. Make sure you import the marker classes:

import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;

And add the overlay, then you’re done.

Format data application

Today I finished my program for combining data from different files. There will be three different files at the end of a day. A text file with lines and tab separated data containing all the data recorded by the Suunto watch. A gpx file that contains GPS data in xml format. And a text file with lines and tab separated data which holds time and activity data.

I placed myself for the daunting task of combining these different files and formats into one tab separated text file. As Flash can’t output a text file I chose Processing for the job. It appeared the capabilities to work with strings are rather limited compared to ActionScript or PHP. But they do have a very convenient XML element class. It was actually the first time I worked with an XML file in this way (that is extracting attributes from an XML node). The loadStrings function is very handy for loading external data and putting it into an array at the same time. In one big loop I cycle through all the files, add a concatenated line at the end of every loop. The result is a tidy text file with only the necessary data in a structured format.

A day well spend :)

Merging three files into one (day_data.txt)

Merging three files into one (day_data.txt)

I give up

Flash Lite app with Shuriken component

Flash Lite app with Shuriken component

Part of the data I want to log and show are my activities. I’ve wrecked my brain trying to think of an easy but digital way of keeping track of my activities during the day. That data should consist of a time stamp and of course my activity. I imagined an application which would run either on my PDA or on my mobile phone. I would have to be really easy to work with as I’ll be using it for two whole weeks on a constant basis. I was thinking of a drop-down menu or combo-box which would hold all my possible activities (hmm, is that possible). Once I’d have selected an activity it would be stored on the device together with a time stamp indicating the current time. The next morning I would download the data from the device and integrate it with other data collected the previous day. Sounds simple right?

My first thought was Excel, as this is an easy way to store structured data and output it in different formats. But the power of my pocket Excel is limited and after some time I realized that you can’t use VB Script so that solution went down the drain.

By chance I realized that Flash (dear Flash) can also store persistent data. This wasn’t something I had worked with before but it sounded promising and I could use it on my Nokia. At first I developed a desktop version. When I got that working (sorting the collected data proved to be the most difficult task) I discovered that I couldn’t use the combo-box component on my Nokia, it wasn’t supported. But thankfully there are geniuses out there who made custom components for handheld devices! After I’d finally set them up other parts of my AS3 code weren’t working on Flash Lite, which supports AS2. But after a about a days work I can’t get the application to really store all the data. The sorting again is a problem (1, 10, 11, 2) pfffff. So this afternoon I made the very uncharacteristic decision to give up this application. Time is pressing. I still have to build… everything really.

So now I feel quite relieved and have decided I’ll use pencil, paper and my watch to collect my activities.