Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import datetime
  4. import time
  5. from influxdb import InfluxDBClient
  6. import subprocess
  7. import json
  8.  
  9. def execute(cmd):
  10.     popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
  11.     for stdout_line in iter(popen.stdout.readline, ""):
  12.         yield stdout_line
  13.     popen.stdout.close()
  14.     return_code = popen.wait()
  15.     if return_code:
  16.         raise subprocess.CalledProcessError(return_code, cmd)
  17.  
  18.  
  19. dbclient = InfluxDBClient('127.0.0.1', 8086, 'root', 'root', 'sensordata')
  20.  
  21. last = {}
  22.  
  23. fields = ["temperature_C", "temperature_C1", "temperature_C2", "humidity", "battery"]
  24.  
  25.  
  26. for event in execute(["/home/pi/rtl_433/build/src/rtl_433", "-U", "-F", "json", "-q"]):
  27.     if last == event:
  28.         continue
  29.     last = event
  30.     js = json.loads(event)
  31.     field_dict = {}
  32.     for field in fields:
  33.         if field in js:
  34.             field_dict[field] = js[field]
  35.             del js[field]
  36.     time = js['time']
  37.     del js['time']
  38.     measurement = js['model']
  39.     del js['model']
  40.     json_body = [
  41.         {  
  42.             "measurement": measurement,
  43.             "tags": js,
  44.             "fields": field_dict
  45.         }
  46.     ]
  47.     print(json_body)
  48.     dbclient.write_points(json_body)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement