Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsFlask is a tiny Python web framework. With a DHT22 on a GPIO pin and the Adafruit CircuitPython DHT library, a Pi can read the sensor and serve a web page that shows the current temperature and humidity and refreshes itself. This project teaches GPIO sensing and web serving together on a normal Raspberry Pi OS install. The DHT22 library sometimes fails a read, so the code retries.
Tick anything you already have and the total updates.
microSD card for the operating system
Follow the colours: red is power, dark grey is ground, blue is a signal.
Official Pi 4 power supply.
dashboard.pyimport time
import adafruit_dht, board
from flask import Flask, jsonify
app = Flask(__name__)
dht = adafruit_dht.DHT22(board.D4)
PAGE = '''<meta name="viewport" content="width=device-width">
<h1>Room</h1><h2 id="t">--</h2><h2 id="h">--</h2>
<script>
async function go() {
const r = await fetch('/data'); const d = await r.json();
document.getElementById('t').textContent = d.t + ' C';
document.getElementById('h').textContent = d.h + ' %';
}
go(); setInterval(go, 5000);
</script>'''
def read():
for _ in range(5):
try:
return dht.temperature, dht.humidity
except RuntimeError:
time.sleep(2)
return None, None
@app.route("/")
def index():
return PAGE
@app.route("/data")
def data():
t, h = read()
return jsonify(t=t, h=h)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Re-check wiring. The DHT22 is slow and sometimes misses a read; the code retries.
Use the Pi's IP address and make sure the phone is on the same Wi-Fi.
Add --break-system-packages, or use a virtual environment.
Yes. Append each reading to a CSV file or use the Pi DS18B20 logger project's approach.
Yes, with the same wiring. Library support for GPIO on the Pi 5 is newer, so update your packages.
Not without opening your router. A VPN such as the WireGuard project is the safe way.
6 parts: Raspberry Pi 4 Model B 2GB RAM Single Board Computer, Official Raspberry Pi 4 USB-C Power Supply – 15.3W, Official Raspberry Pi 32GB A2 Class Micro SD Card, DHT22 AM2302 Digital Temperature & Humidity Sensor, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (dashboard.py).
About ₹8,593 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Reading a DHT22 from the Pi's GPIO; Serving a web page with Flask; Running a Python script at boot with systemd.
Ideas that reuse this build: Pi DS18B20 temperature logger. They are listed under Take it further on this page.
Your first MicroPython project: temperature and humidity on an OLED.
A web page with ON and OFF buttons that switches a relay over Wi-Fi.
A warm ring light that fades up at dusk and down at dawn.
Turn a Pi and a touchscreen into a home command centre with a clock and to-do list.
Learning project. Check every circuit and the datasheet of each part before you power it up.