RadiationInfluxDaemon/RadiationInfluxDaemon.py

70 lines
2 KiB
Python
Raw Normal View History

2019-09-01 19:44:33 +02:00
#!/usr/bin/env python3
# Python Radiation 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 Radiation Logger
Small daemon script that parses the data from the MightyOhm Geiger-Mueller Counter.
The data is fed directly to an InfluxDB.
"""
import argparse
import time
import os
import serial
# InfluxDB Client to allow easy submission to time series database
from influxdb import InfluxDBClient
# Configure argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("dev", help="Device node of the sensor (i.e. /dev/ttyS0)")
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):
ser = serial.Serial(args.dev, 9600)
while True:
ser.flushInput()
ser.readline()
data = ser.readline()
splitdata = data.decode("UTF-8").split(",")
# Prepare data for database
influxData = [
{
"measurement": "feinstaub",
"tags": {
"node": args.node
},
"fields": {
2019-09-01 20:02:31 +02:00
"geiger_cps": int(splitdata[1]),
"geiger_cpm": int(splitdata[3]),
"geiger_dose": float(splitdata[5])
2019-09-01 19:44:33 +02:00
}
}
]
# 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")