69 lines
2 KiB
Python
69 lines
2 KiB
Python
#!/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": {
|
|
"radiation_cps": splitdata[1],
|
|
"radiation_cpm": splitdata[3],
|
|
"radiation_dose": splitdata[5]
|
|
}
|
|
}
|
|
]
|
|
|
|
# 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")
|