One of the cool things about web APIs (see my post for what a web API is) is that useful data is made available for you to hack around with. I found out that Transport for London has an API for their latest bus arrivals from this example here, which I’ve edited a bit to work with the AndyPi or compatible HD44780 LCDs. Being a Northerner I get a bit annoyed about everything being London-centric but its a cool example and hopefully I will get round to looking at the NextBus API soon, which is UK wide.

You can see my repo at GitHub which is for AndyPi or directly connected HD44780 LCDs (You’ll need a potentiometer and you should check out my instructions for the wiring, or download the original if you have a i2c connected LCD. Look in the AndyPi_LCD.py file, near the top you can setup which GPIO pins on the pi interface to which LCD pins.

Basically, it seems like all the London buses are GPS tracked and upload their location data to a central database. You call the web API with a location number of the bus stop you are at, and it gives you the next few numbers and how many minutes until they arrive. The location number is located on the bus stops themselves, and you can text a number to get updates back – I suspect they have a SMS server which interfaces with this API – but we are going to use a Raspberry Pi and an LCD instead. Install it somewhere near your door, and you can have your Pi giving you live updates of when the next bus is coming past your nearest stop!

Step 1: Choose location. You don’t have to run around to the bus stop to get the location number – included in the repo is a csv file of all the stops in TfL’s database with their corresponding codes. If you want to search it, you can use the in-built function to return a python dictionary with bus stop codes, let’s search for Lewisham:

>>> from TfLAPI import *
>>> tfl = TfLBusArrivalsAPI()
>>> location="LEWISHAM"
>>> tfl.searchBusStop(location)
{'LEWISHAM COLLEGE': '50064', 'BROMLEY ROAD / LEWISHAM TOWN HALL': '48601', 'LEWISHAM CLOCK TOWER': '74304', 'LEWISHAM STATION # [DLR]': '76375', 'LEWISHAM STATION #': '51090', 'LEWISHAM CENTRE': '72722', 'LEWISHAM ROAD': '57375', 'LEWISHAM POLICE STATION': '51994', 'CATFORD ROAD / LEWISHAM TOWN HALL': '77541', 'LEWISHAM PARK': '77489', 'LEWISHAM HOSPITAL': '50838', 'HAIL AND RIDE LEWISHAM HILL': '49619', 'CANADIAN AVENUE / LEWISHAM TOWN HALL': '77072', 'SHARDELOES ROAD / LEWISHAM WAY': '54369', 'LEWISHAM FIRE STATION': '56493'}


We’ll chose Lewisham Hospital (because, despite being a true Northerner that’s where I was actually born…), that’s code 50838.

You can play around with next-bus.py, but basically it sends a bus stop location number to the web API, and gets a list of the next few buses and their time to arrival. The line which does this is:

jsonObject = tfl.returnTfLJSON(bus_stop_code)

That’s much easier than having to set the URL up correctly and then make a web request – all thanks to the created of the TfLAPI wrapper. The rest of the code is processing the response stored in jsonObejct, and displaying these on the AndyPi LCD. When it reaches the end of the list of buses it makes the web API call again, for updated timing.