BC Robotics

Raspberry Pi Weather Station – Part 2

  PRODUCT TUTORIAL

In the last part of this tutorial we added our components to the weather board and connected everything up. Now that we have everything ready for power, we can work on the software / coding side of this project. In part two of this tutorial we will boot up the Raspberry Pi, do some initial configuration of the Raspbian operating system, install Python libraries for a few of the sensors, and write some basic Python code to collect and display data from each of the sensors. The Python code will be quite long, but we have broken it down into smaller steps. As always, if you have any questions, feel free to post at the bottom of the page.

Operating System:
Before we get started, you should have a microSD card pre-installed with Raspbian. We are using the installation image dated June 27, 2018. Using a different version than this *could* require additional steps during the installation process – so for this reason we recommend using the exact version we are. At the time of writing this tutorial, this is the most current version available from the Raspberry Pi Foundation and it can be found here.

The Parts Needed:

In addition the completed assembly from Part 1, this tutorial will be requiring a few additional parts:

A few other things will also be needed for this tutorial:

  • A USB Keyboard & Mouse
  • A HDMI Compatible Monitor
  • Internet Access

Step 1 – Boot Up

We are going to start by inserting the microSD card with our operating system pre-installed into the Pi. Your keyboard, mouse and monitor should also be connected at this time. Power up the Pi by plugging in the Power Supply. Once the Pi has done its initial boot of the operating system you should arrive at a desktop. A dialogue should appear for initial configuration – we will take care of this in the next step. 

5.2%

Step 2 – OS Configuration

As of this version of Raspbian, most initial setup can be done by following the dialogue that pops up on first boot up. However, we do need to set up a few additional things once this is initial configuration is completed. Follow through the initial setup, if prompted to reboot – go ahead and do that as well.

Once all of that has been completed, we can go ahead and configure a few more things. Click the Raspberry Logo in the top left corner, scroll down to Preferences, and select “Raspberry Pi Configuration”.

On the first tab you can change your overscan settings. If you are using a computer monitor, you may want to disable the overscan setting to remove the black bars on the sides of the screen.

On the second tab we are going to want to enable the I2C and 1-Wire interfaces. These are the two interfaces our sensors are using in this project.

If prompted to reboot, click yes. Once the Pi boots back up we can proceed.

10.4%

Step 3 – Internet Connectivity

Before we can install any of the libraries or make any of the sensors work we need internet connectivity. If you are using WiFi, this was probably already set up during the initial configuration. If it isn’t working, or you skipped that step, it can be accessed by clicking the connectivity logo (located beside the Volume control in the top right corner of the screen).

Ethernet is as simple as plugging it into the Pi – if you are planning to use ethernet, go ahead and plug this in if you haven’t already.

15.6%

Step 4 – Terminal

Now that all of the basic configuration is done, we can get on with installing the software libraries needed to read each of the sensors. These libraries of code are going to take a lot of the hard work out of reading the sensors. We are going to install these using the “Terminal” , in the bar at the top of the screen, click the black square logo, and this window should pop up:

20.8%

Step 5 – Adafruit Python GPIO Library

The first item to install is the Adafruit Python GPIO Library. This package is the basis for several others that we will be using. To install it we will type a series of commands into the terminal window we just opened up.

Run each of these commands (in order!) by typing them in and hitting enter:

				
					
sudo apt-get update
sudo pip3 install --upgrade setuptools
pip3 install RPI.GPIO
pip3 install adafruit-blinka
				
			
26%

Step 6 – Adafruit BME280 Library

Next we will install the Adafruit BME280 library for Python. This library was also created by Adafruit and has been designed to work with their BME280 Temperature/Humidity/Pressure sensor breakout board.

Run the following command in the terminal:

				
					sudo pip3 install adafruit-circuitpython-bme280
				
			
31.2%

Step 7 – ADS1x15 Library

Next, we are going to set up the ADS1015 Analog to Digital chip. Adafruit has built an easy to use library for this series of Analog to Digital converters. We will use this to read the wind direction. To install the library, type the following command into the Terminal:

				
					sudo pip3 install adafruit-circuitpython-ads1x15
				
			
36.4%

Step 8 – DS18B20

Now it is time to set up the DS18B20 temperature sensor. The setup for this sensor is a bit different; the past two sensors both used the I2C bus, the DS18B20 uses 1-Wire. Enter the following commands in the terminal:

				
					cd
sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls
				
			

