This project uses a Raspberry Pi and a DHT22 sensor to log temperature and humidity data to a local database and display it on a beautiful web dashboard.
- Automated Logging: Reads sensor data every minute.
- Data Persistence: Stores readings in a lightweight SQLite database.
- Web Dashboard: View real-time values and historical graphs on any device in your local network.
- Modern UI: Dark mode, responsive design, and interactive charts.
- Raspberry Pi (3B+, 4, or Zero W)
- DHT22 Sensor (AM2302)
- Jumper Wires (Female-to-Female or Female-to-Male depending on your header)
Connect the DHT22 sensor to the Raspberry Pi:
| DHT22 Pin | Raspberry Pi Pin |
|---|---|
| VCC (+) | 3.3V (Pin 1) |
| GND (-) | GND (Pin 9) |
| DATA | GPIO 4 (Pin 7) |
Note
If you are using a bare DHT22 module (white grid), you may need a 10k resistor between VCC and DATA. Most PCB-mounted modules (blue/black board) already include this.
-
Clone or Copy the Code Download this project to your Raspberry Pi, preferably at
/home/pi/pi_temp_sensor. -
Install System Dependencies The Adafruit DHT library requires
libgpiod3andpython3-pip.sudo apt-get update sudo apt-get install -y python3-pip libgpiod3
-
Install Python Dependencies
# Create a virtual environment (Recommended) python3 -m venv venv source venv/bin/activate # Install requirements pip install -r requirements.txt
If you cannot create a virtualenv, you can install globally with
pip3 install --break-system-packages -r requirements.txt(Raspberry Pi OS Bookworm+ requirement).
Open two terminal windows:
Terminal 1 (Logger):
python3 logger.pyYou should see temperature readings printed to the console.
Terminal 2 (Web Server):
python3 app.pyAccess the dashboard at http://<YOUR_PI_IP>:5000
-
Edit the Service Files Check
sensor-logger.serviceandsensor-web.serviceto ensure the paths match your installation (default assumes/home/pi/pi_temp_sensor). -
Install Services
sudo cp sensor-logger.service /etc/systemd/system/ sudo cp sensor-web.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable sensor-logger sudo systemctl enable sensor-web sudo systemctl start sensor-logger sudo systemctl start sensor-web
-
Check Status
sudo systemctl status sensor-logger sudo systemctl status sensor-web
Navigate to http://raspberrypi.local:5000 or http://<IP_ADDRESS>:5000 in your browser.
The charts will update every minute with new data properly stored in sensor_data.db.
- "Failed to retrieve data": DHT22 sensors can be finicky. The script retries automatically. Check wiring if it fails constantly.
- Permission Errors: Ensure the user running the script has GPIO access (
gpiogroup).