Space Science Blog

Bringing You The Future Of Science and Technology

RASPBERRY PI - WEATHER STATION


In this tutorial we will learn how to build our own weather station using the Raspberry Pi and Sense HAT. We will then use Python, Numpy and Matplotlib to plot and predict the weather.

Artificial Intelligence | DIY Electronics Projects | Game Design Workshop | Gallery | Random Generators | Planetary Science | Rocket Science


Categories

Art and Web Design

Contact me for information about rates and availability.


Latest Articles

 Raspberry Pi Weather Station

In this multi-part tutorial we will learn how to create an Astro Pi using the Raspberry Pi and Sense HAT. The Sense HAT has multiple integrted circuit-based sensors, which can be used to "sense" pressure, humidity, temperature, speed and Earth's magnetic field. The Astro Pi is currently being used to perform a variety of science experiements aboard the International Space Station (ISS).


Weather Station:

Raspberry Pi
Sense HAT
Python Code
Bash Script
Cronjob

For our weather station we will be using the Raspberry Pi 3 Model B+, which is a small single-board computer developed by the Raspberry Pi Foundation. We will download and install Raspbian Jesse, a Debian-based operating system for the Raspberry Pi. Lastly, we will be using the Raspberry Pi Sense Hat, with its integrated circuit-based sensors.


 Raspberry Pi 3 Model B+

For only about $35, the Raspberry Pi 3 is a fully functional computer that comes equipped with a 1.4 GHz 64-bit quad-core processor and 2.4 / 5 GHz dual-band wireless LAN. The Raspberry Pi 3 comes equiped with built-in WiFi, Bluetooth and USB capabilities. You will also need a MicroSD card, which we will use to install Raspbian. When installing and configuring the Raspbian operating system, you may need a display, keyboard and mouse. Once properly configured, you will be able to connect to the Raspberry remotely over the local WiFi network.



For this tutorial, you will need the latest version of Raspbian, which already includes the following software. Once you have properlly installed Raspbian and the Sense HAT, open a terminal window and type the following commands:


$ sudo apt-get install update
$ sudo apt-get install upgrade
$ sudo apt-get install sense-hat
$ sudo apt-get install python3


 Sense Hat

In order to build our own Astro Pi we will need the Raspberry Pi Sense HAT, which is installed atop the Raspberry Pi. This add-on allows you to make measurements of temperature, humidity, pressure and orientation and to output this information to a built-in LED matrix. Before attaching the Sense HAT to your Raspberry Pi, make sure to power it down first. Push the Sense Hat carefully down onto the Raspberry Pi.

NOTE: You can secure the Sense Hat using standoffs, although you do not need these for the Sense HAT to work.

from sense_hat import SenseHat
import time
import datetime

sense = SenseHat()
sense.clear

while True:
   print("##########################################")
   timestamp = datetime.datetime.now().strftime("%y-%m-%d %H:%M")
   print("Timestamp: ", timestamp)
   pressure = sense.get_pressure()
   pressure_str = str(pressure)
   print("Pressure (millibars): ", "%.1f" % pressure)
   humidity = sense.get_humidity()
   humidity_str = str(humidity)
   print("Humudity (%): ", "%.1f" % humidity)
   temp = sense.get_temperature()
   converted_temp = (temp * (9/5)) + 32
   temp_str = str(temp)
   print("Temp (F): ", "%.1f" % converted_temp)
   raw = sense.get_compass_raw()
   print("Bx: {x}, By: {y}, Bz: {z}".format(**raw))
   print("##########################################")
   print("")
   f = open("temp.txt", "a")
   f.write("Timestamp:")
   f.write(timestamp)
   f.write(" Pressure: ")
   f.write(pressure_str)
   f.write(" Humidity:")
   f.write(humidity_str)
   f.write(" Temp (C): ")
   f.write(temp_str)
   f.write("\n")
   f.close()
   formatted_date = datetime.datetime.now().strftime("%y%m%d%H%M")
   f = open("weather-data.txt", "a")
   f.write(formatted_date)
   f.write(", ")
   f.write(pressure_str)
   f.write(", ")
   f.write(humidity_str)
   f.write(", ")
   f.write(temp_str)
   f.write("\n")
   f.close()
   f = open("magnetometer-data.txt", "a")
   f.write(formatted_date)
   f.write(", ")
   f.write("Bx: {x}, By: {y}, Bz: {z}".format(**raw))
   f.write("\n")
   f.close()

   # Take readings from all three sensors
   t = sense.get_temperature()
   p = sense.get_pressure()
   h = sense.get_humidity()

   # Round the values to one decimal place
   t = round(t, 1)
   p = round(p, 1)
   h = round(h, 1)

   # Create the message
   # str() converts the value to a string so it can be concatenated
   message = "Temperature: " + str(t) + " Pressure: " + str(p) + " Humidity: " + str(h)

   # Display the scrolling message
   sense.set_rotation(180)
   sense.show_message(message, scroll_speed=0.3)

   time.sleep(60)


Johnathan Nicolosi - 12 Jul 2018