In this project you will collect data from the Sense HAT’s sensors and log it to a file. Then you will use the PyGal module to display that data as a line graph.
You will be writing code in the Python programming language, which you may have learnt in the Python module.
Step 1: Logging the temperature
First let’s log the temperature to a file every 5 seconds. You can use the emulator to change the temperature.
Open the Weather Logger Starter Trinket: jumpto.cc/weather-go.
Some files and code have been added for you.
collect.py. This is where you’ll write the code to collect the temperature data. Let’s open the
weather.txtfile and write the temperature to it every five seconds.
Add the highlighted code to
Opening the file with
ameans that data will be appended to the end of the file.
Writing a newline character
\nputs each temperature reading on its own line.
weather.txt. It should be empty. This is where the data will be stored.
Now click Run. Use the temperature slider on the emulator to change the temperature. You should see the temperature reading added to the end of
weather.txtevery five seconds.
Remember that the emulator tries to behave like a real Sense HAT so you won’t see exactly the same reading even if you don’t change the emulator.
Click the square
Stopbutton when you have finished collecting data.
You can highlight the data in
weather.txtand delete it if you want to start collecting new temperature data. Collect around 10 temperature readings.
Step 2: Creating a temperature graph
Now you’ve collected some temperature data let’s show it on a line graph.
main.py. At the moment it imports the
collect.pyscript that you wrote to collect the temperature data. Now change it so that it imports the
Comment out the
import collectline by adding a
#at the beginning and remove the
#from the beginning of the
main.pyshould now look like this:
display.pyto work on the code for displaying the data. You’ll be using the Pygal library which has been included for you.
Add the highlighted code to read the values from the
weather.txtfile into a list called
Run the code to see the values printed out. You can remove the
Now let’s add the code to create a line graph from the data using Pygal.
Pygal automatically creates labels for the y axis from the data. Let’s add a title and labels for the x axis. We can number the readings starting from 1. We need to add one to the length of the temperature list so that range will return a list of numbers that goes from 1 up to the length of the list.
Add the highlighted code to add a title and labels:
Challenge: Simulate different weather conditions
Can you collect and display weather for different weather conditions? Try a hot summer’s day where the temperature is over 30 degrees C and a cold winter where the temperature drops below freezing.
Remember that you’ll need to swap between including
You can just highlight and delete the data in
weather.txt to clear the data. Or you could create new files to store different readings.
Challenge: Record humidity or pressure
Humidity is high when there is a lot of moisture in the air. High pressure is associated with fine weather while low pressure means cloudy, rainy or snowy weather.
Duplicate your project and change it so that it records humidity or pressure instead of temperature.