PikoPy: A python package for working with a Piko Inverter from Kostal

The first step of my plan, building a Raspberry Pi based photovoltaic monitoring solution, is finished. I created a python package that works with the Kostal Piko 5.5 inverter (and theoretically should work with other Kostal inverters as well) and offers a clean interface for accessing the data:

import pikopy

#create a new piko instance
p = Piko('host', 'username', 'password')

#get current power
print p.get_current_power()

#get voltage from string 1
print p.get_string1_voltage()

The next steps are collecting the data and processing it.

The library can be downloaded from https://pypi.python.org/pypi/pikopy
The code is on Github: https://github.com/Tafkas/KostalPikoPy

A Raspberry Pi photovoltaic monitoring solution

A friend of mine had a photovoltaic system installed on his rooftop last year. After a little talk I offered to setup a monitoring solution which provides him with the data in real-time. The current setup comes with an inverter by the company Kostal.

Kostal Piko 5. 5 Inverter

The Kostal Piko 5.5 runs an internal web server showing statistics like current power, daily energy, total energy plus specific information for each string. Unfortunately the web page and the shipped software look like they were designed in the late 90s and development has been abandoned:

Kostal Piko 5.5 Web Interface

The inverter itself logs data at either a 15 minutes interval for about 100 days or at an hour interval for about 400 days. At the current setting the 15 minutes interval has been selected. Furthermore my friend took notes irregularly from the built-in liquid-crystal display.

The plan was set. A Raspberry Pi would fetch data from the inverter, store it in a database and a web application would render the results to a browser:
SolarPi SetUp

Analyzing Sleep with Sleep Cycle App and R

I have been tracking my sleep for almost two years now using my Fitbit. I started with the Fitbit Ultra and then moved on the the Fitbit One after it came out. In October 2013 I found out about the Sleep Cycle app for the iPhone. For weeks, Sleep Cycle was listed as the best-selling health app in Germany, where currently (as of January 2014) it is in second place. The program promises, to wake you up in the morning without being tired. This is indeed possible if the alarm goes off in light sleep and not in deep sleep. After reading all the positive reviews on the AppStore I decided to give it a try.

The app promises to wake you up in a time frame up to 30 minutes prior to the alarm you set if it detects your movement in the morning. Even more important to me than the actual smart alarm feature was the possibility to collect some data while sleeping. In the morning you are presented with a chart of your sleep pattern of last night:
Sleep Cycle Screenshot of last night

The app also allows to export the database as a comma seprarated file containing: time you went to bed, time you woke up, sleep quality in %, wake up mood and user defined sleep notes. This gives you the opportunity to do some more analysis. I decided to fire up R and create my own charts.

So far I have used the app to track 100 nights of sleep and decided to peak into the data. Let’s take a look how long I slept each night:
Sleep Duration over time

It looks like the longer I slept the higher the sleep quality is. A scatter plot of the data gives:
Sleep time vs. sleep quality

The chart takes also the sleep notes into consideration. You can see clearly that sleeping away from home results in lower sleep quality. The same applies for exercising (note: I tagged a sleep with exercising when I worked out late in the evening). On the contrary taking a 3mg melatonin increased the sleep quality.

Averaging the sleep quality by month shows, that the January worse than the previous month. One explanation is a vacation I took, where I did not sleep so well at all.
Average sleep quality by month

The R code for the data wrangling and the charts:

Berlin Marathon 2014 Participants

After the 2013 Berlin Marathon sold out in less than four hours, the organizers decided to alter the registration process for 2014. First there was a pre-registration phase followed by a random selection from the pool of registrants to receive a spot. Those who were selected had to register until November 11th, 2013. Any spots that were not confirmed till the 11th would be offered to pre-registered candidates according to the order in which they were randomly selected.

At is a list of all registered participants of the event. Being curious how many runners are on the list I had two options: 1. going through the entire list and counting or 2. download the entire list and let the computer do the math. I chose the latter. If you checked the website already you saw that they present only parts of the list at a time a reload it asynchronously while you scroll down. I wrote a little python script that queries the data and saves the JSON response to a csv file.

Checking the file we see there are only 16,707 participants so far. Sure, there are spots sold to agencies or given to sponsors, but how many will be handed out in the second wave? Until they announce the results let’s look the data:

The distribution of the year of birth shows a bimodal pattern. Most runners are born in the late 60s or early 70s and then there is a second spike a around 1980.
Looking at the top 10 participating nations it is no surprise that Germany stands out by far. Followed by Great Britain and Denmark. The only non-European country in the top 10 are the United States of America:

The code for generating the images:

Update: The official registration period is over and 23,286 runners have signed up.

Five years of Weight Tracking

After I moved back from New Jersey in June 2008 I started to track my body weight more seriously. My routine usually consists of getting up and after finishing the morning bathroom I would step on my scale. That way I try to ensure that the condition for each weighing are as similar as possible. I recorded my weight on paper and eventually would put everything into a spreadsheet for further analysis.

In January 2011 I upgraded my bathroom scale to the Withings WiFi Body Scale. That way I could automate the process of tracking my weight by just stepping on it. No more writing on paper and eventually transferring everything into spreadsheets.

The people at Withings provide an API so external services could access your weight data. A nice way to get better charts is through Trendweight. Just link your withings account to the web site and they will generate nice JavaScript charts of your weight/body fat/lean mass. Another great feature is the export functionality where you can export a comma separated version of your data including trend values for total weight as well as fat %, fat mass, and lean mass.

Using the exported data we can fire up R to look at the data:

The generated chart shows my weight loss in 2009, where I started cycling again after years of absence. From my lowest in the Fall of 2009 I gradually gained weight throughout 2010, 2011 until 2012, where I hit the 100 kg mark around Christmas time:

Body Weight 2008 - 2013

That was enough and I decided it was time to start working out again. So far, I am at a good downwards trend. I have to keep up that momentum. My next goal is to get between 80 kg and 85 kg and then maintain my weight. The color of the dots reflect my body fat percentage and there seems to be a strong correlation between body fat and my actual weight:

Body Weight vs. Body Fat Percentage

Doing a simple linear regression gives us an adjusted R-squared of 0.8419

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:

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

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.


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