View local UK train departures in real time using a Raspberry Pi and your monitor of choice.
Another computer connected to your network. We’ll refer to this as “your usual computer”, to distinguish it from the Raspberry Pi computer that you are setting up as a train timetable display.
This project should run on any Raspberry Pi computer. For this tutorial, we’ll be using a Raspberry Pi Zero 2 W, and with this model you will need a micro USB power supply and a mini-HDMI-to-standard-HDMI cable. Non-Zero models will require a micro USB power supply (or USB-C for Raspberry Pi 4), and a standard-HDMI-to-HDMI cable instead.
Power supplies come in a variety of formats, and you may find that an unofficial model such as a phone charger won’t be powerful enough for your Raspberry Pi. For ease and reliability, we offer affordable official USB-C and micro USB power supplies in a variety of regional formats. If you plan to use your own power supply, you’ll see a lightning bolt in the top right corner of your screen if it’s not supplying enough power to the computer.
We’re going to use an application called Raspberry Pi Imager to write Raspberry Pi OS to our microSD card. Raspberry Pi Imager is available for free for Windows, macOS, Ubuntu for x86, and Raspberry Pi OS. You can download it to your usual computer here.
Open Raspberry Pi Imager, connect an SD card adapter to your computer, and insert your microSD card into it.
In Raspberry Pi Imager:
CHOOSE OS: the latest recommended version of Raspberry Pi OS will be top of the list. Select this option.
Open the advanced menu: click the button with the cogwheel icon in the bottom right corner.
Set hostname: give your Raspberry Pi a name so you can find it on your network. For this tutorial, we’re going to name it “traintimes”.
Enable SSH: check the Enable SSH box and set a username and strong memorable password.
Configure wireless LAN: enter the SSID and password for your network.
Select SAVE to close the advanced menu.
CHOOSE STORAGE: select your microSD card.
WRITE: lastly, click to write Raspberry Pi OS to your microSD card.
Once complete, you can remove your microSD card from your SD card adapter and insert it into your Raspberry Pi. Connect your Raspberry Pi to your monitor and then, lastly, connect the power cable.
All UK train stations have a unique code. You can find the code for your nearest, or favourite, one on the National Rail website. For this tutorial, we’ll be using CBG, the code for Cambridge station.
Check the code for your station by opening a web browser on any device and entering the following URL, replacing “CBG” at the end with your station’s code:http://realtime.nationalrail.co.uk/ldbcis/departures.aspx?u=039B1CD1-14D4-4CB9-83B1-A84CC3AEDF83&crs=CBG
You should now see the real-time departure board for your station. If not, double-check that you’ve correctly entered the URL above and the code for your station.
We’re going to SSH into your Raspberry Pi. SSH allows you to wirelessly connect to your Raspberry Pi, eliminating the need for a keyboard and mouse. It’s perfect if your Raspberry Pi is located in a hard-to-reach location.
Open Terminal on your computer and type the following, replacing “username” with the username you chose in Raspberry Pi Imager when you set up your microSD card, and “traintimes” with the hostname you chose:
ssh username@traintimes.local
Hit Enter. When asked for your password, use the password you created in Raspberry Pi Imager.
Next, we’re going to make sure Raspberry Pi OS is up-to-date by running the following:
sudo apt update && sudo apt upgrade -y
We’re going to write a small script that will run whenever your Raspberry Pi is powered on. This will save you having to follow this tutorial all over again any time your Raspberry Pi loses power.
In Terminal, type:
nano traintimes
This will create a new file called “traintimes”.
Next, we need to write the contents of this file. To do so, type the following, changing the URL so that it ends with the code for your station instead of “CBG”:
sleep 5
chromium-browser --kiosk 'https://realtime.nationalrail.co.uk/ldbcis/departures.aspx?u=039B1CD1-14D4-4CB9-83B1-A84CC3AEDF83&crs=CBG'
The sleep
function allows your Raspberry Pi time to connect to the internet before launching the browser. You can change this number to reflect the speed of your own network.
Save and close the file, using ^X to exit and Y to save and ENTER to confirm.
Next, we need to edit a file called autostart
to tell your Raspberry Pi to run your script on startup. To open this file, type:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
At the end of this file, add the following, again replacing “train” with your chosen username:
@xset s off
@xset -dpms
@bash /home/train/traintimes &
Save and close the file, using ^X to exit and Y to save and ENTER to confirm.
Finally, type:
sudo reboot
Your Raspberry Pi will reboot and the timetable should display after the wait time you specified using the sleep
function.
For support with official Raspberry Pi products, please visit the Raspberry Pi Forums.