Virtualization and ContainersRaspberry Pi HostingIntermediate2-3 hoursLab build

Build a Raspberry Pi Environmental Monitor for Rack Temperature, Power State, and Service Alerts

By following this guide, you will create a Raspberry Pi-based environmental monitor that tracks rack temperature, power state, and provides service alerts.

Last reviewed4/30/2026
Pi container hosts
Raspberry PiPythonFlaskSQLiteDHT22 SensorRelay Module

Expected Outcome

A fully functional Raspberry Pi environmental monitor that logs temperature data, checks power state, and sends alerts for service issues.

Assumptions

  • Raspberry Pi 3 or later
  • MicroSD card (16GB or larger)
  • Power supply for Raspberry Pi
  • DHT22 temperature and humidity sensor
  • Relay module for power state monitoring
  • Jumper wires
  • Breadboard (optional)
  • Internet connection

Bill of Materials

  • Raspberry Pi OS (Raspbian) installed on the MicroSD card
  • Python 3
  • Flask for web server
  • SQLite for data storage
  • DHT library for temperature sensor
  • GPIO library for relay control

Build Steps

  1. Set up the Raspberry Pi

    Prepare your Raspberry Pi with the necessary software and libraries.

    Changes system state: review before running

    sudo apt update
    sudo apt upgrade -y
    sudo apt install python3 python3-pip -y
    pip3 install flask sqlite3 Adafruit-DHT RPi.GPIO
  2. Connect the DHT22 sensor to the Raspberry Pi

    Wire the DHT22 sensor to the GPIO pins on the Raspberry Pi.

    Example pattern only. Adjust for your environment before running.

    # Connect VCC to 3.3V
    # Connect GND to Ground
    # Connect Data to GPIO pin (e.g., GPIO4)
  3. Connect the relay module for power state monitoring

    Wire the relay module to control and monitor the power state.

    Example pattern only. Adjust for your environment before running.

    # Connect VCC to 5V
    # Connect GND to Ground
    # Connect IN pin to GPIO pin (e.g., GPIO17)
  4. Create the monitoring script

    Write a Python script to read data from the DHT22 sensor and check the power state.

    Example pattern only. Adjust for your environment before running.

    nano monitor.py
    # Add the following code to monitor.py:
    import Adafruit_DHT
    import RPi.GPIO as GPIO
    import sqlite3
    # Your code to read sensor data and log to SQLite
  5. Set up a web server to display data

    Use Flask to create a simple web interface to view the monitored data.

    Example pattern only. Adjust for your environment before running.

    nano app.py
    # Add Flask code to serve the data from SQLite
    from flask import Flask, render_template
    app = Flask(__name__)
    # Your code to route data to the front end
  6. Run the monitoring script and web server

    Execute the monitoring script and start the Flask web server.

    Example pattern only. Adjust for your environment before running.

    python3 monitor.py &
    python3 app.py

Validation

  • Access the web interface via http://<Raspberry_Pi_IP>:5000 to view real-time data.
  • Check the SQLite database to ensure data is being logged correctly.

Troubleshooting

  • If the DHT22 sensor is not responding, check the wiring and ensure the GPIO pin is correctly configured.
  • If the web server is not accessible, verify that Flask is running and the correct IP address is used.

Cleanup or Rollback

  • To stop the monitoring script and web server, use 'pkill python3' to terminate the processes.
  • Remove any temporary files created during the setup.

Next Improvements

  • Consider integrating email or SMS alerts for critical service issues.
  • Expand the project by adding additional sensors or monitoring capabilities.