kickoff
This commit is contained in:
commit
6b895bcf06
37
smartbridge.py
Executable file
37
smartbridge.py
Executable file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import requests
|
||||
import os
|
||||
import time
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
|
||||
SOURCE = "http://192.168.242.199/meter/now"
|
||||
MQTT_SERVER = "192.168.0.42"
|
||||
MQTT_TOPIC = "home/power"
|
||||
|
||||
def obtain_data() :
|
||||
req = requests.get(SOURCE)
|
||||
req.raise_for_status()
|
||||
return req.json()
|
||||
|
||||
if __name__ == "__main__" :
|
||||
data = obtain_data()
|
||||
if data['status'] != 'ok' :
|
||||
print("status: %s" % data['status'])
|
||||
os.Exit(1)
|
||||
|
||||
# Get Power readings
|
||||
power = data['elec']['power']['now']['value'] # Current power readings in Watt
|
||||
print("%d W" % (power))
|
||||
|
||||
# MQTT publish
|
||||
client = mqtt.Client()
|
||||
client.connect(MQTT_SERVER, 1883, 60)
|
||||
tsp = int(time.time())
|
||||
data = {"power": power, "timestamp": tsp}
|
||||
msg = json.dumps(data)
|
||||
print(msg)
|
||||
result = client.publish(MQTT_TOPIC, msg)
|
||||
|
Loading…
Reference in a new issue