0
0
Iot-protocolsHow-ToBeginner · 4 min read

Build an IoT Weather Station with Raspberry Pi: Step-by-Step Guide

To build an IoT weather station with Raspberry Pi, connect sensors like temperature and humidity to the Pi's GPIO pins, then use Python to read sensor data and send it online via Wi-Fi. Use libraries like Adafruit_DHT for sensors and requests to post data to a cloud service or dashboard.
📐

Syntax

Here is the basic syntax to read data from a DHT11 temperature and humidity sensor connected to Raspberry Pi using Python:

  • import Adafruit_DHT: Imports the sensor library.
  • sensor = Adafruit_DHT.DHT11: Defines the sensor type.
  • pin = 4: Sets the GPIO pin number where the sensor is connected.
  • humidity, temperature = Adafruit_DHT.read_retry(sensor, pin): Reads sensor data with retries.
  • print(): Displays the data.
python
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11
pin = 4

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:
    print(f"Temp={temperature:.1f}C  Humidity={humidity:.1f}%")
else:
    print("Failed to get reading. Try again!")
Output
Temp=23.4C Humidity=45.2%
💻

Example

This example shows how to read temperature and humidity from a DHT11 sensor and send the data to a free IoT cloud service using HTTP POST requests.

python
import Adafruit_DHT
import requests
import time

sensor = Adafruit_DHT.DHT11
pin = 4
url = "https://api.thingspeak.com/update"
api_key = "YOUR_WRITE_API_KEY"

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
        print(f"Temp={temperature:.1f}C  Humidity={humidity:.1f}%")
        payload = {
            'api_key': api_key,
            'field1': temperature,
            'field2': humidity
        }
        response = requests.post(url, data=payload)
        if response.status_code == 200:
            print("Data sent successfully")
        else:
            print("Failed to send data")
    else:
        print("Sensor read failed")
    time.sleep(60)
Output
Temp=23.4C Humidity=45.2% Data sent successfully ... (repeats every 60 seconds)
⚠️

Common Pitfalls

  • Incorrect GPIO pin number causes sensor read failures.
  • Not installing required libraries like Adafruit_DHT or requests.
  • Using wrong sensor type in code (e.g., DHT22 instead of DHT11).
  • Network issues prevent data from sending to cloud.
  • Forgetting to replace YOUR_WRITE_API_KEY with your actual API key.

Always test sensor reading locally before adding network code.

python
import Adafruit_DHT

# Wrong pin example
sensor = Adafruit_DHT.DHT11
pin = 17  # Wrong pin if sensor is on GPIO4

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is None or temperature is None:
    print("Failed to get reading. Check sensor connection and pin number.")

# Correct pin example
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
    print(f"Temp={temperature:.1f}C  Humidity={humidity:.1f}%")
Output
Failed to get reading. Check sensor connection and pin number. Temp=23.4C Humidity=45.2%
📊

Quick Reference

Summary tips for building your IoT weather station:

  • Use DHT11 or DHT22 sensors for temperature and humidity.
  • Connect sensors to Raspberry Pi GPIO pins (usually GPIO4).
  • Install Python libraries: pip install Adafruit_DHT requests.
  • Test sensor reading locally before sending data online.
  • Use free IoT platforms like ThingSpeak to visualize data.
  • Ensure Raspberry Pi has internet access for cloud communication.

Key Takeaways

Connect sensors properly to Raspberry Pi GPIO pins and use correct pin numbers in code.
Use Python libraries like Adafruit_DHT to read sensor data easily.
Test sensor readings locally before sending data to the cloud.
Send data to IoT platforms using HTTP POST requests with your API key.
Check network and API key settings to avoid data transmission failures.