[RPI/Python] Raspberry PI Weather station using SenseHat

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

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.

Write the following lines into this file, and we will explain what each section of code does as we go.

This line imports the Sense Hat module from the sense_hat library. This library allows us to interact with the Hat itself through python.

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, 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:

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.

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:

You can just use nano myweatherstation.py and paste the above script.

You can run the script using:

To run the script in a loop, you can use:

 

Enjoy!

[update from October 13th, 2023]

Following the update to Bookworm (aka Raspberry Pi OS 12), you can have the following issues:

Error:

Fix:

and insert the line:

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!