One way to get your kids ready to leave for school (and get them into coding)

Michael Klein
6 min readFeb 13, 2021

IMHO, one cool thing about kids is their perception of time. Sometimes, parents/guardians have to manage that, especially to speed things up in the morning to get them ready to leave for school.

To motivate my son for coding on the one hand, to support him and to start the day with less discussions on the other, I thought about how to illustrate the current time to commute to school using a Sense HAT on a Raspberry Pi.

Here’s the basic idea:

  • Show a green light when there’s enough time to finish breakfast.
  • Indicate a yellow warning when to get dressed and brush teeth.
  • Signal a red light when to seriously hurry up.

One additional aspect to consider is current outside temperature — You don’t want a last minute surprise realizing that your car windows need a time-consuming defrost, delaying departure further.

So, on top of green, yellow, and red, a white indicator for icy conditions can be helpful — Here’s the set of graphic indicators my son and I came up with where the white background represents freezing degrees:

Pixel Art for Sense HAT

This article highlights the basic building blocks to implement the underlying idea. As an illustration it doesn’t claim to be complete, e.g. error handling is missing. So, it does not outline every single detail; it’s not an all-in, production-ready, out-of-the-box, downloadable solution.

What’s needed?

  • A Raspberry Pi with Internet access and Python 3 installed
  • A Sense HAT (a breadboard with LEDs is fine, too) connected to the Pi
  • A Google Cloud account and a Google Maps API key
  • An OpenWeather account with an API key

How to retrieve the current commute time via API?

Google Directions API is the way to go. All you need to know to get started is nicely described and illustrated here. The following assumes that you have followed the steps to set up such a Google Cloud project and API key.

WARNING: Calls to Google API are subject to charge! There’s a free credit which is enough for our illustration here (at the time of writing). I’m not responsible for any costs that may occur to you! Refer to Google documentation for details.

With that in place, very little bash scripting, and a bit of Python code we can retrieve the time in seconds it takes to commute:

  1 #!/bin/bash
2
3 HOME=<coordinates-of-home-address>
4 SCHOOL=<coordinates-of-school-address>
5 GM_API_KEY=<your-Google-Maps-API-key>
6 URL="https://maps.googleapis.com/maps/api/directions/json?origin=${HOME}&destination=${SCHOOL}&departure_time=now&key=${GM_API_KEY}"
7 JSON=commute-info.json
8
9 curl -s $URL > $JSON
10 cat $JSON | python3 -c "import sys, json; print(json.load(sys.stdin)['routes'][0]['legs'][0]['duration_in_traffic']['value'])"

What above code snippet does is that it queries the Google Directions API (in line 9) to retrieve the current duration in traffic from HOME to SCHOOL (lines 3 and 4). Those are latitude/longitude coordinates which can easily be retrieved e.g. by right-clicking on the address in Google Maps.

Our API call response is in JSON format; some Python (in line 10) is used to extract the information we’re looking for in the “value” field (excerpt):

  1 {
2 "routes" : [
3 {
4 "copyrights" : "Map data ©2021",
5 "legs" : [
6 {
7 "duration_in_traffic" : {
8 "text" : "34 mins",
9 "value" : 2013
10 }
11 }
12 ]
13 }
14 ]
15 }

The value returned represents commute time in seconds which we’ll use later again.

How to get the current outside temperature via API?

Some time ago, I stumbled upon OpenWeatherMap. Their Weather API is easy going, this page will get you started with everything.

With your API key at hand first, you need to find your location’s City ID here next.

WARNING: Calls to OpenWeather API are subject to charge! There’s a free credit which is enough for our illustration here (at the time of writing). I’m not responsible for any costs that may occur to you! Refer to OpenWeather documentation for details.

Again using a little bash and Python, we can get the current outside temperature in degrees Celsius:

  1 #!/bin/bash
