Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. import json
  2. import requests
  3. import time
  4. from influxdb import InfluxDBClient
  5.  
  6. envoy_ip = 'ip'
  7. envoy_user = 'envoy'
  8. envoy_password = 'last6digitsofserial'
  9. url = 'http://' + envoy_ip + '/production.json'
  10.  
  11. def startLoop():
  12.     try:
  13.         response = requests.get(url, timeout=4)
  14.         data = json.loads(response.text)
  15.         client = InfluxDBClient('ip', 8086, 'user', 'pass', 'database_name')
  16.         InstantConsumption = data['consumption'][0]['wNow']
  17.         InstantGeneration = data['production'][1]['wNow']
  18.         GenerationToday = data['production'][1]['whToday']
  19.         GridUsage = data['consumption'][1]['wNow']
  20.         ConsumptionToday = data['consumption'][0]['whToday']
  21.         LifetimeGeneration = data['production'][1]['whLifetime']
  22.         LifetimeConsumption = data['consumption'][0]['whLifetime']
  23.         LifetimeGridConsumption = data['consumption'][1]['whLifetime']
  24.         json_body = [
  25.         {
  26.             "measurement": "solar",
  27.             "tags": {
  28.                 "host": "envoy"
  29.             },
  30.             "fields": {
  31.                 "InstantConsumption": InstantConsumption,
  32.                 "InstantGeneration": InstantGeneration,
  33.                 "GridUsage": GridUsage,
  34.                 "GenerationToday": GenerationToday,
  35.                 "ConsumptionToday": ConsumptionToday,
  36.                 "LifetimeGeneration": LifetimeGeneration,
  37.                 "LifetimeConsumption": LifetimeConsumption,
  38.                 "LifetimeGridConsumption": LifetimeGridConsumption
  39.             }
  40.         }
  41.         ]
  42.        
  43.         client.write_points(json_body)
  44.     except:
  45.         pass
  46.    
  47.  
  48.     time.sleep(5)
  49.    
  50. while True:
  51.     startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement