Complete Guide Raspberry Pi Weather Station Using Sense HAT

Sense HAT rainbow background

The project will create a local Raspberry Pi Weather Station that detects temperature, humidity, and barometric pressure, including the Sense Hat. We will also create a graphical user interface to display temperature, humidity, and barometric pressure readings in real-time.

 Necessary equipment: 

You have to need flowing equipment for Raspberry Pi Weather Station

  1. Raspberry y Pi (versions with 40 GPIOs)withHats of
  2. Sense HAT

A small and affordable weather station can be built with temperature, humidity, and barometric pressure sensors.

  Temperature

 temperature sensor measures the temperature. By default, the Sense Hat temperature reads in degrees Celsius, so if you want to convert the temperature to degrees Fahrenheit, multiply the degrees in Celsius by 9, divide by 5, and add 32, as shown in the following formula.

Humidity Sensor

There are two common ways to express moisture: absolute humidity and relative humidity. Absolute humidity is the mass of water vapor in a certain amount of air regardless of temperature, and it is expressed as kg per cubic meter (kg / m3). 

The amount of water vapor that air can hold changes with temperature. The higher the temperature, the more water vapor can be retained. Relative humidity is expressed as a percentage and the current water vapor in the air relative to the maximum possible amount at a given temperature.

 Sense Hat records relative humidity because it is useful for weather forecasting: the higher the relative humidity percentage, the higher the probability of precipitation. It is always combined with a temperature sensor as the relative humidity changes with temperature.

Barometric Pressure: 

SensorBarometric pressure sensor measures atmospheric pressure, the “weight” of air at a given point, measured by HPA (hectopasal), which is equivalent to amber (millibars).

 Why is it interesting to measure pressure? Because changes in atmospheric pressure can help you forecast the weather. With increasing pressure comes signs of good weather and a decrease in bad weather symptoms such as rain or storms.

The pressure change is really small. You must follow the barometer readings carefully to notice a trend.

Temperature Humidity and Pressure Readings:

 Now we will see how the data readings are taken from the sensors and the data obtained in the Python shell is printed.

Mount the Sense hat on your Raspberry Pi just like in your Project 8 and make sure it’s well connected. After it is first attached, the Sense hat will display a rainbow background when you boot the raspberry pie that matches the rainbow you see below (see Figure 1).

 Sense HAT rainbow background
Figure 1: Sense HAT rainbow background

Inside your Projects folder create a new folder called Sensors. Create a new folder called Sensor inside your project folder. Now take out the python IDE of Raspberry Pi and write the following program there and when the writing is done save it as weather_data.py. 

Reading temperature, humidity, and pressure with the Sense HAT

➊ from sense_hat import SenseHat
  #from sense_emu import SenseHat

  from time import sleep

  #create an object called sense
➋ sense = SenseHat ()

  while True:
  ➌ temperature = sense.temperature
  ➍ temperature = str (round (temperature, 2))
  ➎ print ('Temperature:' + temperature + '* C \ n')
    humidity = sense.humidity
    humidity = str (round (humidity, 2))
    print ('Humidity:' + humidity + '% \ n')

    pressure = sense.pressure
    pressure = str (round (pressure, 2))
    print ('Pressure:' + pressure + 'hPa \ n')

    sleep (1)

Script Analysis: 

from sense_hat import SenseHat
  #from sense_emu import SenseHat

  from time import sleep

 

 ➊ First the sense_hat library is imported from SenseHat. Who controls time from sleep. has been imported library   

➋ sense = SenseHat () 

 Create an object called sense to refer to Sense HAT  

 

➌ temperature = sense.temperaturereads the temperature

This line from the scene. You can take humidity or atmospheric pressure readings using the following lines. 

sense.temperature gets the temperature reading.
sense.humidity gets the humidity reading.
sense.pressure gets the pressure reading.

 

 ➍ temperature = str (round (temperature, 2)) The

readings we get from here are in decimal numbers. The round () function has been used to make these numbers better readable. 

   ➎ print (‘Temperature:’ + temperature + ‘* C \ n’)
    humidity = sense.humidity
    humidity = str (round (humidity, 2))
    print (‘Humidity:’ + humidity + ‘% \ n’)

    pressure = sense.pressure
    pressure = str (round (pressure, 2))
    print (‘Pressure:’ + pressure + ‘hPa \ n’)

    sleep (1)

 The str () function is also used to convert the tax argument to a string. You need to convert the readings into strings so that you can print them on the shell.

Creating a user interface for reading data 

Now let’s take this project to another level, creating a great user interface for displaying sensor readings. The interface will feature the following: The

  1.  desktop has a window that displays temperature, humidity, and pressure. 
  2. Humidity is displayed in the vertical progress bar from 0 to 100 percent.
  3. The temperature and pressure will appear in numbers. 
  4. There will be levels for reading each data. 

 

Figure 2 shows a draft format for the user interface that will give you an idea of ​​how the code works.

Graphical user interface
Figure 2: Graphical user interface

 will be able to edit the code to select the font type, size, and color and how to locate the labels and texts in the window. The following table lists all the titles and values, and how we will display them.

 

WIDGETOPTIONS
Window TitleText: “Local Weather Station”
Humidity TitleText: “Humidity”, Font: Helvetica, Size: 18, Vertical padding: 3
Humidity ValueFont: Courier, Size: 20, Color: Blue, Position: North
Humidity Progress BarOrientation: Vertical, Size: 20, Color: Blue, Position: North, Length: 200, Maximum Value: 100
Temperature TitleText: “Temperature”, Font: Helvetica, Size: 18, Position: South
Temperature ValueFont: Courier, Size: 20, Color: Red, Position: North
Pressure TitleText: “Pressure”, Font: Helvetica, Size: 18, Position: South
Pressure ValueFont: Courier, Size: 20, Color: Green, Position: North:

 

Script Writing for Raspberry Pi Weather Station:

Now Extract the python IDE of the Raspberry Pi and write the following program there and when theit as writingweather_station.py in the is done save sensor folder.

Displaying the Sense HAT readings in a graphical user interface

  #import necessary libraries
➊ from tkinter import *
  from tkinter import ttk
  import time
  from sense_hat import SenseHat
  #from sense_emu import SenseHat

  #create an object called sense
  sense = SenseHat ()

  #create window
➋ window = Tk ()
  window.title ('Local Weather Station')
  window.geometry ('200x480')

  #create humidity label for title and value
➌ humidity_label = Label (window, text = 'Humidity', font =
  ('Helvetica', 18), pady = 3)
  humidity_label.pack ()

➍ humidity = StringVar ()

humidity_value = Label (window, textvariable = humidity, font =
  ('Courier', 20), fg = 'blue', anchor = N, width = 200)
  humidity_value.pack ()

  #create humidity canvas
➏ humidity_canvas = Canvas (window, width = 200, height = 200)
  humidity_canvas.pack ()

  #create humidity progress bar
➐ humidity_bar = DoubleVar () ➑

progressbar_humidity = ttk.Progressbar (humidity_canvas, variable =
  humidity_bar, orient = VERTICAL, length = 200, maximum = 100)
  progressbar_humidity.pack (fill = X, expand =1)

  #create temperature label for title and value
  temperature_label = Label (window, text = 'Temperature', font =
  ('Helvetica', 18), anchor = S, width = 200, height = 2)
  temperature_label.pack ()

  temperature = StringVar ()

  temperature_value = Label (window, textvariable = temperature, font =
  ('Courier', 20), fg = 'red', anchor = N, width = 200)
  temperature_value.pack ()

  #create pressure label for title and value
  pressure_label = Label (window, text = 'Pressure', font =
  ('Helvetica', 18), anchor = S, width = 200, height = 2)
  pressure_label.pack ()

  pressure = StringVar

  () pressure_value = Label (window,
   
 

 update_readings():
      humidity.set (str (round (sense.humidity, 2)) + '%')
      humidity_bar.set (sense.humidity)
      temperature.set (str (round (sense.temperature, 2)) + ' C ')
      # temperature.set (str (round (sense.temperature * (9/5) +32, 2))
  + '* F')
      pressure.set (str (round (sense.pressure)) + 'hPa')
      window. update_idletasks ()
      window.after (3000, update_readings)

