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 log file location: Edit the
temp_monitor.pyfile to update the log file path if needed:logging.basicConfig( filename='/home/yourusername/temp_monitor.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s' )
-
(Optional) Add a custom logo: Place your image file at the location specified in the code:
with open("/home/yourusername/logo.gif", "rb") as image_file:
-
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
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.
- 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
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 -
Generate a bearer token:
python generate_token.pyThis will create a secure random token and save it to the
.envfile. -
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
