This repository contains a Python-based project for monitoring CO2 levels, temperature, and humidity using a USB-zyTemp CO2 sensor connected to a Raspberry Pi. The project includes automated control (e.g., turning on a fan when CO2 levels exceed a threshold) and a web service for visualizing sensor data in real time.
graph TD
A[CO2 Sensor] -->|USB data| B[co2_sensor.py]
B -->|Sensor data| C[(sensor_data SQLite DB)]
C -->|Read data| D[monitor.py]
D -->|Check CO2 levels| E[Automation: Fan Control]
C -->|Read data| F[app.py]
F -->|Serve data| G[Web Interface]
%% Descriptions for each interaction
B -->|Write data| C
F -->|Displays sensor data| G
D -->|Activates fan if CO2 > threshold| E
- CO2 Sensor: The physical sensor device connected via USB.
co2_sensor.py:- Reads data from the CO2 sensor.
- Writes CO2, temperature, and humidity readings to the SQLite database (
sensor_data).
sensor_dataSQLite DB:- Stores the CO2, temperature, and humidity readings for logging and analysis.
monitor.py:- Reads data from the database.
- Checks if CO2 levels exceed a set threshold, and triggers the Fan Control if necessary.
app.py:- Reads data from the database.
- Serves the data to the Web Interface via a Flask web server.
- Web Interface:
- Displays real-time CO2, temperature, and humidity data.
- CO2 Monitoring: Reads CO2 levels (in ppm) from the sensor.
- Temperature Monitoring: Reads temperature data (in °C).
- Humidity Monitoring: Reads humidity data (in %).
- Data Logging: Logs sensor data to an SQLite database.
- Automation: Automatically activates a fan when CO2 levels exceed a set threshold.
- Web Interface: Serves a simple web interface to visualize sensor data in real time.
- Raspberry Pi (tested on Raspberry Pi Zero W)
- USB-zyTemp CO2 sensor (Holtek Semiconductor)
- Python 3.x
- Python packages:
hidapiFlasksqlite3datetime
- Clone the repository:
git clone https://github.com/isheglov/co2-sensor-script.git
cd co2-sensor-script
cp .env.dist .env- Set up your Python environment:
If you are using a virtual environment, create and activate it:
python3 -m venv hid_env
source hid_env/bin/activate- Install the required packages:
pip install -r requirements.txt- Create the SQLite Database:
Run createDB.py to initialize the database:
python3 createDB.py- Connect the CO2 Sensor:
Ensure the USB-zyTemp CO2 sensor is connected to the Raspberry Pi.
- Run the script:
python3 co2_sensor.py- Start the Web Server:
Run the Flask app to start the web interface:
python3 web_service/app.pyBy default, the web server will run on http://localhost:5000. You can access this in a browser to view real-time CO2, temperature, and humidity data.
To automatically activate a fan when CO2 levels exceed a specified threshold, run the monitor.py script in the automation/ folder. This script will continuously monitor sensor readings and trigger the fan when necessary.
python3 automation/monitor.pyTo continuously run the script in the background on a Raspberry Pi using systemd, follow these steps:
- Create a systemd service file:
sudo nano /etc/systemd/system/co2sensor.service- Add the following content:
[Unit]
Description=CO2 Sensor Monitoring Service
After=network.target
[Service]
ExecStart=/home/pi/myenv/bin/python /home/pi/co2_sensor.py
WorkingDirectory=/home/pi/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
- Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable co2sensor.service
sudo systemctl start co2sensor.serviceThe web interface provides real-time data visualization and displays the current CO2, temperature, and humidity readings. Access it at http://localhost:5000 (or replace localhost with your Raspberry Pi’s IP address if accessing from another device).
sudo systemctl restart myapp.service- /: Main dashboard for real-time data visualization.
- /current: Shows the latest sensor readings.
This project is licensed under the MIT License - see the LICENCE file for details.
The project includes a comprehensive test suite to verify functionality of the CO2 sensor code.
To run all tests:
# Activate your virtual environment first
source hid_env/bin/activate
# Run all tests with pytest
pytest tests/To run a specific test file:
pytest tests/test_co2_sensor.pyTo run a specific test method:
pytest tests/test_co2_sensor.py::TestCO2Sensor::test_parse_data_co2The tests cover:
- CO2, temperature, and humidity data parsing
- Database operations
- Device communication and error handling
- Edge cases and invalid data handling
When adding new functionality, please include appropriate tests following these guidelines:
- Use unittest or pytest frameworks
- Mock external dependencies (database, hardware devices)
- Use descriptive test method names
- Include docstrings describing what each test verifies
This project uses GitHub Actions for continuous integration:
-
Pylint: Automatically checks code quality on every push
- Verifies code against PEP 8 style guidelines
- Runs on Python 3.8, 3.9, and 3.10
-
Pytest: Runs the test suite on every push and pull request
- Executes all unit tests
- Generates test coverage reports
- Runs on Python 3.8, 3.9, and 3.10
GitHub Actions workflow files can be found in .github/workflows/.
Contributions are welcome! Please fork the repository and submit a pull request.
- Ensure your code follows the style guidelines in CLAUDE.md
- Add tests for new functionality
- Verify all tests pass before submitting your PR
- Maintain or improve test coverage
If you have any questions or issues, feel free to open an issue in the repository or contact me directly.