Robustere Variante vom CO2 Daemon

This commit is contained in:
Nis Wechselberg 2019-05-24 22:43:59 +02:00
commit 3656e52a32
10 changed files with 466 additions and 0 deletions

124
.gitignore vendored Normal file
View file

@ -0,0 +1,124 @@
###SublimeText###
# cache files for sublime text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache
# workspace files are user-specific
*.sublime-workspace
# project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using SublimeText
# *.sublime-project
# sftp configuration file
sftp-config.json
###Python###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
.static_storage/
.media/
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/

67
CO2InfluxDaemon.py Normal file
View file

@ -0,0 +1,67 @@
#!/usr/bin/env python3
# Python CO2 Logger
# Copyright (c) 2019 Nis Wechselberg <enbewe@enbewe.de>
#
# This work is licensed under MIT license. You should have
# received a copy of the MIT license legalcode along with this
# work. If not, see <https://opensource.org/licenses/MIT>.
# pylint: disable=C0103
"""
Python CO2 Logger
Small daemon script that continuously monitors the CO2 concentration
using a cheap sensor from Amazon. The data is fed directly to an InfluxDB.
"""
import argparse
import time
import os
# InfluxDB Client to allow easy submission to time series database
from influxdb import InfluxDBClient
# Parser library for the Dostman AirCO2ntrol sensor
from CO2Meter import CO2Meter
# Configure argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("dev", help="Device node of the sensor (i.e. /dev/co2mini0)")
parser.add_argument("dbHost", help="Hostname/IP of the InfluxDB machine")
parser.add_argument("dbName", help="Name of the database to write to")
parser.add_argument("-i", "--interval", help="Time between two measurements (sec)",
type=int, default=180)
parser.add_argument("-n", "--node", help="Node tag to mark the measurement", default="")
args = parser.parse_args()
# Make sure the device exists and is readable
if os.path.exists(args.dev):
Meter = CO2Meter(args.dev)
Meter.get_data()
time.sleep(10)
while True:
measurement = Meter.get_data()
# Prepare data for database
influxData = [
{
"measurement": "feinstaub",
"tags": {
"node": args.node
},
"fields": {
"co2": measurement['co2'],
"temperature": measurement['temperature']
}
}
]
# Send data to InfluxDB
influx = InfluxDBClient(host=args.dbHost, database=args.dbName)
influx.write_points(influxData)
time.sleep(args.interval)
else:
print("Device not found")

View file

@ -0,0 +1,8 @@
{
"folders":
[
{
"path": "."
}
]
}

21
CO2Meter/LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Michael Heinemann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

64
CO2Meter/README.md Normal file
View file

@ -0,0 +1,64 @@
# CO2Meter
Python Module to use co2meters like the 'AirCO2ntrol Mini' from TFA Dostmann with USB ID 04d9:a052. There are also other modules using the same interface.
This module supports Python 2.7 and 3.x.
## Attribution
Reverse Engineering of the protocol and initial code done by [Henryk Plötz](https://github.com/henryk).
Read all about it at [hackaday](https://hackaday.io/project/5301-reverse-engineering-a-low-cost-usb-co-monitor)
Code derived from [this article](https://hackaday.io/project/5301-reverse-engineering-a-low-cost-usb-co-monitor/log/17909-all-your-base-are-belong-to-us)
## Install
With pip:
```bash
pip install git+https://github.com/heinemml/CO2Meter
```
Without pip:
```bash
python setup.py install
```
Remark: you don't need to install, you can also just copy the CO2Meter.py into your project.
If you don't want to run your script as root make sure you have sufficient rights to access the device file.
This udev rule can be used to set permissions.
```
ACTION=="remove", GOTO="co2mini_end"
SUBSYSTEMS=="usb", KERNEL=="hidraw*", ATTRS{idVendor}=="04d9", ATTRS{idProduct}=="a052", GROUP="plugdev", MODE="0660", SYMLINK+="co2mini%n", GOTO="co2mini_end"
LABEL="co2mini_end"
```
save it as `/etc/udev/rules.d/90-co2mini.rules` and add the script user to the group `plugdev`.
This rules make the device also available as co2mini0 (increase trailing number for each additional device).
## Usage
```python
from CO2Meter import *
import time
sensor = CO2Meter("/dev/hidraw0")
while True:
time.sleep(2)
sensor.get_data()
```
The device writes out one value at a time. So we need to parse some data until we have co2 and temperature. Thus the get_data() method will initially return none or only on value (whichever comes first).
When you just need one measurement you should wait some seconds or iterate until you get a full reading. If you just need co2 a call to `get_co2` might speed things up.
### Callback
You can pass a callback to the constructor. It will be called when any of the values is updated. The parameters passed are `sensor` and `value`. `sensor` contains one of these constants:
```python
CO2METER_CO2 = 0x50
CO2METER_TEMP = 0x42
CO2METER_HUM = 0x44
```
### Error handling
In Case the device can't be read anymore (e.g. it was unplugged) the worker thread will end in the background. Afterwards calls to any of the `get_*` functions will throw an `IOError`. You will need to handle any resetup, making sure that the device is there etc yourself.

143
CO2Meter/__init__.py Normal file
View file

@ -0,0 +1,143 @@
import sys
import fcntl
import threading
import weakref
CO2METER_CO2 = 0x50
CO2METER_TEMP = 0x42
CO2METER_HUM = 0x44
HIDIOCSFEATURE_9 = 0xC0094806
def _co2_worker(weak_self):
while True:
self = weak_self()
if self is None:
break
self._read_data()
if not self._running:
break
del self
class CO2Meter:
_key = [0xc4, 0xc6, 0xc0, 0x92, 0x40, 0x23, 0xdc, 0x96]
_device = ""
_values = {}
_file = ""
_running = True
_callback = None
def __init__(self, device="/dev/hidraw0", callback=None):
self._device = device
self._callback = callback
self._file = open(device, "a+b", 0)
if sys.version_info >= (3,):
set_report = [0] + self._key
fcntl.ioctl(self._file, HIDIOCSFEATURE_9, bytearray(set_report))
else:
set_report_str = "\x00" + "".join(chr(e) for e in self._key)
fcntl.ioctl(self._file, HIDIOCSFEATURE_9, set_report_str)
thread = threading.Thread(target=_co2_worker, args=(weakref.ref(self),))
thread.daemon = True
thread.start()
def _read_data(self):
try:
result = self._file.read(8)
if sys.version_info >= (3,):
data = list(result)
else:
data = list(ord(e) for e in result)
decrypted = self._decrypt(data)
if decrypted[4] != 0x0d or (sum(decrypted[:3]) & 0xff) != decrypted[3]:
print(self._hd(data), " => ", self._hd(decrypted), "Checksum error")
else:
operation = decrypted[0]
val = decrypted[1] << 8 | decrypted[2]
self._values[operation] = val
if self._callback is not None:
if operation == CO2METER_CO2:
self._callback(sensor=operation, value=val)
elif operation == CO2METER_TEMP:
self._callback(sensor=operation,
value=round(val / 16.0 - 273.1, 1))
elif operation == CO2METER_HUM:
self._callback(sensor=operation, value=round(val / 100.0, 1))
except:
self._running = False
def _decrypt(self, data):
cstate = [0x48, 0x74, 0x65, 0x6D, 0x70, 0x39, 0x39, 0x65]
shuffle = [2, 4, 0, 7, 1, 6, 5, 3]
phase1 = [0] * 8
for i, j in enumerate(shuffle):
phase1[j] = data[i]
phase2 = [0] * 8
for i in range(8):
phase2[i] = phase1[i] ^ self._key[i]
phase3 = [0] * 8
for i in range(8):
phase3[i] = ((phase2[i] >> 3) | (phase2[(i-1+8)%8] << 5)) & 0xff
ctmp = [0] * 8
for i in range(8):
ctmp[i] = ((cstate[i] >> 4) | (cstate[i]<<4)) & 0xff
out = [0] * 8
for i in range(8):
out[i] = (0x100 + phase3[i] - ctmp[i]) & 0xff
return out
@staticmethod
def _hd(data):
return " ".join("%02X" % e for e in data)
def get_co2(self):
if not self._running:
raise IOError("worker thread couldn't read data")
result = {}
if CO2METER_CO2 in self._values:
result = {'co2': self._values[CO2METER_CO2]}
return result
def get_temperature(self):
if not self._running:
raise IOError("worker thread couldn't read data")
result = {}
if CO2METER_TEMP in self._values:
result = {'temperature': (self._values[CO2METER_TEMP]/16.0-273.15)}
return result
def get_humidity(self): # not implemented by all devices
if not self._running:
raise IOError("worker thread couldn't read data")
result = {}
if CO2METER_HUM in self._values:
result = {'humidity': (self._values[CO2METER_HUM]/100.0)}
return result
def get_data(self):
result = {}
result.update(self.get_co2())
result.update(self.get_temperature())
result.update(self.get_humidity())
return result

11
CO2Meter/example.py Normal file
View file

@ -0,0 +1,11 @@
#!/bin/env python
from CO2Meter import *
from datetime import datetime
import time
Meter = CO2Meter("/dev/hidraw0")
while True:
measurement = Meter.get_data()
measurement.update({'timestamp': datetime.now()})
print(measurement)
time.sleep(5)

7
CO2Meter/setup.py Normal file
View file

@ -0,0 +1,7 @@
from distutils.core import setup
setup(name='CO2Meter',
version='2.1',
py_modules=['CO2Meter'],
url='https://github.com/heinemml/CO2Meter',
description='Library to access USB CO2Meters'
)

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Nis Wechselberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0
README.md Normal file
View file