Select Page

Hardware Requirements
To build portable weather forecast device, below are the hardware requirements which are easily available in market at reasonable prices.

  1. Raspberry Pi4 Model B
  2. Weather sensors: Temperature and humidity (DHT22), Atmospheric pressure (BMP280), Wind speed and direction (Anemometer), Rainfall (Rain Gauge)
  3. Breadboard
  4. Jumper wires (male to male, male to female)
  5. Resistors
  6. Power supply (5V, 2.5 Amp)
  7. MicroSD card (minimum 16GB compatible with Raspberry Pi OS)
  8. A weatherproof enclosure

Software Requirements

  1. Raspberry Pi OS
  2. Python 3 (minimum)
  3. Python Libraries – Adafruit_DHT, Adafruit_BMP280, py-sds011, RPi.GPIO, numpy, pandas, matplotlib.

How to assemble portable weather device

  1. Firmly mount the Raspberry Pi 4 Model B board in the weather proof enclosure.
  2. Connect the DHT22, BMP280, SDS011, anemometer, then rain gauge sensors to the breadboard.
  3. 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.
  4. Now connect the required resistors as well as the power supply to Raspberry Pi.

Programming code using Python

  1. 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
  2. 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()

How to operate

  1. Now plugin and power on the raspberry Pi then connect it with internet.
  2. 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.
  3. Now run the following command to make the code executable.
    chmod +x weather_station.py
  4. Run below script.
    ./weather_station.py
  5. Now the code will start to read the data from sensors then display it on the console.
  6. 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.

error: Content is protected !!