Since the temperature sensor will be listed in the devices folder, we will want to see everything in that folder. The last command “ls” will display the contents of the folder in the window. Our DS18B20 shows up with an address of 28-0316853d8fff – but each sensor has a unique ID so your number will not match the image! 

41.6%

Step 9 – Test Your DS18B20

Time to get a reading from the DS18B20 sensor! Enter the following command with your sensor ID in place of *sensor ID*

				
					cd *sensor ID*
				
			

Now we can pull a temperature from it by entering the following command:

				
					cat w1_slave
				
			

Not as easy to read as the BME280, but the information should be there. The temperature is located in the second line, take the number given and divide by 1000 to get the temperature in degrees Celsius.

46.8%

Step 10 – DS18B20 Library

Since the DS18B20 isn’t the easiest sensor to read directly in Python, we will be installing one last library to make it a little more straight forward. In the terminal type the following commands:

				
					cd
sudo pip3 install w1thermsensor
				
			

This will make things quite a bit easier once we start writing some code.

52%

Step 11 – Python

Now we are getting somewhere! All of the complex sensors are sending back data so lets write some basic python code to poll each of the sensors and report back every 15 seconds. We aren’t going for award winning code here, just something easy to understand for those relatively new to Python.

We want to program in Python 3 so type the following command in the terminal and hit enter:

				
					idle3
				
			

Alternatively, Python 3 can be found by clicking the Raspberry Button in the top left. Under programming you will find a shortcut. Either way, A new window should have opened for the Python 3 Shell. We are now done with the terminal window so it can be minimized or moved out of the way (but don’t close it!).

57.2%

Step 12 – Getting Ready

In the Python Shell window we just opened, click “File” and “New File” to start a new Python file. This will open another window that we will write all of our code in.

There are a million different ways to approach writing code to accomplish our end result of collecting data from sensors and sending it to Thinkspeak. We are going to approach this by basing the code around the timing interval we want to send data to Thingspeak (once every 15 seconds). This allows us to structure the program around a simple 15 second time loop.

Why 15 seconds? Two reasons: We want to have sufficient time to measure a wind speed (there are ways to manage this at issue, but we want to keep this as simple as possible) and Thingspeak limits free accounts to a once per 15 second data rate.

62.4%

Step 13 – Starting The Code

Before we can read any of the sensors, we will need to create the basic framework of the program. This means we need to import the time library, create our loop, and set the interval we want to pause the code each time the loop has completed. For those unfamiliar with Python, note that the white spaces (tabs and spaces) are very important in this language so be sure to format exactly as shown in the example.

				
					import time

interval = 15 #How long we want to wait between loops (seconds)

while True:

    time.sleep(interval)
				
			

Libraries are added to the project using the import command. On the first line we are importing the time library. On line 3 we define a variable to store the length of time in seconds that we want to sleep the program during each loop of the code. Next, we want to create the loop itself – in Python this can be done in a variety of ways; “While true:” works well. Note that when you press “Enter” the next line becomes indented. Finally, on line 7 we “sleep” or pause our program using the variable we set above.

Note: Because we are going to be adding a lot of code over the next few steps, each new line we add will be highlighted in the example code and the line number will correspond to notes below the code.

67.6%

Step 14 – Read The Temperature

During the set up we installed a bunch of libraries to make this program a lot easier to write. We are going to use one of these libraries right now to read the DS18B20 temperature sensor. In these next bits of code we are going to import the DS18B20 library, set up the sensor, read the sensor, and print the result.

				
					import time
from w1thermsensor import W1ThermSensor

ds18b20 = W1ThermSensor()

interval = 15 #How long we want to wait between loops (seconds)

while True:

    time.sleep(interval)
    
    #Pull Temperature from DS18B20
    temperature = ds18b20.get_temperature()
    
    #Print the results
    print( "Temperature: " , temperature)
				
			
  • (2) Similar to the time library, we import the w1Thermsensor library
  • (4) Next we create our temperature sensor object and give it a name
  • (13) Down in the loop we get the temperature from the sensor and store it in a variable named “temperature”
  • (16) And finally print the result using the print command

Let’s try it out – press “F5” and it should prompt you to save it. After saving it should run, and in the Python Shell window you should see the temperature appear after a 15 second wait. It will continue to post an updated temperature every 15 seconds. This can be stopped by pressing “Ctrl + C”.

72.8%

Step 15 – Read The BME280

Next we will tackle the BME280 – this provides us with three measurements: Temperature, Pressure, and Humidity. The library we have installed already handles most of the hard work, but because of the way this library is written there are a few tricks that will prevent issues going forwards.

Just like the last sensor, we will import the library, set up the sensor, read the sensor, and print the results. Since we are now using the Circuit Python compatible libraries in this tutorial, there are a few extra chunks of code to add.

				
					import time
from w1thermsensor import W1ThermSensor

import board
import busio
from adafruit_bme280 import basic as adafruit_bme280
i2c = busio.I2C(board.SCL, board.SDA)

bme = adafruit_bme280.Adafruit_BME280_I2C(i2c)

ds18b20 = W1ThermSensor()

interval = 15 #How long we want to wait between loops (seconds)

while True:

    time.sleep(interval)
    
    #Pull Temperature from DS18B20
    temperature = ds18b20.get_temperature()
    
    
    #Pull temperature from BME280
    case_temp = bme.temperature
    
    #Pull pressure from BME280 Sensor & convert to kPa
    pressure_pa = bme.pressure
    pressure = pressure_pa / 10
    
    #Pull humidity from BME280
    humidity = bme.humidity
    
    #Print the results
    print( "Temperature: " , temperature)
    print( "Humidity: " , humidity, "%")
    print( "Pressure: " , pressure, "kPa")
    print( " ")
				
			
  • (4) Import the board library from Adafruit Blinka
  • (5) Import the busio library from Adafruit Blinka
  • (6) Import the Adafruit BME280 library
  • (7) Configure I2C to use the boards hardware I2C pins
  • (9) Next we create our BME280 sensor object and give it a name. In the brackets we set it to I2C, as that is how it is connected
  • (24) Get the temperature from the BME280 and store it in the variable “case_temp”
  • (27) Get the pressure from the BME280 and store it in the variable “pressure_pa”
  • (28) Since we want kilopascals, we will divide by 10. The new result is stored in the variable “pressure”
  • (31) Get the humidity from the BME280 and store it in the variable “humidity”
  • (35) Print the humidity
  • (36) Print the pressure
  • (37) Print a blank line to separate the data and make it easier to read

Although we are not using the BME’s temperature sensor, we still need to read it. Because of the way the library is written, simply asking for the pressure and humidity without asking for the temperature first will cause your program to crash. So we have read the temperature and called it “case temp” as it would be a decent indication of the air temperature inside the box that this will eventually end up in.

Once your code looks like the above, hit “F5” again to run it. This time there should be a bunch more information!

78%

Step 16 – Read The ADC (Wind Direction)

Wind direction is next – again we are going to import the library, set up the sensor, and read the results. Unlike all of the past sensors, this one does not simply spit out exactly what we want to know. The sensor has differing resistance for each of the 16 wind directions it is capable of monitoring – in its current configuration, this means each direction will output a different voltage. We will read this voltage with our Analog to Digital converter and convert this to useable information in the form of a direction.

				
					import time
from w1thermsensor import W1ThermSensor

import board
import busio
from adafruit_bme280 import basic as adafruit_bme280
i2c = busio.I2C(board.SCL, board.SDA)

import adafruit_ads1x15.ads1015 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

bme = adafruit_bme280.Adafruit_BME280_I2C(i2c)
ads = ADS.ADS1015(i2c)
ads.gain = 1

ds18b20 = W1ThermSensor()

interval = 15 #How long we want to wait between loops (seconds)

while True:

    time.sleep(interval)
    
    #Pull Temperature from DS18B20
    temperature = ds18b20.get_temperature()
    
    #Pull temperature from BME280
    case_temp = bme.temperature
    
    #Pull pressure from BME280 Sensor & convert to kPa
    pressure_pa = bme.pressure
    pressure = pressure_pa / 10
    
    #Pull humidity from BME280
    humidity = bme.humidity
    
    #Calculate wind direction based on ADC reading
    chan = AnalogIn(ads, ADS.P0)
    val = chan.value
    windDir = "Not Connected"
    windDeg = 999
    
    if 20000 <= val <= 20500:
        windDir = "N"
        windDeg = 0
    
    if 10000 <= val <= 10500:
        windDir = "NNE"
        windDeg = 22.5
    
    if 11500 <= val <= 12000:
        windDir = "NE"
        windDeg = 45
    
    if 2000 <= val <= 2250:
        windDir = "ENE"
        windDeg = 67.5
    
    if 2300 <= val <= 2500:
        windDir = "E"
        windDeg = 90
    
    if 1500 <= val <= 1950:
        windDir = "ESE"
        windDeg = 112.5
    
    if 4500 <= val <= 4900:
        windDir = "SE"
        windDeg = 135
    
    if 3000 <= val <= 3500:
        windDir = "SSE"
        windDeg = 157.5
    
    if 7000 <= val <= 7500:
        windDir = "S"
        windDeg = 180
    
    if 6000 <= val <= 6500:
        windDir = "SSW"
        windDeg = 202.5
    
    if 16000 <= val <= 16500:
        windDir = "SW"
        windDeg = 225
    
    if 15000 <= val <= 15500:
        windDir = "WSW"
        windDeg = 247.5
    
    if 24000 <= val <= 24500:
        windDir = "W"
        windDeg = 270
    
    if 21000 <= val <= 21500:
        windDir = "WNW"
        windDeg = 292.5
    
    if 22500 <= val <= 23000:
        windDir = "NW"
        windDeg = 315
    
    if 17500 <= val <= 18500:
        windDir = "NNW"
        windDeg = 337.5
    
    #Print the results
    print( "Temperature: " , temperature)
    print( "Humidity: " , humidity, "%")
    print( "Pressure: " , pressure, "kPa")
    print( "Wind Dir: " , windDir, " (", windDeg, ")")
    print( " ")
				
			
  • (9) Just like the other libraries, we import the Adafruit_ADS1x15 library
  • (10) Load the AnalogIn library for the ADS1x15
  • (13) Next we create our ADC object and give it a name. It is also connected over I2C
  • (14) Set the programmable gain amplifier to 1 in the ADC
  • (38) Get the reading from the ADC channel 0 and store it in the variable “chan”
  • (39) We only need the adc value, so pull that from chan and assign it to a new variable “val” after multiplying it by 16. This is required due to changes in the Adafruit Library.
  • (40-41) Define a base level reading if sensor isn’t connected
  • (43-105) Check to see if val is between two values, if it is we assign the direction, otherwise it continues down the list until a match is found
  • (111) Print the results

The code for this is large and cumbersome, but rather than slim it down to something elegant and easy to type, we would rather show the mental process of how to figure out what direction the wind is coming from. Each direction outputs a different number – but this number will change a small amount based on temperature, power fluctuations, etc. so we cant just check if our ADC output matches a number in a list. Instead we have to see if they fall within a range.

So where do each of the ranges of numbers come from that we are testing against? The numbers we are using were found by manually moving the wind direction sensor to each of the directions and reading the output. These numbers were written down and then we added a buffer to each “side” to ensure we don’t have any incorrect readings. So as long as the value from the ADC is between these large ranges, it will read that specific direction. Feel free to test the code again with the “F5” key.

83.2%

Step 17 – Wind Speed

The last two sensors are simple digital inputs on the Pi, these do not have a fancy chip to read or anything of the sort. The wind speed sensor is effectively a magnet and a small magnetic switch. As the sensor spins the magnet moves past a switch, causing it to close. A wind speed of 1.2km/h will cause this switch to open or close once per second. So we just need to count how many times it is opening and closing per second to figure out a speed.

The Pi has an easy way of monitoring this sort of thing. Even though we have paused the entire program for a period of 15 seconds, we can create a background process that will still watch for changes in this pin and keep count while the main code is paused.

So lets get started: First we need to import the Raspberry Pi GPIO library, set up the pin we are using, and create the background process that monitors the pin. We will then calculate the windspeed and, finally, print the result.

				
					import time
from w1thermsensor import W1ThermSensor

import board
import busio
from adafruit_bme280 import basic as adafruit_bme280
i2c = busio.I2C(board.SCL, board.SDA)

import adafruit_ads1x15.ads1015 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

import RPi.GPIO as GPIO

bme = adafruit_bme280.Adafruit_BME280_I2C(i2c)
ads = ADS.ADS1015(i2c)
ads.gain = 1

ds18b20 = W1ThermSensor()

interval = 15 #How long we want to wait between loops (seconds)
windTick = 0 #Used to count the number of times the wind speed input is triggered

#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)

#Set digital pin 17 to an input and enable the pullup
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Event to detect wind (4 ticks per revolution)
GPIO.add_event_detect(17, GPIO.BOTH)
def windtrig(self):
    global windTick
    windTick += 1

GPIO.add_event_callback(17, windtrig)

