Building an Interactive Raspberry Pi
Environmental Monitor with Django and DHT22
AIM:
To develop a Django web application that integrates with a DHT22
temperature and humidity sensor on a Raspberry Pi. The goal is to
create a real-time monitoring system where users can access a
webpage to view the current temperature and humidity readings.
Additionally, the system should provide visual feedback by
turning on an LED if the temperature exceeds a predefined
threshold value. This experiment combines web development with
hardware interfacing to create an interactive and responsive
environment for environmental monitoring using a Raspberry Pi.
PROCEDURE:
1. Hardware Setup:
- Connect the DHT22 sensor to the Raspberry Pi, ensuring proper
wiring for power, data, and ground.
- Connect an LED with a current-limiting resistor to act as an
indicator based on temperature.
2. Raspberry Pi Setup
- Ensure your Raspberry Pi is set up and connected to the internet.
- Install required libraries using `pip install [Link] Adafruit-
DHT`.
3. Create Django Project:
- Open a terminal on the Raspberry Pi.
- Create a new Django project using `django-admin startproject
myproj`.
4. Create Django App
- Create a Django app within the project using `python [Link]
startapp myapp`.
5. Update Settings and URLs:
- Add the created app ('myapp') to the `INSTALLED_APPS` list in
`myproj/[Link]`.
- Update the URL routing in `myproj/myproj/[Link]` to include a
path for the sensor display.
6. Implement Django View:
- In the `myapp/[Link]` file, implement a view function that reads
sensor values and controls the LED based on temperature.
7. Create HTML Template:
- Create an HTML template named `[Link]` in the
`myapp/templates/myapp` directory. This will be used to display the
sensor data.
8. Run Django Development Server:
- Apply migrations using `python [Link] migrate`.
- Start the Django development server with `python [Link]
runserver`.
9. Access Webpage:
- Open a web browser and navigate to
`[Link] to view real-time sensor data.
10. Observe LED Indicator:
- Observe the connected LED on the Raspberry Pi. It should provide
a visual indicator based on the temperature reading.
CIRCUIT DIAGRAM:
Code:
# myproj/myapp/[Link]
from [Link] import render
from [Link] import HttpResponse
import [Link] as gpio
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 21 # GPIO 21 in BCM
ledpin = 20
[Link]([Link])
thresh_tmp = 25
def display(request):
try:
# Read sensor values
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print(f'TEMP: {temperature:.2f} C')
print(f'HUMIDITY: {humidity:.2f} %')
[Link](ledpin, [Link])
if temperature > thresh_tmp:
[Link](ledpin, [Link])
else:
[Link](ledpin, [Link])
# Format the message for rendering
newstr = f'TEMP: {temperature:.2f} C, HUMIDITY:
{humidity:.2f}%'
else:
print("Failed to sense")
newstr = "Failed to sense"
return render(request, 'myapp/[Link]', {'newstr': newstr})
except KeyboardInterrupt:
print("Exception occurred")
# myproj/myproj/[Link]
from [Link] import path
from [Link] import display
urlpatterns = [
path('display/', display, name='display'),
# ... other paths
]
# Create an HTML template for rendering the sensor values
# myproj/myapp/templates/myapp/[Link]
'''
<!DOCTYPE html>
<html>
<head>
<title>Sensor Display</title>
</head>
<body>
<h1>Sensor Data</h1>
<p>{{ newstr }}</p>
</body>
</html>
'''
# Apply migrations and run the Django development server
python [Link] migrate
python [Link] runserver
Result:
DHT22 Sensor is used to sense the temperature and humidity
value, which are then displayed on the web page created using
Django.
If the value of the temperature is greater than a predefined
threshold value, the led is turned on.