During our recent hackday I wanted to work on a project that utilized our public API to display football scores on a Raspberry Pi. Having worked in the technical services industry for the majority of my career I was interested in finding out what our customer’s experience was like building a basic client side prototype.
First things first, I needed to prepare a fresh Raspian Raspberry pi image. For those who haven’t done this before you can follow the instructions for your respective operating systems here.
I went with Wheezy because I knew the library I’d be using had worked on Wheezy before and I didn’t want to be troubleshooting dependency problems all day.
The Piface display has a nice packaged library that can be installed with the package manager. I wasn’t working in a virtual environment for this project so installing it this way was fine. I also had to enable the SPI interface for the screen to work after updating the OS. I usually just use the pi user for my projects but any sudoer would be fine.
sudo apt-get update && sudo apt-get upgrade -y
# - SPI
# - enable
At this point you could also enable ssh or a GUI login depending on your preferred way of working.
sudo reboot # to commit changes
sudo apt-get install python3-pifacecad -y
Once that was installed I pulled down my client library and installed dependencies.
git clone https://github.com/martyni/crowdscores_client.git
sudo pip-3.2 -r requirements.txt
I had written my client library on my laptop but, after having trouble with installing a geolocation library on the pi, I commented some of the dependencies out for this project as they weren’t really needed.
Next to authorize my requests I place a file in my user’s home directory called .crowdscores containing my API key.
echo “MY API KEY” > ~/.crowdscores
And to test this worked I ran my client script with an interactive python3 shell. If it works I should see my API key and be able to run some of the commands from the client.
python3 -i client.py
As the client library was working as expected I could concentrate on working on the code that controlled the display. There are 4 main parts to the script that controls it.
Screen Object – Directly controls what is displayed on the screen and formats the text so it gets shown correctly without any lingering “Ghost” characters or showing an ugly flashing cursor.
Game Object – This handles storing the score and team names of each game. It creates a screen instance for when it needs to display information.
Matches Object – The Matches object is responsible for interacting with the Crowdscores API and pulling down a list of matches to iterate over. It’s hard coded to check for matches that started within the last 3 hours although this was just an arbitrary figure I chose during the hack day to make sure that there were some games to display.
Main loop – It originally had some sleeps built in to display each of the scores for an arbitrary amount of time but as I finished this project with a couple of hours to spare I had some time to work on improving the project’s features.
As a bonus feature I wanted the scores to be read out loud, not just shown on the screen . After a quick google search, I came across a likely sounding library called pyttsx on an ActiveState blog post.
I couldn’t get it to pip install using pip-3.2 but worked fine installed as a python2 package.
sudo pip install pyttsx
By this time it was getting late in the day, so I opted to really put the hack in hackday and have the Game object write each game to a file then call a sys shell to run the “say.py” script (not exactly a best practises implementation). As the system call is blocking, and will wait for the say script to finish, I removed all of the previous sleep statements so the say script then became rate limiting step. Finally I needed to show the office my handiwork. Running the app is done by just running the screen code with a python3 prompt. If you’ve followed the instructions so far the one liner below should silently run it as log as your terminal sessions remains open.
python3 ~/crowdscores_client/screen.py >/dev/null &
First I demonstrated its application as a “wearable” using a mobile phone powerpack, some elastic bands and a pair of crowdscores branded gloves. Then showed its versatility by plugging it into a TV and having it announce the scores for all to hear.
I found that sometimes the pyttsx library wouldn’t work unless I had hard set the sound device as either HDMI or 3.5mm jack in the raspi-config settings.
# - either HDMI or 3mm Jack
Let me know if you have any thoughts or comments about this project via github. I don’t have any real plans to develop the screen or talking code further. I may make further changes to the API client code.