2
3 CITY_ID=<your-home-town>
4 OW_API_KEY=<your-OpenWeatherMap-API-KEY>
5 URL="https://api.openweathermap.org/data/2.5/weather?q=${CITY_ID}&units=metric&appid=${OW_API_KEY}"
6 JSON=weather-info.json
7
8 curl -s $URL > $JSON
9 cat $JSON | python3 -c "import sys, json; print(json.load(sys.stdin)['main']['temp'])"

For some reason, OpenWeather’s API key is referred to as APP ID (line 5). In line 8 above, we query the API and one line later, we extract the temperature value.

The data exchange format is also JSON. Here’s an actual excerpt:

  1 {
2 "main": {
3 "temp": 2.01
4 }
5 }

Let’s keep this temperature in mind for later. (The actual temperature at the time of writing was much lower.)

Now, that we have all input available to determine what we would like to show on the Raspberry Pi’s Sense HAT, let’s look at how to do that next.

How to display images on a Sense HAT?

There’s an excellent Getting Started guide from the Raspberry Pi Foundation. Among others, that guide describes how to use (again very little) Python to display simple images on the Sense HAT. This device comes with an 8x8 pixel panel that can be used to illustrate what we want.

Let’s say, we have frozen windows and heavy traffic outside. Time for everyone to get dressed! In that case, we could draw a red dot on white background on the Sense HAT (source code adapted from aforementioned link):

  1 from sense_hat import SenseHat
2
3 sense = SenseHat()
4
5 # Define colors
6 r = (255, 0, 0) # Red
7 w = (255, 255, 255) # White
8
9 # Set up where each color will display
10 # Let's make a red dot on a white background
11 icy_and_late = [
12 w, w, w, w, w, w, w, w,
13 w, w, w, r, r, w, w, w,
14 w, w, r, r, r, r, w, w,
15 w, r, r, r, r, r, r, w,
16 w, r, r, r, r, r, r, w,
17 w, w, r, r, r, r, w, w,
18 w, w, w, r, r, w, w, w,
19 w, w, w, w, w, w, w, w,
20 ]
21
22 # Display these colors on the LED matrix
23 sense.set_pixels(icy_and_late)

I leave it up to the reader’s imagination to come up with other pixel art. Ours is further up, feel free to make use of it.

Putting it together

With current time to commute and temperature known, it is straight-forward to implement conditions, e.g. using if-then in a bash script, to invoke the appropriate Python code to display our lights.

An example of a decision matrix looks as follows:

+--------------------+----------------------+------------------+
| Time (in Minutes) | Temp. (in Celsius) | Indication |
+--------------------+----------------------+------------------+
| < 40 | > 2 | Green on Black |
| > 39, but < 50 | > 2 | Yellow on Black |
| > 49 | > 2 | Red on Black |
| < 40 | < 3 | Green on White |
| > 39, but < 50 | < 3 | Yellow on White |
| > 49 | < 3 | Red on White |
+--------------------+----------------------+------------------+

Last, the only remaining thing to do is to configure a morning schedule, probably via a cronjob entry, to trigger the lights:

*/5 7-8 * * 1-5 /home/pi/bin/indicate-conditions-to-school.sh
1 9 * * 1-5 /home/pi/bin/lights-off.sh

The first crontab entry above runs (roughly speaking) every 5 minutes, every Monday through Friday between 7–9AM, to indicate above illustrated conditions to school. Shortly after 9AM on those days, the second entry turns the lights off again.

Conclusion

Especially implementing those if-then clauses, drawing simple 8x8 smileys, and displaying those self-made images on the Pi’s Sense HAT was a fun and great way for my kid to get introduced to simple programming.

It’s been running at our home for over a year, hassle-free and low-energy consuming.

No more pushing to get dressed :-)

Disclaimer: I’m not associated with any of the services or brands mentioned above. Again, I’m not responsible for any costs or any damage that may occur. Comes as-is, no warranty, no guarantee, no support, no whatsoever.

--

--

Michael Klein
0 Followers

Dad, Husband, Gen Xer, and IT guy who grew up with a C64