Weather station

Before you get started, you have to connect the Sense HAT module with your Raspberry Pi Zero W. Usually, Raspberry Pi Zero and Zero W come with non-soldered pins, so, if you haven't changed anything and haven't installed anything else, you will probably have to solder a GPIO pin extension to which you can install your Sense HAT easily. It is really up to you to set up this module, depending on your project and your hardware.

To start, let's update our Raspberry Pi Zero W board with the latest programs and updates. In the Raspberry Pi Zero and Zero W board, there are some issues, and updates are necessary. First, run the following command to update your Raspberry with the latest packages:

sudo apt-get update  

 Now, run the following command to upgrade your operating system to the latest one:

sudo apt-get upgrade  

Now, run the following command to reduce as much as possible the chance of bugs and later errors:

sudo rpi-update  

Next, add the following line to the end of the /boot/config.txt file:

core_freq=250  

You can edit the file with a vim or nano editor. After this, you should be fine. So, let's continue to install all the necessary libraries and do the full setup. The next step here is to install the Sense HAT software package so that we can interact with the module. To do this, simply run:

sudo apt-get install sense-hat  

Now, it is important to reboot your system. You can do this with the following command:

sudo reboot  

Once you are back to your Raspberry Pi Zero W, you have to ensure that everything went OK and that your module has been installed correctly. To do this, we will write a simple program. Create the sensehat_test file with your favorite editor (for example, using vim):

sudo vim ~/sensehat_test.py  

Now, we need to include the necessary libraries. First, from the sense_hat library, we need to include the Sense HAT module that allows us to interact with the Sense HAT module. To do this, we add the first line of code:

from sense_hat import SenseHat 

The next step is to create a link to the library, and therefore we initialize a variable that will allow us to interact with further calls to this library. Do this with the following command:

sense = SenseHat() 

Lastly, with the following command, you will see "Hello World" in the Sense HAT LEDs. We can write a message directly to the Sense HAT:

sense.show_message("Hello World") 

Now, save and quit your file. To run your script, you must simply execute the following command:

sudo python ~/sensehat_test.py  

At this point, as we said previously, you should see the text "Hello world" appear on the Sense HAT module. If not, try to replace the pins and fit them properly into your Raspberry Pi Zero W pinout. If you get errors, try resetting your Raspberry Pi and executing that Python script again. Next, you can find the full code to test this out:

from sense_hat import SenseHat 
sense = SenseHat() 
sense.show_message("Hello World") 

At this point, Sense HAT should be ready and we can now go to step two, where we will install and configure the weather station. Create a new file with your favorite editor:

sudo vim ~/weather_script.py  

Now, start writing some code. First of all, as we do in every Python script, we need to import all the necessary libraries. We can do this with the following lines of code:

#!/usr/bin/python
from sense_hat import SenseHat
import time
import sys

The sense_hat library is necessary to interact with Sense HAT. We need this library because we will read from the sensors. The Time library from Python standard libraries is necessary because we can use it to do many things with time, such as add a delay. Lastly, the sys library allows us to access variables and functions that are usually managed by the interpreter. As we said previously, the first line that we see in the next block of code initializes Sense HAT, so we can perform further calls to the functions relevant to the Sense HAT module:

sense = SenseHat() 

The following line clears the LED matrix in the Sense HAT module so we can have a clear matrix to write the results:

sense.clear() 

In this block of code, we will create a while loop that will run forever and will stop if Ctrl+C is pressed. We can do this with Python using Indent, where it needs with the following block of code:

try:
while True:

The next step is to get the temperature from the Sense HAT library. We can get this with one line of code using the library that we imported in the beginning. Notice that the output is in Celsius, so if you want to convert it to Fahrenheit, you have to make the conversion yourself:

temp = sense.get_temperature() 

Now, you may need to convert the temperature to the closest decimal number. You can do this using the following code:

round(temp, 1) 

So, to sum up, if you want the temperature expressed in Celsius, then you have to execute the following command:

temp = round(temp, 1) 

On the other hand, if you want the temperature expressed in Fahrenheit, you have to run the following command:

temp = 1.8 * round(temp, 1)  + 32 

The last line of the code that we will write is to print the value of the temperature. In case you want to print the Celsius temperature, type the following command:

print("Temperature C",temp) 

You can also replace the C with an F if you have previously converted the temperature to Fahrenheit. Apart from the temperature, we will get the humidity and pressure values from Sense HAT. Sense HAT provides these values just like the temperature, so it is pretty much the same lines of code:

humidity = sense.get_humidity()  
humidity = round(humidity, 1)
print("Humidity :",humidity)

pressure = sense.get_pressure()
pressure = round(pressure, 1)
print("Pressure:",pressure)

The last line of code uses the time library. We need to sleep the code execution for one second so that we can see the flow of data. We will do this with the following code:

time.sleep(1) 

When KeyboardInterrupt is triggered, we ignore the exception, so we can have the script leave the while loop running. We will do this with the following code:

except KeyboardInterrupt:
pass

Without the preceding lines of code, when the user pressed Ctrl+C, the program would end. The following is the full code:

#!/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

Save and exit from the editor with the file name as weather_script.py. Next, run the Python script:

python weather_script.py 

Now, you should see the temperature, humidity, and pressure on your screen, for example:

('Temperature C', 30.0)
('Humidity :', 39.8)
('Pressure:', 1025.7)

At this point, if everything goes well, you should be able to see these values on your screen. If you want to change the way they appear, you can always go back to your Python script file to edit the way they are printed. In the following section, we will display these values in a cooler way.

Changing the Python script we have written and displaying the data to the LED matrix in Sense HAT is not a big deal. There is already a call to Sense HAT, where you can print a string to the matrix. So, what we have to do is simply concatenate all the data we want to pass to Sense HAT in one string. The first step is to edit the file that we created before. Do this with vim or any other editor you like; for example, using vim, we can do this with the following code:

sudo vim weather_script.py 

Next, we use the command that puts your program to sleep for one second:

time.sleep(1) 

We need to add a new line of code. This new line of code combines all the data from the sensor that we get at the start of the program and displays them in one line in Sense HAT:

sense.show_message("Temperature C" + str(temp) + "Humidity:" + str(humidity) + "Pressure:" + str(pressure), scroll_speed=(0.08), back_colour= [0,0,200]) 

It is important to write the previous command in one line of code. In the sense, we display a message. The message is the concatenation of " " strings such as "Temperature C" and str() that is integer numbers converted into strings with the str() command from the Python library. The scroll speed is set to 0.08, but we can change it to more or less. Depending on the way the text was changed, the color was set to blue. Next, we need to make a call to the sense clear with the following code:

sense.clear(). 

This way, we will ensure that the LED matrix is completely cleared if we end the script for any reason. The full code is displayed as follows:

#!/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()

Now, save and exit the file that you were editing. Run the script again with the following code:

sudo python weather_script.py  

Now, you should see the text scroll across the Sense HAT LED matrix area. You will notice that there is one problem here. You cannot display all the data at once. This can be handled with an external monitor or the next section of the chapter. Now, you will see how you can improve your Raspberry Pi weather station with Initial State.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.96.155