CO2InfluxDaemon/CO2InfluxDaemon.py
2019-05-24 22:43:59 +02:00

67 lines
2 KiB
Python

#!/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")