Skip to main content

Command Palette

Search for a command to run...

Room Monitor using Flask Server and DHT Sensor

Updated
β€’2 min read
Room Monitor using Flask Server and DHT Sensor

πŸ“ Project Structure

room_monitor/
β”œβ”€β”€ app.py
β”œβ”€β”€ templates/
β”‚   └── index.html

app.py

from flask import Flask, render_template
import Adafruit_DHT

app = Flask(__name__)

DHT_SENSOR = Adafruit_DHT.DHT11  # or DHT22
DHT_PIN = 4  # GPIO4

@app.route('/')
def index():
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
    if humidity is not None and temperature is not None:
        status = "Success"
    else:
        status = "Sensor error"
        temperature = humidity = None
    return render_template('index.html', temperature=temperature, humidity=humidity, status=status)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Room Monitor</title>

    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Roboto', sans-serif;
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .card {
            background: #fff;
            padding: 30px 40px;
            border-radius: 20px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            text-align: center;
            max-width: 350px;
            width: 100%;
        }

        h1 {
            color: #222;
            margin-bottom: 25px;
            font-size: 28px;
        }

        p {
            font-size: 20px;
            margin: 15px 0;
            color: #333;
        }

        .status {
            font-weight: bold;
            margin-top: 20px;
            font-size: 16px;
            color: green;
        }

        button {
            margin-top: 20px;
            padding: 10px 25px;
            font-size: 16px;
            background-color: #007BFF;
            border: none;
            color: white;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="card">
        <h1>🌑️ Room Monitor</h1>
        <p><strong>Temperature:</strong> {{ temperature }} &deg;C</p>
        <p><strong>Humidity:</strong> {{ humidity }} %</p>
        <p class="status">{{ status }}</p>
        <button onclick="location.reload()">πŸ”„ Refresh</button>
    </div>
</body>
</html>

To auto-reload the page every 5 seconds using JavaScript, you can add a simple <script> tag before the closing </body> tag. Here's your updated HTML with the auto-reload functionality:

<script>
        setTimeout(function() {
            location.reload();
        }, 5000); // Reload every 5 seconds
</script>

index.html with auto reload

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Room Monitor</title>

    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Roboto', sans-serif;
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .card {
            background: #fff;
            padding: 30px 40px;
            border-radius: 20px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            text-align: center;
            max-width: 350px;
            width: 100%;
        }

        h1 {
            color: #222;
            margin-bottom: 25px;
            font-size: 28px;
        }

        p {
            font-size: 20px;
            margin: 15px 0;
            color: #333;
        }

        .status {
            font-weight: bold;
            margin-top: 20px;
            font-size: 16px;
            color: green;
        }

        button {
            margin-top: 20px;
            padding: 10px 25px;
            font-size: 16px;
            background-color: #007BFF;
            border: none;
            color: white;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="card">
        <h1>🌑️ Room Monitor</h1>
        <p><strong>Temperature:</strong> {{ temperature }} &deg;C</p>
        <p><strong>Humidity:</strong> {{ humidity }} %</p>
        <p class="status">{{ status }}</p>
        <button onclick="location.reload()">πŸ”„ Refresh</button>
    </div>

    <script>
        setTimeout(function() {
            location.reload();
        }, 5000); // Reload every 5 seconds
    </script>
</body>
</html>

More from this blog

Aarav's Blogs

19 posts