From 773fd8c35ccf8f51b6af0584c6ee185494de13c8 Mon Sep 17 00:00:00 2001 From: Merlin Raschtuttis Date: Tue, 22 May 2018 15:37:25 +0000 Subject: [PATCH] probably final --- power_mqtt1.py | 129 ++++++++++++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 56 deletions(-) diff --git a/power_mqtt1.py b/power_mqtt1.py index bb87206..9527d5e 100755 --- a/power_mqtt1.py +++ b/power_mqtt1.py @@ -4,14 +4,10 @@ import smbus import time import paho.mqtt.client as mqt from datetime import datetime -from datetime import timedelta bus = smbus.SMBus(1) mqtc = mqt.Client() - - - access = {} # [ code : [last_access, penalty] ] @@ -34,16 +30,6 @@ def millis_since(start_time): ms = (dt.days * 24 * 60 * 60 + dt.seconds) * 1000 + dt.microseconds / 1000.0 return ms - - -# Settings for connection -host = "10.42.0.244" -topic= "foobar/oben/licht" -port = 1883 - -# Callbacks -rc = 0 - def on_connect(mosq, obj, foo ,bar ): print( "Connected" ) @@ -54,22 +40,6 @@ def on_message(mosq, obj, msg): def on_subscribe(mosq, obj, mid, granted_qos): print("Subscribed OK") -# Set callbacks -mqtc.on_message = on_message -mqtc.on_connect = on_connect -mqtc.on_subscribe = on_subscribe - -# Connect and subscribe - -def init_mqtt(): - mqtc.connect(host, port, 60) - mqtc.subscribe(topic, 0) - rc = 0 - while rc == 0: - rc = mqtc.loop() - - - #Funktion Setze Bit in Variable / Function Set Bit in byte def set_bit(value, bit): return value | (1<= 0: + decode_topic(topic, 1) + elif c.find("OFF") >= 0: + decode_topic(topic, 0) + return elif l < 3: try: msg = int(inp) @@ -178,8 +180,23 @@ def msgs(inp, topic): return +def init_mqtt(): + mqtc.connect("10.42.0.244", 1883, 60) + mqtc.subscribe("foobar/oben/licht", 0) + mqtc.subscribe("foobar/oben/lounge-back/licht/action", 0) + mqtc.subscribe("foobar/oben/lounge-front/licht/action", 0) + mqtc.subscribe("foobar/oben/lounge/strom/action", 0) + mqtc.subscribe("foobar/oben/baellebad/licht/action", 0) + mqtc.subscribe("foobar/oben/baellebad/strom/action", 0) + mqtc.subscribe("foobar/oben/cantina/licht/action", 0) + mqtc.subscribe("foobar/oben/cantina/strom/action", 0) + mqtc.subscribe("foobar/oben/flur/licht/action", 0) + mqtc.subscribe("foobar/oben/licht", 0) + mqtc.on_message = on_message + mqtc.on_connect = on_connect + mqtc.on_subscribe = on_subscribe + mqtc.loop_forever() + init_mqtt() -while 1: - time.sleep(10) # vim: noai:ts=4:sw=4