Creative learning with data. Calibrating altitude sensor with weather data

In today's topic I want to take you on a small journey. A journey on how you can use your creativity when going through an online resource, trying to learn something new. I am doing that a lot, especially when it is related to coding. There are roughly three methods you can go through a resource:

  1. Read the title, skip the text, copy-paste the code and hope it works. If not, find another resource with a similar title and repeat. If it works, go on Twitter and brag about your smartass-esness.
  2. Actually read the text. Try to understand the main concepts and implement these in your project using the code as a reference.
  3. Go full on understand-concept-mode by clicking on each reference link to the point you either forget why you came here in the first place. Or, end up starting a new project on a whole different topic. Five levels deep into the link references where you got inspired by something else.

This is the point where you expect me to elaborate on my personal preference and experience. Well, you are wrong; I am no different then you when it comes to reading resources.

Instead, I woud like to add a method to this list. A plot twist: don't worry which of the three methods is best. All of them have advantages. All of them are fine for any reason you have chosen at that specific moment. Except the first one, that's just a toxic paradox. For me at least. I can get very angry with myself for not understanding why something is not working. While I am putting literally ZERO effort to understand it. Oh shit.. now I did elaborate on my preference. Sorry, back to the topic.

Don't worry about the method. Instead, try to extend the resource with an extra feature, functionality, concept or whatever in your own project. By adding something new, you actually force yourself to interact with the code you just copied. Chances are, you will have to refactored bits and pieces. If you didn't understand the main concept you will end up braking stuff. Which is fine, because you learn the most by making mistakes and fixing them.

Just to lead by example, I will discuss a very simple example.

The beginning of the journey

So, where do we begin? At home, with a Raspberry Pi, some jumper wires and a BMP280 environmental sensor. The sensor measures barometric pressure, temperature and altitude. It is a very simple project for people starting with programming, Raspberry Pi, electronics and such. If you are interested in the technical stuff and actually setting up this system, check out my extensive github repository for that.

The conflict in the journey

What makes journey's interesting? The conflicts! Unexpected things that go wrong or require extra effort. So what's the conflict here? Well, there is some fake news going on in this sensor. It 'measures' altitude. See the quotes I put there? Yeah, you can't measure altitude. It is derived from the measured pressure, and the sea level pressure. The documentation of the sensor states: "Look at your local weather report for a pressure at sea level reading and set the seaLevelhPA property". So, the sensor has a property that needs to be set manually in order to get a correct altitude reading. Cool. However, pressure is not constant. It changes, just like the weather does. That's why people are interested in measuring the pressure. We will have to update our sea level pressure property regularly to maintain the accuracy of our altitude.

Now, we don't want to do things manually, do we? No. We are lazy. We are developers. We have to automate stuff. And voila, we have our first conflict on the journey. How do we automate sensor calibration?

I hopped on the Internet and did some research on weather data and the sea level pressure. I quickly stumbled on an API of the Dutch Metrological institute. Dutch government and open data are making some serious progress on the data awesomeness scale. All free of charge.

Open data awesomeness scale (By Janneke)

This API gives us 10-minute data from the closest weather station for a location of choice. Including the sea level pressure we are looking for. Perfect! Now all we need to do is built in a get request with a location parameter. Read out the data and set our property.

Are we there yet? No, another challenge pops-up. The API has a request limit per day. In case we are interested in reading out the sensor every few seconds, we will quickly run out of requests. Besides, with the 10 minute data update there is no point in requesting the weather very few seconds.

We roll up our sleeves and our fingers start typing to solve this new challenge. We do some more research. How can we delay a function in a Python script that runs continuously? We decide as a threading function. We just learned on the Internet that threading allows us to execute a function with a delay. We decide to apply our newly acquired knowledge by refactoring our get request to a threading function. We brake some code in the proces, swear a couple of times... but we succeed. Now we can measure our temperature and pressure every few seconds in real time, while the sea level pressure for sensor calibration is update every 10 minutes.

The happy ending of the journey

After a long journey we finally reach the ending. With some scratches and blisters on the fingers that have typed the extra lines of code. We have successfully extended an existing resource with new features. This might have been your first implementation of a get request. First time using an API or first time using threading in Python. And there are more benefits to this method:

  • You don't need to feel stupid for just copying and pasting code
  • You had a specific problem to solve which gave you a clear scope
  • You prevented diving in too much details about the concept you are trying to learn. Only focused on the elements that are in scope
  • You have utilized your creativity
  • You have actually earned your right to brag on Twitter

Did you like the illustrations? You can have your own as well! Janneke would be happy to make them for you