"""
IoT project for real-time temperature and humidity monitoring using Raspberry Pi and DHT11 sensor with Flask web dashboard.
This system interfaces a DHT11 digital sensor with a Raspberry Pi to monitor environmental conditions and display the data on a responsive web dashboard that auto-updates every 2 seconds.
- ✅ Interface DHT11 sensor with Raspberry Pi GPIO
- ✅ Read temperature and humidity data using Python
- ✅ Display readings on terminal at regular intervals
- ✅ Create Flask-based web dashboard for live data visualization
- ✅ Understand GPIO pin configuration and sensor protocols
- Raspberry Pi (any model with GPIO pins)
- DHT11 Temperature & Humidity Sensor
- 10kΩ Resistor (pull-up)
- Jumper wires
- Breadboard (optional)
DHT11 Sensor Pinout:
┌─────────────┐
│ DHT11 │
│ ┌───────┐ │
│ │ Front │ │
└──┴───────┴──┘
│ │ │ │
1 2 3 4
Pin 1 (VCC) → Raspberry Pi 3.3V (Pin 1) or 5V (Pin 2)
Pin 2 (DATA) → Raspberry Pi GPIO4 (Pin 7) + 10kΩ resistor to VCC
Pin 3 (NC) → Not Connected
Pin 4 (GND) → Raspberry Pi Ground (Pin 6)
git clone https://github.com/YOUR_USERNAME/dht11-raspberry-pi-monitor.git
cd dht11-raspberry-pi-monitorsudo apt-get update
sudo apt-get install python3-pip
pip3 install -r requirements.txt- Connect DHT11 sensor to Raspberry Pi GPIO4
- Add 10kΩ pull-up resistor between DATA and VCC
- Verify connections
Edit config.py to match your setup:
DHT_GPIO_PIN = 4 # Change if using different GPIO# Uncomment hardware lines in app.py first!
python3 app.pyOpen browser: http://localhost:5000
- Real-time Monitoring: Updates every 2 seconds
- Responsive Design: Works on mobile and desktop
- REST API:
/api/sensor-dataendpoint for integration - Error Handling: Graceful failure on sensor read errors
- Status Indicators: Visual feedback for sensor status
- Uses BCM (Broadcom) pin numbering
- GPIO4 = Physical pin 7 on 40-pin header
- Configured as INPUT with pull-up resistor
- Start Signal: MCU pulls DATA LOW for 18ms
- Response: Sensor pulls DATA LOW for 80μs, then HIGH for 80μs
- Data Transmission: 40 bits sent (5 bytes)
- Byte 1: Humidity integer
- Byte 2: Humidity decimal (always 0 for DHT11)
- Byte 3: Temperature integer
- Byte 4: Temperature decimal (always 0 for DHT11)
- Byte 5: Checksum (sum of bytes 1-4)
- Bit Encoding:
- 0 bit: 50μs LOW + 26-28μs HIGH
- 1 bit: 50μs LOW + 70μs HIGH
- Backend: Flask serves API and web pages
- Frontend: HTML/CSS/JavaScript for dashboard
- AJAX: Periodic polling for live updates
- REST API: JSON responses for sensor data
Returns current sensor readings
{
"temperature": 25.5,
"humidity": 60.2,
"timestamp": "2024-11-05 14:30:22",
"status": "success"
}Returns sensor configuration
{
"sensor_type": "DHT11",
"gpio_pin": 4,
"update_interval": 2,
"temp_range": "0-50°C",
"humidity_range": "20-90%"
}Sensor reads return None:
- Check wiring connections
- Verify GPIO pin number
- Ensure pull-up resistor is connected
- Try different GPIO pin
Permission denied error:
- Run with sudo:
sudo python3 app.py - Or add user to gpio group:
sudo usermod -a -G gpio $USER
Module not found:
- Reinstall:
pip3 install --upgrade -r requirements.txt - Check Python version:
python3 --version(needs 3.7+)
Your Name - Next Tech Lab Assignment