Public Transport Display on Raspberry Pi

Published on July 17, 2017

I hate missing my train or arriving at the station to find out that my train is being delayed. This happens often, because German public transports are so unpredictable ! Since I take the train every day for work, it is also a pain to check online every time I leave. I found a solution to this problem, using a Raspberry Pi.

The Solution

A Raspberry Pi computer shows in my corridor the departure times for the next trains or public transports (bus, etc.) for a set of chosen lines. Let me first show you how the solution looks like !

The display can show information about the next trains for various destinations. So this can be useful not only for me, but also for my girlfriend or our flatmate. By the way, this video shows only the first prototype, the software was quite improved afterwards.

If you take a Raspberry PI 3 you won’t need the wifi dongle :). The touchscreen constructor provides a pre-configured Raspbian image, to download on its website, so it works out-of-the-box !

Then I bought some on/off hook strips from Tesa to put it in my corridor ! Hiding a part of the cable behind the mirror, it looks like this:

Software Setup

Getting an official Deutsch Bahn API key is impossible if you are not a business company. So I used schiene, a tiny Python library for interacting with Bahn.de, developed by Kevin Kennell. Basically it crawls the bahn.de mobile website to retrieve the next time table. It is like an unofficial API. I wrote a small script that wraps around it and handles the presentation of the results (including incidents and delay) in the standard output with a periodic refresh of the results. It is available on my GitHub here.

This works for any connection in Germany. The script dbtime.py should be started with a few arguments, to specify the start and end stations of your trip(s). I’ve created a small bash script launcher.sh to start the program in the terminal of my Raspberry Pi with my desired connections:

So when I double-click launcher.desktop on the touchscreen, it will starts the program directly in a new terminal. It will display the next trains from Stuttgart to Karlsruhe with the little prefix ==KA==>, considering only direct connections (True). It looks like this:

If you add more trips, it will alternatively display each of them. The information is refreshed after a number of seconds (by default 45 seconds).

To make it look better on the touchscreen, I changed the police to DejaVu Sans Mono Book with size 17 and changed the default dimensions of the terminal. To do so, just add the following line in the file /usr/share/raspi-ui-overrides/applications/lxterminal.desktop

Exec=lxtermnal --geometry=37x15

At first I considered not having a GUI on the Touchscreen, and simply start the program in the console, but then I would not be able to shutdown the Raspberry Pi from the touchscreen anymore.

My friend Daniel is doing something similar, with a much fancier design, you can check it out here.