➓ update_readings ()
  window.mainloop ()

Script Analysis: 

from tkinter import *
  from tkinter import ttk
  import time
  from sense_hat import SenseHat
  #from sense_emu import SenseHat

  #create an object called sense
  sense = SenseHat ()importing the

As usual, you started the code required library. Now you may be wondering why we have to import ttk if we have already imported everything from the Tkinter library with * in the previous line.

  In this case, when you import with Wildcard *, there is no special reason why you are importing only a subset of what you store in the library folder, it is only the author of the library who decided to do this but we also need this user interface separately ttk library to import You need to use the sense_hat library to collect weather data.

Creating a user interface for Raspberry Pi Weather Station:

After all the libraries, you apply the part of the code that creates the user interface.

= Window = Tk ()
  window.title (‘Local Weather Station’)
  window.geometry (‘200×480’

Create a window here which is 200 × 480 pixels and give it the title Local Weather Station.  

 

➌ humidity_label = Label (window, text = ‘Humidity’, font =
  (‘Helvetica’, 18), pady = 3)
  humidity_label.pack ()

 Create a label for the title of humidity matching the table above.


Idity humidity = StringVar ()

 Create a string variable called which humidity will maintain the value. 

 

➎ humidity_value = Label (window, textvariable)
     

  displayed on the value 

➏ humidity_canvas = Canvas ( window, width = 200, height = 200)
  humidity_canvas.pack ()

Lines of code A canvas has been created to place the progress bar. 

➐ humidity_bar = DoubleVar ()

is followed by double type humidity_bar by called. variable, Which is managed according to the progress bar.

 

Until the end, the lines create a moisture progress bar to keep on the canvas.

Automatically Updating the Readings

def update_readings():
      humidity.set (str (round (sense.humidity, 2)) + ‘%’)
      humidity_bar.set (sense.humidity)
      temperature.set (str (round (sense.temperature, 2)) + ‘ C ‘)
      # temperature.set (str (round (sense.temperature * (9/5) +32, 2))
  + ‘* F’)
      pressure.set (str (round (sense.pressure)) + ‘hPa’)
      window. Update_idletasks ()
      window.after (3000, update_readings)

Update the displayed values ​​to keep your weather text up to date every three seconds with the update_readings () function.

Temperature is always updated through the following line 

temperature.set (str (round (sense.temperature, 2)) + ‘* C’)

Now we will analyze the components of this line, 

  1. Sense. temperature – There is no temperature reading from Sense Hat.
  2. round (sense. temperature, 2) – The temperature in two decimal places makes the mind round the text.
  3. Converts str (round (sense.temperature, 2) -rounded reading to. 
  4. str(str (round (sense.temperature, 2)) + ‘* C’) – Attaches the degree symbol to the string. 
  5. temperature.set (str ( round (sense.temperature, 2)) +the latestreading 

withtemperatureis updated 


  dynamically. ➓ update_readings () window.mainloop ()

keeps you running to me all the time. 

finally, the bottom line for you Shows temperature from Fahrenheit to Celsius and Celsius and Fahrenheit. 

# Temperature.set (str (round (sense.temperature, 2)) + ‘* C’)

temperature.set (str (sense.temperature * (9/5)) +32, 2)) + ‘* F’)

Run the script 

To run the, you must first save the program above, then run Run ▸ Run current script or press F5 from the keyboard, then the program will run at the beginning of the project. Your work data will be displayed in your user interface.  

RASPBERRY PI TUTORIALS FOR BEGINNERS

 13 total views,  1 views today