A lightweight environmental monitoring system for server rooms or any space where temperature and humidity tracking is critical. Built on a Raspberry Pi Zero 2 W with a Sense HAT.
- Real-time Temperature Monitoring: Measures ambient temperature with hardware compensation for CPU heat
- Humidity Tracking: Monitors relative humidity percentage
- Web Dashboard: Clean, responsive web interface automatically refreshes every 60 seconds
- API Endpoints: JSON data access for integration with other monitoring systems
- LED Display: Shows current temperature on the Sense HAT LED matrix
- Logging: Records all measurements to a log file
- Raspberry Pi (Zero 2 W or other model)
- Sense HAT add-on board
- Power supply
- (Optional) Case for the Raspberry Pi
# Install required system packages
sudo apt-get update
sudo apt-get install -y python3-pip python3-sense-hat
# Create a virtual environment (optional but recommended)
python3 -m venv venv
source venv/bin/activate
# Install Python dependencies
pip install flask-
Clone this repository:
git clone https://github.com/yourusername/temp_monitor.git cd temp_monitor -
Configure environment variables: Copy
.env.exampleto.envand customize paths as needed:cp .env.example .env
Edit
.envto set your paths:# Log file path (absolute or relative) LOG_FILE=/home/yourusername/temp_monitor.logStatic assets (logo and favicon) are served from the repository's
static/directory by default. Replace the files there if you want to customize the images. -
Generate a bearer token:
python generate_token.py
This will create a secure token and save it to
.env. -
Set up as a service (for automatic startup): Create a systemd service file:
sudo nano /etc/systemd/system/temp_monitor.service
Add the following content:
[Unit] Description=Temperature Monitor Service After=network.target [Service] User=yourusername WorkingDirectory=/home/yourusername/temp_monitor ExecStart=/home/yourusername/temp_monitor/venv/bin/python3 temp_monitor.py Restart=always RestartSec=10 [Install] WantedBy=multi-user.targetEnable and start the service:
sudo systemctl enable temp_monitor.service sudo systemctl start temp_monitor.service
The application can be deployed as a Docker container, making it easier to manage dependencies and deployment.
- Docker and Docker Compose installed on your Raspberry Pi
- Raspberry Pi with ARM architecture (armv7l or aarch64)
- Sense HAT hardware properly connected
-
Create a logs directory:
mkdir -p logs
-
(Optional) Replace static assets: The container serves images from the built-in
static/directory. If you want to override them, replace the files instat ic/before building the image or mount your ownstatic/directory at runtime. -
Create a .env file:
cp .env.example .env
The bearer token will be auto-generated on first run, or you can generate it manually (see below).
-
Build the Docker image:
docker-compose build
-
Start the container:
docker-compose up -d
-
View logs:
docker-compose logs -f
-
Stop the container:
docker-compose down
To generate or regenerate the bearer token inside the container:
docker-compose exec temp-monitor python generate_token.pyThe token will be saved to the .env file in your project directory (which is mounted as a volume).
If you prefer to build and run without docker-compose:
# Build the image
docker build -t temp-monitor .
# Run the container
docker run -d \
--name temp-monitor \
--privileged \
-p 8080:8080 \
-v $(pwd)/logs:/app/logs \
-v $(pwd)/static:/app/static:ro \
-v $(pwd)/.env:/app/.env \
-v /sys:/sys:ro \
--device /dev/i2c-1:/dev/i2c-1 \
-e LOG_FILE=/app/logs/temp_monitor.log \
temp-monitor- Privileged Mode: The container requires privileged mode to access the I2C interface and hardware sensors on the Sense HAT
- ARM Architecture: This application is designed for ARM-based Raspberry Pi. The Python base image will automatically use the appropriate ARM variant
- Device Access: The container needs access to
/dev/i2c-1for Sense HAT communication and/sys(read-only) for CPU temperature readings - Persistent Data: Logs and the
.envfile are stored in mounted volumes, so they persist across container restarts - Auto-restart: The docker-compose configuration includes
restart: unless-stoppedto automatically restart the container if it crashes or after system reboot
Access the web dashboard by navigating to:
http://[raspberry-pi-ip-address]:8080
The dashboard will automatically refresh every 60 seconds.
GET http://[raspberry-pi-ip-address]:8080/api/temp
Returns:
{
"temperature_c": 23.5,
"temperature_f": 74.3,
"humidity": 45.2,
"timestamp": "2023-09-19 14:23:45"
}GET http://[raspberry-pi-ip-address]:8080/api/raw
Returns:
{
"cpu_temperature": 54.2,
"raw_temperature": 32.6,
"compensated_temperature": 23.5,
"humidity": 45.2,
"timestamp": "2023-09-19 14:23:45"
}The system compensates for the effect of CPU heat on temperature readings using a formula:
compensated_temp = raw_temp - ((cpu_temp - raw_temp) * factor)
Where factor is a calibration value (default 0.7) that may need adjustment based on your specific hardware configuration and enclosure.
To change how often temperature readings are updated, modify the sampling_interval variable (in seconds):
sampling_interval = 60 # seconds between temperature updatesThe web interface uses an embedded HTML template with CSS. You can customize the appearance by modifying the HTML template in the index() function.
The application uses environment variables for configuration. Create a .env file (copy from .env.example) with these settings:
- LOG_FILE: Path to the log file (defaults to
temp_monitor.log) - BEARER_TOKEN: API authentication token (auto-generated if not provided)
- Static assets: Images are served from the
static/directory. Replacestatic/My-img8bit-1com-Effect.giforstatic/f avicon.icoif you need custom artwork.
All paths can be absolute or relative. The application will create the log directory if it doesn't exist.
- Sense HAT not detected: Ensure the HAT is properly connected and that I2C is enabled (use
sudo raspi-config) - Web interface not accessible: Check that port 8080 is not blocked by a firewall
- Inaccurate temperature: Adjust the compensation factor in the
get_compensated_temperature()function - Favicon not displaying: Verify
static/favicon.icoexists and is being served - Log file creation fails: Ensure the directory specified in
LOG_FILEexists or that the user has permission to create it
Contributions are welcome! Please feel free to submit a Pull Request.
This application monitors temperature and humidity using a Raspberry Pi with Sense HAT and provides a web interface and API endpoints to access the data.
The API endpoints are protected with Bearer Token authentication. You need to include a valid token in the Authorization header to access the API.
-
Install the required dependencies:
pip install -r requirements.txt
-
Configure your environment (see Setup section above for details)
-
Start the application:
python temp_monitor.py
To access the API endpoints, include the bearer token in the Authorization header:
curl -H "Authorization: Bearer YOUR_TOKEN_HERE" http://your-server:8080/api/temp/api/temp- Get current temperature and humidity data/api/raw- Get raw temperature data (including CPU temperature)/api/verify-token- Verify if your token is valid/api/generate-token- Generate a new token (requires existing valid token)
You can regenerate the token in two ways:
-
Using the script:
python generate_token.py -
Using the API (requires existing valid token):
curl -X POST -H "Authorization: Bearer YOUR_CURRENT_TOKEN" http://your-server:8080/api/generate-token
- Keep your bearer token secure and don't share it publicly
- The token is stored in the
.envfile, which should be kept private - Consider regenerating the token periodically for enhanced security
