Putting it all together
This is the second part of a multi-part blog post on recreating an arena
hockey experience at home. If you got here without reading Part I you can find
it
here.
Part I TL;DR
In part 1 we covered the NHL APIs, which ones to use, how to find games, etc.
We also realized that an automated approach may not work due to TV delays and
such, so we came up with something a bit more manual. I may go back and update
my web app to include automated score tracking later. I mean how cool would that be to display the current score on my web app.
Equipment Used
- Raspberry Pi 3B+
- Raspberry Pi Jumper wires
- Altec Lansing LifeJacket 3s Bluetooth Speaker
- Red rotating beacon light
- AC/DC Control Relay
- Android Tablet
UPDATE: Turns out the bluetooth speaker is a bigger hassle than it is worth. The steps in this series continues to show how to work with it but I'll be trading mine out for a wired speaker.
For the Android Tablet, this can be any tablet or device you can get a web
browser on. I like the idea of the tablet as it makes it real easy to setup
and anyone in the room could use it. If you didn't read part 1, I chose the
Altec Lansing speaker merely based on the fact I already had one sitting
around that wasn't being used. You could use any compatible speaker it doesn't have to be a bluetooth one. But I do like the idea that the speaker can be setup anywhere in the room.
Programs! Get Your Programs Here!
Our raspberry Pi can support a number of different programming languages,
Java, Python, Node.js, Go, Bash, etc, but before we begin writing code
we need to pick one. Most of my professional career as a Developer was
spent writing Java code for both standalone and web applications. When I started doing more DevOps and systems related work I started using more shell scripting and Python. For this project I want something robust, fun, and easy to get started with.
I feel like this is a Pokemon moment, "I choose you Python" or more specifically
Flask.
Why Python/Flask?
Somewhere a long time ago I read some Python documentation that suggested when
you comment your code it should be done in the style of Monty Python, the
comedy group. While by today's standards that is probably a bad idea, I've
always enjoyed watching Monty Python and loved the idea of this so I use
Python whenever I can.
Ok so here is a more valid list of reasons:
- Python is easy to learn
- Most *nix systems, including our raspberry pi already has Python installed
- Python is an interpreted language so we don't have to compile it
- Python is a mature language and we can easily find support on forums, etc
- Freely available libraries, including the GPIO Zero library we will use to activate our goal light.
- Flask is a web application framework that can be easily customized to our needs.
- Flask has a built-in webserver we can use for development.
- Our application is static, meaning the content won't really change, Flask is perfect for small static apps.
What about Django you say. We could very well have used Django for this, it
is a very mature web framework with a lot of support, but my focus right now
is on time. We need to get something going quickly, and I know with a few
lines of code I can get a static Flask app up and going pretty quick.
Puck Drop
If you haven't done so already, you will need to setup your raspberry Pi. I'm
not going to go over how to put your raspberry pi together and do the initial
setup, but if you need them you can find them
here. Go
ahead and get the Raspbian OS installed, WiFi setup, etc. You can plug a
monitor, keyboard, mouse, etc in and work directly on the raspberryPi if you
wish or following these
instructions
you can setup a Remote Desktop (RDP) session, which I will be using in my
examples.
I prefer the RDP method as it allows me to use my MacBook to connect remotely
to the pi where my development environment and IDE will be installed at. If
you are going to hookup a mouse and keyboard directly to the raspberry pi,
just make sure you use USB devices and not bluetooth ones since our speaker
will be paired via bluetooth.
If you don't have a favorite IDE to use on the raspberry pi I would recommend
installing
VSCode as
your editor it has a lot of great extensions to make your life so much easier
and it is faster than Eclipse or IntelliJ and IMHO it is way better than the
Python editors Thonny or IDLE.
VSCode isn't in the package repository but you can use the web browser on your
raspberry pi to download it, just be sure to grab the 32-bit ARM .deb package
unless you know for sure you have a 64-bit OS on your raspberry pi.
Ok, let's get started!
Lets create a new directory for our application, we will call it 'nhl_webapp'.
VSCode allows us to open a terminal directly in the IDE which can be
faster to create the base files/folders on the command line.
mkdir -p nhl_webapp nhl_webapp/templates nhl_webapp/static
nhl_webapp/static/media nhl_webapp/libs
cd nhl_webapp
Initialize our project as a git project
git init
touch app.py .gitignore README.md
- app.py - will hold our main web application code
- .gitignore - Since I'll be using git to manage my code this allows me to omit certain files/folders from being checked in.
- README.md - Markdown file that will describe our project and tell others how to use it.
Next we will create our virtual environment and activate it. The virtual
environment will be used to install our dependencies to and run our
application.
python3 -m venv env
source env/bin/activate
Great, now lets install Flask, GPIOZero and their dependencies to our virtual
environment.
python -m pip install Flask gpiozero
We don't need to check in our entire virtual environment to git but we do
want to ensure we capture the requirements for others, so lets add the
`env/*` folder to our .gitignore and capture our requirements in a separate
file.
python -m pip freeze > requirements.txt
cat env/* > .gitignore
So now our file structure should look like the screenshot below.
Let's add some code and test that everything is working.
In VSCode open the app.py file and add the following code and save the file. If you don't want to type it out you can find the source on my github repo.
Now to test this basic application.
In the terminal window type the following to start the application. Because
Flask comes with a built-in dev web server, starting the app will also
launch the web server on its default port of 5000.
python3 app.py
If everything worked we should see the server startup.
Now let's see what the web app looks like in the web browser. Open Chromium
or whatever browser you have on the raspberry pi and go to the address of
http://localhost:5000 If all went well we should see the page with "Our NHL
WebApp" on it.
So now we have a functioning Flask web app, although at this point it is very basic and doesn't do anything other than print our text.
If you are using git and have a remote repository, now may be a good time to commit those changes. If you are unfamiliar with git there are a lot of free resources on the web to learn git. Git is a great way to keep track of all your changes in a code repository so if you ever accidentally delete a file or make a change that breaks something it is easy to go back to a previous version of your code. It also allows you to see what you changed and when.
In our next steps we will add some colors, graphics, and a button to activate the goal horn and goal light to tie it all together. So this is a good time to search the web for some free hockey graphics, team logos, and an mp4 of the goal horn.
No comments:
Post a Comment