SolarPi – A Flask powered photovoltaic monitor

After collecting some photovoltaic data using PikoPy and a some readings from the residential meter it was time to put everything together. The data is collected by a couple of scripts triggered by a cronjob every five minutes.

$ crontab -l
*/5 * * * * python /home/solarpi/kostal_piko.py
*/5 * * * * python /home/solarpi/collect_meter.py
*/15 * * * * python /home/solarpi/collect_weather.py

The results are then written into a SQLite database. I chose SQLite over PostgreSQL for its lighter footprint on the Raspberry Pi. And since we will have no concurrent writes SQLite should be capable serving as a database backend.

The sole purpose of the web application is to fetch data from the database and render the results. I decided to use Flask, a microframework for Python based on Werkzeug and Jinja 2. For the Frontend I used the Bootstrap based SB Admin 2 Theme.

Initially I used SQLAlchemy as an Object Relational Mapper where I defined my models in Python:

class PVData(SurrogatePK, Model):
__tablename__ = 'pv_data'
id = Column(db.Integer(), nullable=False, primary_key=True)
created_at = Column(db.Text(), nullable=False, default=dt.datetime.utcnow)
dc_1_u = Column(db.Integer(), nullable=True)
dc_1_i = Column(db.Float(), nullable=True)
ac_1_u = Column(db.Integer(), nullable=True)
ac_1_p = Column(db.Integer(), nullable=True)
dc_2_u = Column(db.Integer(), nullable=True)
.
.
.
current_power = Column(db.Integer(), nullable=True)
daily_energy = Column(db.Float(), nullable=True)
total_energy = Column(db.Integer(), nullable=True)

While using an ORM is very helpful for CUD operation I decided to keep my read queries using raw SQL instead.

The main view is a dashboard presenting all kinds of interesting KPIs like yielded, imported, exported Energy and how much money was received for the sold energy:

SolarPi Dashboard

Furthermore there are detailed charts available for each day at a five minute interval, or the last 7 days and 30 days, respectively:

<a href="https://i2.wp.com/blog.tafkas.net/wp-content/uploads/SolarPiDailyChart.png”>SolarPi Daily Chart

The project is live at: http://solarpi.tafkas.net

The code is on Github at https://github.com/Tafkas/solarpi

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 viagra en ligne achat.com/Tafkas/KostalPikoPy

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.

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: