Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import datetime
- import time
- from influxdb import InfluxDBClient
- import subprocess
- import json
- def execute(cmd):
- popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
- for stdout_line in iter(popen.stdout.readline, ""):
- yield stdout_line
- popen.stdout.close()
- return_code = popen.wait()
- if return_code:
- raise subprocess.CalledProcessError(return_code, cmd)
- dbclient = InfluxDBClient('127.0.0.1', 8086, 'root', 'root', 'sensordata')
- last = {}
- fields = ["temperature_C", "temperature_C1", "temperature_C2", "humidity", "battery"]
- for event in execute(["/home/pi/rtl_433/build/src/rtl_433", "-U", "-F", "json", "-q"]):
- if last == event:
- continue
- last = event
- js = json.loads(event)
- field_dict = {}
- for field in fields:
- if field in js:
- field_dict[field] = js[field]
- del js[field]
- time = js['time']
- del js['time']
- measurement = js['model']
- del js['model']
- json_body = [
- {
- "measurement": measurement,
- "tags": js,
- "fields": field_dict
- }
- ]
- print(json_body)
- dbclient.write_points(json_body)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement