Hardware Requirements
To build portable weather forecast device, below are the hardware requirements which are easily available in market at reasonable prices.
- Raspberry Pi4 Model B
- Weather sensors: Temperature and humidity (DHT22), Atmospheric pressure (BMP280), Wind speed and direction (Anemometer), Rainfall (Rain Gauge)
- Breadboard
- Jumper wires (male to male, male to female)
- Resistors
- Power supply (5V, 2.5 Amp)
- MicroSD card (minimum 16GB compatible with Raspberry Pi OS)
- A weatherproof enclosure
Software Requirements
- Raspberry Pi OS
- Python 3 (minimum)
- Python Libraries – Adafruit_DHT, Adafruit_BMP280, py-sds011, RPi.GPIO, numpy, pandas, matplotlib.
How to assemble portable weather device
- Firmly mount the Raspberry Pi 4 Model B board in the weather proof enclosure.
- Connect the DHT22, BMP280, SDS011, anemometer, then rain gauge sensors to the breadboard.
- Use jumper wires to connect the sensors to the Raspberry Pi GPIO pins as follows:
- DHT22
- Connect the DHT22 data pin to GPIO4 (pin 7) on the Raspberry Pi then connect the DHT22 VCC (3.3V) and GND pins to the Raspberry Pi’s into 3.3V and GND pins respectively.
- BMP280
- Connect the BMP280 SDA pin to GPIO2(pin 3) on the Raspberry Pi then connect the BMP280 SCL pin to GPIO3( pin 5) on the Raspberry Pi. Connect the BMP280 VCC (3.3V) and GND pins to the Raspberry Pi’s into 3.3V and GND pins respectively.
- SDS011
- Connect the SDS011 TX pin to GPIO14 (pin 8) on the Raspberry Pi then connect the SDS011 RX pin to GPIO15 (pin 10) on the Raspberry Pi. Connect the SDS011 5V and GND pins to the Raspberry Pi’s into 5V and GND pins respectively.
- Anemometer
- Connect the anemometer data pin to GPIO17 (pin 11) on the Raspberry Pi then connect the anemometer VCC(3.3V) and GND pins to the Raspberry Pi’s respective 3.3V and GND pins.
- Rain Gauge
- Connect the rain gauge data pin to GPIO18(pin 12) on the Raspberry Pi. Connect the rain gauge VCC (3.3V) and GND pins to Raspberry Pi’s into 3.3V and GND pins respectively.
- DHT22
- Now connect the required resistors as well as the power supply to Raspberry Pi.
Programming code using Python
- To start programming code using python run the below command in python in order to install necessary libraries.
- pip3 install Adafruit_DHT Adafruit_BMP280 py-sds011 Rpi.GPIO numpy pandas mat-plotlib
- pip3 install Adafruit_DHT Adafruit_BMP280 py-sds011 Rpi.GPIO numpy pandas mat-plotlib
- Now, you can create a Python script in pycharm such as Weather_Device.py with the following programming code using Python
Import time
Import Adafruit_DHT
Import Adafruit_BMP280
Import py_sds011
Import RPi.GPIO as GPIO
Import numpy as np
Import pandas as pd
Import matplotlib.pyplot as plt
# Initialize DHT22
DHT_SENSOR = Adafruit_DHT.
DHT22
DHT_PIN = 4
#Initialize BMP280
bmp280 = Adafruit_BMP280.
Adafruit_BMP280(busnum = 1)
#Initialize SDS011
sds011 = py_sds011
SDS011(“/dev/ttyS0”, use_query_mode=True)
sds011.set_working_period(0) #Continuous mode
#Initialize anemometer as well as rain gauge
ANEMOMETER_PIN = 17
RAIN_GAUGE_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(ANEMOMETER_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(RAIN_GAUGE_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
wind_count = 0
rain_count = 0
def wind_callback(channel):
global wind_count
wind_count += 1
def rain_callback(channel):
global rain_count
rain_count += 1
GPIO.add_event_detect(ANEMOMETER_PIN, GPIO.FALLING, callback=wind_callback, bouncetime=300)
GPIO.add_event_detect(RAIN_GAUGE_PIN, GPIO.FALLING, callback=rain_callback, bouncetime=300)
def read_dht22():
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
return temperature, humidity
def read_bmp280():
pressure = bmp280.read_pressure() / 100.0
return pressure
def read_sds011():
pm25, pm10 = sds011.query()
return pm25, pm10
def read_anemometer():
global wind_count
wind_speed = wind_count * 1.2 / 5.0 # m/s
wind_count =0
return wind_speed
def read_rain_gauge():
global rain_count
rainfall = rain_count * 0.2794 # mm
rain_count = 0
return rainfall
if name == “main”:
try:
while true:
temperature, humidity = read_dht22()
pressure = read_bmp280()
pm2_5, pm10 = read_sds011() wind_speed = read_anemometer()
rainfall = read_rain_gauge()
# Now process the data and display with following code
print(f’Temperature: {temperature} C, Humidity: {humidity}%, Pressure: {pressure} hPa’)
print(f’PM2.5: {pm2_5} ug/m3, PM10: {pm10} ug/m3′)
print(f’Wind speed: {wind_speed} m/s’)
print(f’Rainfall: {rainfall} mm’)
time.sleep(60)
except KeyboardInterrupt: GPIO.cleanup()
After you write the above code, you will need to understand the operation to make the portable weather device functional.
How to operate
- Now plugin and power on the raspberry Pi then connect it with internet.
- Transfer the above python code (Weather_Device.py) to raspberry Pi with the help of scp client, SSH or any other method of your choice then follow the below steps.
- Now run the following command to make the code executable.
chmod +x weather_station.py - Run below script.
./weather_station.py - Now the code will start to read the data from sensors then display it on the console.
- You can make the run script automatically on startup in raspberry pi. To do so, edit the /etc/rc.local file then add below line before exit 0 line. /path/to/weather_device.py &
Note: The above code is just implementation. Hence in addition, you may need to adjust the values as well as calculations based on specific sensors and their datasheets to build portable weather device.