Downloading Fitbit Data using Google Spreadsheets

One of the most important features in quantified self is the ability to export your data in an open format. Fitbit lets you download your personal data if you subscribe to a premium membership. Alternatively they provide an API at dev.fitbit.com/ that allows developers to interact with Fitbit data in their own applications, products and services.

In a blog post at quantifiedself.com Mark Levitt shows a way how to export your Fitbit data into Google Spreadsheets. I explored to API myself adding and removing some of the fields to get more insights to the data.

In a future post I will delve into the data in order to understand some of my own physical activity patterns.

Update 14 November 2014: I removed the Active Score since it has been dropped by the Fitbit API

Getting the Raspberry Pi temperature from the command-line

If you are overclocking your Raspberry Pi or you just curious how hot this little guy gets, there are two ways to get the internal temperature. Assuming you are running Raspbian as your operating system.

Method 1:
$ /opt/vc/bin/vcgencmd measure_temp
This gives you the temperate in in degrees Celsius: temp=54.1'C

Method 2:
If you need the temperature to be more precise (e.g. storing it in an database or for further processing) use the following command.
$ cat /sys/class/thermal/thermal_zone0/temp
This will give you the temperature in Millidegrees Celsius: 54072

From my personal experience the temperature ranges from about 50°C to 55°C and I have never seen my Raspberry Pi running over 58°C.

RaspberryPi Temperature

Using SSH Public Key Authentication on the Raspberry Pi

If you log into your Raspberry Pi using ssh it will prompt you for a password. Having to do this multiple times a days this is very annoying. To ease the pain, and enhance security, you can use public key authentication instead. Therefor you create a pair of keys on your client, and store the public key on your Raspberry Pi. Then you set up an authentication by key. Afterwards the user can login into the Raspberry Pi using his private key.

Creating the keys

The first step is to create a pair of keys on the client using ssh-keygen. For the RSA key we choose 2048 bits:
Generate SSH Key Pair

This will generate a pair of keys and store them in the folder ~/.ssh/:
Content of dot SSH Folder

I have not set a passphrase. Actually a passphrase is good idea as it gives more security yet I do not want to enter the passphrase everytime I access the key.

Store public key on the Raspberry Pi

For the last time you have to log into your Raspberry Pi using password. Once logged in you copy your public key from the client into ~/.ssh/authorized_keys:

The file should look similar to:
authorized_keys

Optional: Deactivate password authentication

In order to increase the security you can disable the password authentication. Be aware that you cannot login into your Raspberry Pi over SSH using a password.

In your /etc/ssh_config set PasswordAuthentication no and restart your ssh daemon. Try to connect to the Raspberry Pi after the ssh daemon has restarted before you end your current session. In case something goes wrong you will not be able to connect again.

Charting Sunrise and Sunset in Highcharts

In order to visually enhance my temperature logging I added some Javascript that computes sunrise and sunset for the 24h, 28h, weekly and monthly chart. Then I use this information to plot vertical bands on the chart indicating the effects of the sun on temperatures (and humidities):

To add the bands to your Highchart just get the sunrise and sunset value for a particular day and push it on the xAxis.plotbands.

The resulting chart:

Gathering and Charting Temperatures using RRDTool and Highcharts

tl;dr Checkout the charts on my RaspberryPi

For quite a long time I was looking for a way to monitor and record th temperature and humidity at my apartment. What was missing was a convenient, preferably wireless solution. After receiving my RaspberryPi I started to look into that more intensively.

USB-WDE1 Receiver

The USB Weather Data Receiver USB-WDE1 wirelessly receives data from various weather sensors of ELV at 868 MHz. The receiver is connected to a USB port on the computer, so no additional power supply is required. The data is transmitted via a simple serial ASCII protocol, which is well documented by ELV. The RasberryPi running Raspbian is used for the data acquisition allowing very little power consumption while being completely flexible.

The USB interface of the USB WDE1 is realized by the USB-serial converter CP 2102 of Silicon Labs. The responsible kernel module CP2101 for accessing the device is included in any modern Linux distribution. When connecting the USB-WDE1 should appear in the system once the appropriate messages:

$ Dmesg
usb 1-3.1: Product: ELV USB WDE1 weather data receiver
usb 1-3.1: Manufacturer: Silicon Labs

The udev subsystem then also creates a corresponding device file, usually is the / dev/ttyUSB0. This device behaves as seen by a Linux application program such as a serial port and therefore can be accessed with any terminal program such as minicom. If you connect other USB-to-serial converter to the RaspberryPi, the device can also be called /dev/ttyUSB1 or similar. It is important to set the baud rate to 9600 bits/s.

A simple and universal way to output the data supplied by the receiver on the terminal provides to tool socat, which should also be part of any Linux distribution. You may have to re-install it via the package manager. Using

socat / dev/ttyUSB0, B9600 STDOUT
$1;1;;21,6;9,5;21,6;21,3;21,1;19,2;;;58;78;58;59;42;53;;;;;;;;0
$1;1;;21,6;9,5;21,6;21,3;21,1;19,3;;;58;78;58;59;42;53;;;;;;;;0
$1;1;;21,6;9,5;21,6;21,3;21,1;19,3;;;58;78;58;59;42;53;;;;;;;;0

Each line represents a complete data set consisting of 25 semicolon-separated fields. The first three fields are immutable, followed by the measured temperature (°C) of eight sensors and their humidity values​(%). The next fields show temperature (° C), humidity (%), wind speed (km / h), precipitation (rocker beats) and rain sensor (0/1) of the combination sensors. Since I do not have a combination sensor I won’t focus on those values. The last field with the fixed value of 0 indicates the end of the record.

Gathering Data With RRDtool

Now that I could receive temperature, as well as humidity, from the sensors I needed to come up with a way to store the information. For this I chose the RRDTool package to manage the data. Its a circular (RR in RRD stands for Round Robin) database that lets you store a predefined amount of data. After initial creation of the DB it is as big as it will ever get and just contains “unknown” data. This is a widely used open source package that has a bit of a steep learning curve on some of its aspects but gives you everything for functionality. It works on multiple platforms including Linux and Windows and has a large, active support community.

The ‘rrdtool create’ command is used to setup the database. Here’s the tmpdb.sh bash script I used to set it up:

Once I had setup the database I needed something to read all of the sensors every 5 minutes and place the data in the DB. For this I run a little script:

The script remains in an infinite loop while socat receives data from the sensors. After a complete line has been received, rrdtool updates database.

Graphs

Once you have some data in your temperatures.rrd database it is time to create some charts. rrdtool comes with a built-in graphics engine that can be utilized to easily create some charts. One drawback though is, that the generated charts do not look very appealing:

Another reason to avoid pre-generated graphics is that the creating process takes lots of cpu power, where with the RaspberryPi this is a very limited resource. In the beginning I created the charts every five minutes. Later I changed the schedule to every hour for the periods of month and year. Even then I was not totally happy with the result. After looking for alternatives on the Internet I stumbled upon Highcharts, a charting library written in pure JavaScript. This approach delegates the chart generating to the client side. Therefor I export data from the RRDTool to an xml file:

This results in a bunch of xml files. I use the jQuery.get method to get the contents of the xml files. In the success callback function, I parse the returned values, add the results to the series members of the options object, and create the chart:

The resulting chart:
Last weeks temperature charted with HighCharts

Setting up Dynamic DNS on the Raspberry Pi

Once you have set up your Raspberry Pi chances are that you want to access it from remote machine or host a little web site on it. The problem is that your provider usually gives you a dynamic IP, which changes every time you connect to the Internet. In Germany most (A|V)DSL provider reset your connection every 24h. The solution for this is a dynamic DNS (DDNS), which automatically updates the name server in the Domain Name System (DNS). Here is how you set it up using the provider DynDNS NoIP.

The first step is obtaining a free subdomain from the provider. Therefor you register an account at NoIP.com and check the little box Create my hostname later. Once you activated the account and logged into the website click on *Hosts/Redirects*. To create a new subdomain click on Add a Host and get creative. As host type choose DNS Host (A)

After setting up your host you can update your IP using curl:

curl -u $USERNAME:$PASSWORD "http://dynupdate.no-ip.com/nic/update?hostname=$HOSTNAME"

To update your IP everytime you boot your Raspberry Pi you can set-up a crontab: crontab -e

@reboot curl -u $USERNAME:$PASSWORD "http://dynupdate.no-ip.com/nic/update?hostname=$HOSTNAME"

After you have successfully updated your IP address you have to setup your router to open the desired ports and forward requests to the Raspberry Pi in your local network.

Update: DynDNS no longer offerering free accounts.

A (not so) safe betting strategy for winning at roulette

One time I was on a trip to Budapest with a couple of friends. While roaming the streets we were passing by a casino and my friend insisted that there was a perfect strategy that would only lead to winning at roulette tables. Curious as I was I had him explain his theory. The system basically works as follows:

First, you place a coin on red. If red wins, take your winning and start over. Otherwise, you double your bet after every loss, so that the first win would recover all previous losses plus win a profit equal to the original stake. If there were no constraints this could actually work. I usually get suspicious when I hear “guaranteed wins” in the context of gambling. My first doubt was, that the chance of getting either red or black were not equally fifty percent since there was also the zero. Another thing is that at a roulette tables there is usually a limit.

I thought the best way to convince my friend that his system was not so perfect as he thought, was to simulate the whole process and show him the outcome.

Our gambler starts with a budget of 1000 coins and bets 1 coin initially. He also has a winning target, that if he reaches it, he withdraws from the table and goes home happily. The table limit is 1200 coins per bet. For simplicity I also assume that if zero comes up it counts as a lost bet.

Roulette Martingale

The graph shows clearly that the higher the gambler sets it target the lower is the probability of reaching it. If you want to play with the system, alter some parameters or extend it to a different betting strategy here is my code:

Results of the St. Pat’s 10 Miler and 5K

Recently I ran the St. Pat’s 10 Miler in Atlantic City, Nj. It was my first official running event ever and I enjoyed it lot.

Shortly after the race the official results have been posted on the Internet. The data did not only include the number and times of the participants but also gender and age. Looking at the finisher time distribution it shows that most runners finished at around 90 minutes:

Finisher Time Distribution of the St. Pat's 10 Miler and 5K 2008

How does age affect the finishing time?

Finisher Time by Gender and Age of the St. Pat's 10 Miler and 5K 2008

The code to generate the images: