OK, since I broke the first one during a firmware upgrade, I thought I should document this.
So, here are the hardware requirements:
- Raspberry Pi model 3B
- MicroSD Card of about 16GB
- Power Supply
- Sense HAT
- Ethernet cable or use the onboard Wi-Fi
Download the latest version of the Pi Installer – https://www.raspberrypi.com/software/ and flash your SD Card with the latest version
Once you have the SD Card ready, put it in your Raspberry Pi and boot.
I won’t go into configuring the network on the Pi, since it’s GUI. Just configure your network and put the OS to boot into CLI.
OK, now on with the software:
- upgrade
- install the Sense HAT software
1 2 3 4 5 |
sudo apt-get update sudo apt-get upgrade sudo apt-get install sense-hat sudo reboot |
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.
1 |
sudo nano ~/sensehat_test.py |
Write the following lines into this file, and we will explain what each section of code does as we go.
1 |
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.
1 |
sense = SenseHat() |
This line creates a link to the Sense Hat library and initializes itself so we can start making calls to it.
1 |
sense.show_message("Hello World") |
This line writes a message to the Sense Hat, and you should see “Hello World” scroll across the RGB lights.
Press CTRL + X then Y then press ENTER to save the file.
With the file now saved we can run it with the following command:
1 |
sudo python ~/sensehat_test.py |
The text “Hello World” should now scroll across the RGB LEDs 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 rerunning the following command.
1 |
sudo reboot |
Now here’s the full Python script I used to get the date, time, Temperature in Celsius degrees, Pressure in mmHg and Humidity in percents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#import modules from sense_hat import SenseHat from datetime import date, datetime import time import sys sense = SenseHat() sense.clear() try: while True: #Get values from SenseHat Sensors 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) pressureMMHG = pressure * 0.750063755419211 floatPressureMMHG = round(pressureMMHG, 2) print("Pressure:",floatPressureMMHG) # Month abbreviation, day and year today = date.today() d4 = today.strftime("%b-%d-%Y") print("Current Date is: ", d4) # Get time now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Current Time is: ", current_time) #Sample print in oneline #sense.show_message("Temperature C" + str(temp) + "Humidity:" + str(humidity) + "Pressure:" + str(pressureMMHG), scroll_speed=(0.08), back_colour= [0,0,200]) #Defining colours blue = (0, 0, 255) red = (255, 0, 0) orange = (255, 165, 0) yellow = (255, 255, 0) green = (0, 255, 0) blue = (0, 0, 255) purple = (160, 32, 240) #Print On SenseHat Matrix // the lower the value, the faster it moves sense.show_message ("Date: " + str(d4), scroll_speed=(0.07), text_colour=purple) sense.show_message ("Time: " + str(current_time), scroll_speed=(0.07), text_colour=yellow) sense.show_message("C: " + str(temp), scroll_speed=(0.10), text_colour=red) sense.show_message("Humidity % " + str(humidity), scroll_speed=(0.10), text_colour=blue) sense.show_message("mmHg: " + str(floatPressureMMHG), scroll_speed=(0.10), text_colour=orange) time.sleep(1) except KeyboardInterrupt: pass sense.clear() |
You can just use nano myweatherstation.py and paste the above script.
You can run the script using:
1 |
sudo python myweatherstation.py |
To run the script in a loop, you can use:
1 |
sudo python myweatherstation.py & |
Enjoy!
[update from October 13th, 2023]
Following the update to Bookworm (aka Raspberry Pi OS 12), you can have the following issues:
Error:
1 2 3 4 5 6 |
Traceback (most recent call last): File "/home/pi/Desktop/PY Sense Hat/pbnet-weather.py", line 2, in <module> sense = SenseHat() File "usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 39, in init raise OSError ('Cannot detect %s device' % self.SENSE_HAT_FB_NAME) OSError: Cannot detect RPi-Sense FB device |
Fix:
1 |
sudo nano /boot/config.txt |
and insert the line:
1 |
dtoverlay=rpi-sense |
To make the script run after reboot:
- create a script called: pbnet-weather.sh and put inside: sudo python /PBNET-WEATHER/pbnet-weather.py &
- make the script executable: chmod u+x pbnet-weather.sh
- create a cron entry: crontab -e
- in cron put: @reboot sudo /home/pi/pbnet-weather.sh &
Have fun!