Skip to content

isheglov/co2-sensor-script

Repository files navigation

CO2 Sensor Monitoring Script

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
Loading

Explanation of Each Component

  • 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_data SQLite 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.

Features

  • 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.

Requirements

  • Raspberry Pi (tested on Raspberry Pi Zero W)
  • USB-zyTemp CO2 sensor (Holtek Semiconductor)
  • Python 3.x
  • Python packages:
    • hidapi
    • Flask
    • sqlite3
    • datetime

Setup and Installation

  1. Clone the repository:
   git clone https://github.com/isheglov/co2-sensor-script.git
   cd co2-sensor-script
   cp .env.dist .env
  1. 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
  1. Install the required packages:
pip install -r requirements.txt
  1. Create the SQLite Database:

Run createDB.py to initialize the database:

python3 createDB.py
  1. Connect the CO2 Sensor:

Ensure the USB-zyTemp CO2 sensor is connected to the Raspberry Pi.

  1. Run the script:
python3 co2_sensor.py
  1. Start the Web Server:

Run the Flask app to start the web interface:

python3 web_service/app.py

By 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.

Running Automation for Fan Control

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.py

Running as a Background Service (Optional)

To continuously run the script in the background on a Raspberry Pi using systemd, follow these steps:

  1. Create a systemd service file:
sudo nano /etc/systemd/system/co2sensor.service
  1. 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
  1. Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable co2sensor.service
sudo systemctl start co2sensor.service

Web Interface

The 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

Web Pages:

  • /: Main dashboard for real-time data visualization.
  • /current: Shows the latest sensor readings.

License

This project is licensed under the MIT License - see the LICENCE file for details.

Testing

The project includes a comprehensive test suite to verify functionality of the CO2 sensor code.

Running Tests

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.py

To run a specific test method:

pytest tests/test_co2_sensor.py::TestCO2Sensor::test_parse_data_co2

Test Coverage

The tests cover:

  • CO2, temperature, and humidity data parsing
  • Database operations
  • Device communication and error handling
  • Edge cases and invalid data handling

Writing New Tests

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

Continuous Integration

This project uses GitHub Actions for continuous integration:

  1. 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
  2. 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/.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

  1. Ensure your code follows the style guidelines in CLAUDE.md
  2. Add tests for new functionality
  3. Verify all tests pass before submitting your PR
  4. Maintain or improve test coverage

Contact

If you have any questions or issues, feel free to open an issue in the repository or contact me directly.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published