Raspberry Pi Weather Station using the Sense HAT
In this tutorial we will be showing you how to setup a basic Raspberry Pi weather station by utilising the Sense HAT. This is the perfect little hobby project for anyone wishing to start monitoring the weather conditions in a certain room, outside or anywhere you would like.
For anyone who doesn’t know, the Sense HAT is a fantastic piece of equipment that comes with a large abundance of sensors all in a single package. It’s the easiest way of adding a ton of sensors to the Pi without needing to do any extra circuitry.
This weather station tutorial will show you how to setup the sense HAT software itself and how to retrieve the data from its three primary sensors, those being the temperature, humidity and pressure sensors. We will also briefly touch on how to write text to the LED Matrix as we will use this as a way of displaying your sensor data.
We will also go into setting up your Raspberry Pi weather station so that the data is stored on Initial States Data analytics service, this will allow us to stream the data directly to there web interface and view all the data in pretty graph.
This tutorial will make use of a large amount of Python code but luckily it’s all pretty basic so you shouldn’t have too many problems following it.
You will need a sense HAT and a few other bits of equipment to be able to complete this tutorial.
Raspberry Pi 2 or 3
Network Connection (The Pi 3 has WiFi inbuilt)
Getting started with the Sense HAT
Before you get started with this tutorial, make sure that you have correctly placed the Sense HAT on the GPIO pins on the Raspberry Pi. It’s an incredibly easy installation and shouldn’t require any extra fiddling once installed correctly.
1. Before we get started, we need to run the following commands to ensure that the Raspberry Pi is running the latest software.
sudo apt-get update sudo apt-get upgrade
2. With the Raspberry Pi up to date we now need to install the sense hat software package, this provides all the libraries we need to interact with the Sense HAT.
sudo apt-get install sense-hat sudo reboot
3. Now we have the software installed we need to write a quick script to ensure the Sense HAT is working correctly.
We can start writing this script by entering the following command.
sudo nano ~/sensehat_test.py
4. Now write the following lines into this file, we will explain what each section of code does as we go.
from sense_hat import SenseHat
This line imports the Sense Hat module from the sense_hat library. This library allows us to interact with the HAT itself through python.
sense = SenseHat()
This line creates a link to the Sense HAT library and initializes itself so we can start making calls to it.
This line writes a message to the Sense HAT, you should see “Hello World” scroll across the RGB lights.
Press Ctrl + X then Y then press enter to save the file.
5. With the file now saved we can run it with the following command:
sudo python ~/sensehat_test.py
The text “Hello World” should now scroll across the RGB Led’s on top of the Sense HAT. If it doesn’t it is likely that the HAT has not been properly pressed down on top of the GPIO pins.
If it is still not working, try restarting the Raspberry Pi by running the following command again.
Setting up your Sense HAT as a weather station
1. Now that we have tested that the Sense HAT is working correctly we can now get writing our python weather station script. We will start with a basic script that continually reads and displays all the data from the Sense HAT’s sensors.
To begin writing our new python script run the following command in terminal.
sudo nano ~/weather_script.py
2. Now enter the following lines of code into the script, we will explain each block of code as we go.
#!/usr/bin/python from sense_hat import SenseHat import time import sys
First we need to import all the libraries that we plan to utilize in our script. In our case, we will be using the following libraries.
This is the library that we utilize to interact with the Sense Hat itself, without this we wouldn’t be able to read any of the sensor data or interact with the LED matrix.
This allows us to do a large variety of different time stuff, but for our simple script we will be just using its sleep functionality. This allows us to suspend the current thread for a small period.
This library provides us access to some variables and functions that are managed by the interpreter itself. In the case of our script we will be using this to terminate the script if we ever need to do so.
sense = SenseHat() sense.clear()
The first line creates a link to the Sense HAT library and initializes itself so we can start making calls to it.
The second line tells the library to clear the LED Matrix, by default this means switching off all the LED’s. It’s always a good idea to do this when dealing with the Sense HAT as it ensures nothing is being displayed already.
try: while True:
We setup our first try statement, we need to do this so we can break out of our while loop by pressing “Ctrl + C” make sure you keep the indentation for the while True. This is because Python is sensitive to indentation. The next few lines of code will need a three-tab (12 spaces) indentation.
temp = sense.get_temperature()
Getting the temperature is extremely simple thanks to the Sense HAT Library. All we have to do is make a call to the library for it to retrieve the temperature from the sensor. The output that this will give us will be in Celsius, it also provides a larger decimal number, but we will deal with that on our next line of code.
We will explain how to convert the temperature to Fahrenheit if you would prefer to deal with that instead of Celsius.
temp = round(temp, 1)
temp = 1.8 * round(temp, 1) + 32
Here we give you two choices for this line of code, the Celsius code just utilizes the value we got from the sensor and rounds it to the nearest decimal place.
The Fahrenheit code is basically the same, the only difference being that we convert the value from Celsius to Fahrenheit.
This bit of code is extremely basic and just prints the temperature to the terminal.
humidity = sense.get_humidity() humidity = round(humidity, 1) print("Humidity :",humidity) pressure = sense.get_pressure() pressure = round(pressure, 1) print("Pressure:",pressure)
Both the humidity and pressure sensors can be read just like the temperature sensor. Luckily for us the Sense HAT library makes this incredibly simple. Their values also come back with as a large decimal number, so we will again round them then simply display the values to terminal.
There isn’t much extra to say about these two code blocks as they operate just like the temperature code.
This is a simple call to the time library that puts the script to sleep for a period of approximately 1 second. This is basically to reduce the rate at which the data is read and outputted.
You can speed up the read rate by decreasing this number or removing the line completely. You can also slow it down further by increasing the number. The number should approximately be the number of seconds you want it to wait between reads.
except KeyboardInterrupt: pass
This code makes the try look for an KeyboardInterrupt exception. When it is triggered we ignore the exception so we can have the script leave the while loop cleanly. We do this by calling pass.
3. With all the code entered into our python file you should end up with something that looks like below, of course this will differ slightly if you used the Fahrenheit conversion code and not just straight Celsius.
#!/usr/bin/python from sense_hat import SenseHat import time import sys sense = SenseHat() sense.clear() try: while True: temp = sense.get_temperature() temp = round(temp, 1) print("Temperature C",temp) humidity = sense.get_humidity() humidity = round(humidity, 1) print("Humidity :",humidity) pressure = sense.get_pressure() pressure = round(pressure, 1) print("Pressure:",pressure) time.sleep(1) except KeyboardInterrupt: pass
Once your code looks something like the one displayed above and you are certain you have correctly indented your code you can quit and save, press Ctrl+X and then press Y and then press enter.
4. We can now run our new python script by running the following command in terminal.
sudo python ~/weather_script.py
5. You should now start to see text similar to the following appear in your terminal if everything is working exactly as it should be.
('Temperature C', 30.0) ('Humidity :', 39.8) ('Pressure:', 1025.7)
Once you’re happy with the data that is being displayed you can stop the script by pressing Ctrl + C. This will terminate the script from running. Of course, you probably don’t want to have to be looking at your Raspberry Pi’s terminal to be able to get the current data from it.
Instead we are going to show you two other methods for displaying data, the first of these is to just display the data to the LED matrix.
The other method is to utilize a piece of software called Initial State that will allow us to graph the data, and if you decide to pay for the software, you can also store the data over a period of time.
Improving your weather station – Utilizing the LED Matrix
1. Changing our weather python script to start displaying its data to the LED matrix is relatively easy. It will basically involve us concatenating all our prints together into a single string, then issuing the show_message command to the Sense HAT library.
Before we get started let’s begin editing our weather script by running the following command.
sudo nano ~/weather_script.py
2. Now that we are in our weather script we can begin making some changes, above the time.sleep line we need to add an extra line of code. This basically handles everything in one simple line.
sense.show_message("Temperature C" + str(temp) + "Humidity:" + str(humidity) + "Pressure:" + str(pressure), scroll_speed=(0.08), back_colour= [0,0,200])
Make sure you have typed out this line of code all onto a singular line. This will make the temperature, humidity, and pressure scroll across the Sense HAT’s LED matrix.
We will explain a bit of what we are doing here. We firstly concatenate the temperature onto the end of “Temperature C”, however since the temp variable is a number we need to wrap it in str() to convert it to a string.
Not converting the string will cause issues due to the way Python interprets adding a number to a string. We do this same process for the humidity and pressure.
We also decrease the scroll speed to 0.08, we do that with the following part of the
code scroll_speed=(0.08) you can increase or decrease this number to either speed up or decrease the speed the text scrolls.
The last bit of code we have on this line is
back_colour= [0,0,200], this part of the code basically sets the background colour of the text to blue.
You can also change the colour of the text itself. We can do this with our example of setting the colour of the text to a pinky colour by adding the following:
, text_color=[200,0,200] after
3. With that line added there is one last thing we will want to add, that being a call to
At the bottom of your script after the exception handling and pass, add the following line of code.
This code will basically ensure that the LED matrix is completely cleared if we kill the script for any reason. It also stops the annoyance of having to deal with a partially turned on LED matrix and has the added benefit of saving a miniscule amount of power, but there is no point on leaving something switched on when you are not utilizing it.
It is also good coding practice to make sure you clean-up everything when you stop a script from running. It helps stop things like memory leaks from occurring.
4. With those changes made your code should look similar to what is displayed below. Remember that the sense.show_message code should be contained within a single line.
#!/usr/bin/python from sense_hat import SenseHat import time import sys sense = SenseHat() sense.clear() try: while True: temp = sense.get_temperature() temp = round(temp, 1) print("Temperature C",temp) humidity = sense.get_humidity() humidity = round(humidity, 1) print("Humidity :",humidity) pressure = sense.get_pressure() pressure = round(pressure, 1) print("Pressure:",pressure) sense.show_message("Temperature C" + str(temp) + "Humidity:" + str(humidity) + "Pressure:" + str(pressure), scroll_speed=(0.08), back_colour= [0,0,200]) time.sleep(1) except KeyboardInterrupt: pass sense.clear()
Once your code looks something like the one displayed above and you are certain you have correctly indented your code you can quit and save, press Ctrl+X and then press Y and the press enter.
5. Now we can test our modified script to make sure that it functions correctly. Run the following command in terminal to run the script.
sudo python ~/weather_script.py
The text should now begin to scroll across the Sense HAT’s LED matrix. Of course, as you will quickly notice, this isn’t the easiest way of viewing all the sensors data.
This is mainly due to the small nature of the LED matrix making the text more difficult to read and only being able to display a single letter at one time. Of course, if you have an actual LED screen and not a light matrix, it will be a much better option for displaying your data on your Pi.
A nicer way of displaying your data is by utilizing a piece of software such as Initial State. We will go into setting up Initial state and getting our script to send data to it on the next page.
Improving your Raspberry Pi weather station with Initial State
1. For those who don’t know, Initial State is a website designed to act as a sort of data storage and data analytics site for Internet of Things devices like the Raspberry Pi. We’re using Initial state in this tutorial as the Cayenne IOT builder for the Raspberry Pi doesn’t currently support the sense HAT, if they eventually add it I will update this tutorial to reflect that.
Before we get started with implementing everything for the Raspberry Pi weather station we will first have to sign up for a free account over at their website
For more detail: Raspberry Pi Weather Station using the Sense HAT