while True:

    time.sleep(interval)
    
    #Pull Temperature from DS18B20
    temperature = ds18b20.get_temperature()
    
    #Pull temperature from BME280
    case_temp = bme.temperature
    
    #Pull pressure from BME280 Sensor & convert to kPa
    pressure_pa = bme.pressure
    pressure = pressure_pa / 10
    
    #Pull humidity from BME280
    humidity = bme.humidity
    
    #Calculate wind direction based on ADC reading
    chan = AnalogIn(ads, ADS.P0)
    val = chan.value
    windDir = "Not Connected"
    windDeg = 999
    
    if 20000 <= val <= 20500:
        windDir = "N"
        windDeg = 0
    
    if 10000 <= val <= 10500:
        windDir = "NNE"
        windDeg = 22.5
    
    if 11500 <= val <= 12000:
        windDir = "NE"
        windDeg = 45
    
    if 2000 <= val <= 2250:
        windDir = "ENE"
        windDeg = 67.5
    
    if 2300 <= val <= 2500:
        windDir = "E"
        windDeg = 90
    
    if 1500 <= val <= 1950:
        windDir = "ESE"
        windDeg = 112.5
    
    if 4500 <= val <= 4900:
        windDir = "SE"
        windDeg = 135
    
    if 3000 <= val <= 3500:
        windDir = "SSE"
        windDeg = 157.5
    
    if 7000 <= val <= 7500:
        windDir = "S"
        windDeg = 180
    
    if 6000 <= val <= 6500:
        windDir = "SSW"
        windDeg = 202.5
    
    if 16000 <= val <= 16500:
        windDir = "SW"
        windDeg = 225
    
    if 15000 <= val <= 15500:
        windDir = "WSW"
        windDeg = 247.5
    
    if 24000 <= val <= 24500:
        windDir = "W"
        windDeg = 270
    
    if 21000 <= val <= 21500:
        windDir = "WNW"
        windDeg = 292.5
    
    if 22500 <= val <= 23000:
        windDir = "NW"
        windDeg = 315
    
    if 17500 <= val <= 18500:
        windDir = "NNW"
        windDeg = 337.5
    
    #Calculate average windspeed over the last 15 seconds
    windSpeed = (windTick * 1.2) / interval
    windTick = 0
    
    #Print the results
    print( "Temperature: " , temperature)
    print( "Humidity: " , humidity, "%")
    print( "Pressure: " , pressure, "kPa")
    print( "Wind Dir: " , windDir, " (", windDeg, ")")
    print( "Wind Speed: " , windSpeed, "KPH")
    print( " ")
				
			
  • (12) Just like the other libraries, we import the GPIO library.
  • (21) Create a variable to store the number of times the wind sensor pin is triggered
  • (24) Set the GPIO pins to use Broadcom’s pin numbering
  • (27) Set pin 17 to be an input and enable the pullup
  • (30-35) Create a background process to keep track of how many times pin 17 transitions from HIGH to LOW or LOW to HIGH, each time it does we add 1 to the count
  • (125) Calculate the wind speed – 1.2km/h per tick per second. This will result in an average speed over the last 15 seconds
  • (126) Reset our counter
  • (133) Print the result

Once again, feel free to test the code again with the “F5” key. Spinning the wind speed sensor will result in an average speed over the last 15 seconds being printed every interval.

88.4%

Step 18 – Rainfall

Similar to the wind speed sensor, the rain gauge works with a magnet and a reed switch. Inside this sensor a small tipping device will switch back and forth every 0.2794mm of rain that falls. So we just need to count the number of times it switches back and forth and multiply to calculate the rainfall.

We are going to do this with another background process, similar to the one used for the wind speed sensor. Instead of counting transitions from high to low and low to high, this sensor requires we only count when the pin is falling from high to low to accurately capture the tipping gauge moving. For this reason we will set the event detect to “Falling”. Now it will ignore any change of low to high, and only increment our count when it goes from high to low. 

				
					import time
from w1thermsensor import W1ThermSensor

import board
import busio
from adafruit_bme280 import basic as adafruit_bme280
i2c = busio.I2C(board.SCL, board.SDA)

import adafruit_ads1x15.ads1015 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

import RPi.GPIO as GPIO

bme = adafruit_bme280.Adafruit_BME280_I2C(i2c)
ads = ADS.ADS1015(i2c)
ads.gain = 1

ds18b20 = W1ThermSensor()

interval = 15 #How long we want to wait between loops (seconds)
windTick = 0 #Used to count the number of times the wind speed input is triggered
rainTick = 0 #Used to count the number of times the rain input is triggered

#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)

#Set digital pin 17 to an input and enable the pullup
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Set digital pin 23 to an input and enable the pullup
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#Event to detect wind (4 ticks per revolution)
GPIO.add_event_detect(17, GPIO.BOTH)
def windtrig(self):
    global windTick
    windTick += 1

GPIO.add_event_callback(17, windtrig)

#Event to detect rainfall tick
GPIO.add_event_detect(23, GPIO.FALLING)
def raintrig(self):
    global rainTick
    rainTick += 1

GPIO.add_event_callback(23, raintrig)

while True:

    time.sleep(interval)
    
    #Pull Temperature from DS18B20
    temperature = ds18b20.get_temperature()
    
    #Pull temperature from BME280
    case_temp = bme.temperature
    
    #Pull pressure from BME280 Sensor & convert to kPa
    pressure_pa = bme.pressure
    pressure = pressure_pa / 10
    
    #Pull humidity from BME280
    humidity = bme.humidity
    
    #Calculate wind direction based on ADC reading
    chan = AnalogIn(ads, ADS.P0)
    val = chan.value
    windDir = "Not Connected"
    windDeg = 999
    
    if 20000 <= val <= 20500:
        windDir = "N"
        windDeg = 0
    
    if 10000 <= val <= 10500:
        windDir = "NNE"
        windDeg = 22.5
    
    if 11500 <= val <= 12000:
        windDir = "NE"
        windDeg = 45
    
    if 2000 <= val <= 2250:
        windDir = "ENE"
        windDeg = 67.5
    
    if 2300 <= val <= 2500:
        windDir = "E"
        windDeg = 90
    
    if 1500 <= val <= 1950:
        windDir = "ESE"
        windDeg = 112.5
    
    if 4500 <= val <= 4900:
        windDir = "SE"
        windDeg = 135
    
    if 3000 <= val <= 3500:
        windDir = "SSE"
        windDeg = 157.5
    
    if 7000 <= val <= 7500:
        windDir = "S"
        windDeg = 180
    
    if 6000 <= val <= 6500:
        windDir = "SSW"
        windDeg = 202.5
    
    if 16000 <= val <= 16500:
        windDir = "SW"
        windDeg = 225
    
    if 15000 <= val <= 15500:
        windDir = "WSW"
        windDeg = 247.5
    
    if 24000 <= val <= 24500:
        windDir = "W"
        windDeg = 270
    
    if 21000 <= val <= 21500:
        windDir = "WNW"
        windDeg = 292.5
    
    if 22500 <= val <= 23000:
        windDir = "NW"
        windDeg = 315
    
    if 17500 <= val <= 18500:
        windDir = "NNW"
        windDeg = 337.5
    
    #Calculate average windspeed over the last 15 seconds
    windSpeed = (windTick * 1.2) / interval
    windTick = 0
    
    #Calculate accumulated rainfall over the last 15 seconds
    rainFall = rainTick * 0.2794
    rainTick = 0
    
    #Print the results
    print( "Temperature: " , temperature)
    print( "Humidity: " , humidity, "%")
    print( "Pressure: " , pressure, "kPa")
    print( "Wind Dir: " , windDir, " (", windDeg, ")")
    print( "Wind Speed: " , windSpeed, "KPH")
    print( "Rainfall: " , rainFall, "mm")
    print( " ")
				
			
  • (22) Create a variable to store the number of times the rain sensor pin is triggered
  • (31) Set pin 23 to be an input and enable the pullup
  • (41-47) Create a background process to keep track of how many times pin 23 transitions from HIGH to LOW, each time it does we add 1 to the count
  • (141) Calculate the rainfall – 0.2794mm per tick. This will result in a total rainfall over the last 15 seconds
  • (142) Reset our counter
  • (150) Print the result
94.2%

Step 19 – All Done!

And that is it for our code! Hitting “F5” will run the program and should display all of the values every 15 seconds. Each sensor should respond, so feel free to try them all out. As always, if you have a question regarding the tutorial, please feel free to ask below!

In the next part of this tutorial we will be creating a Thingspeak account, modifying this program to send to Thingspeak, and looking at how this data can be used once it is logged there. Ready? Head on over to Part 3 of the Tutorial!

100%

126 thoughts on “Raspberry Pi Weather Station – Part 2”

Leave a Reply

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare