commit 3daf57e440641f08d87e88650360e0499c9a8c98 Author: Merlin Raschtuttis Date: Sun Feb 4 15:28:47 2018 +0000 "Initial" Commit to prevent further loss of data diff --git a/power_mqtt1.py b/power_mqtt1.py new file mode 100755 index 0000000..1bb3f04 --- /dev/null +++ b/power_mqtt1.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python3 + +import smbus +import time +import paho.mqtt.client as mqt + +bus = smbus.SMBus(1) +mqtc = mqt.Client() + + + +# Settings for connection +host = "mqtt.chaospott.de" +topic= "foobar/oben/licht" +port = 1883 + +# Callbacks +rc = 0 + +def on_connect(mosq, obj, foo ,bar ): + print( "Connected" ) + +def on_message(mosq, obj, msg): + print( "Received on topic: " + msg.topic + " Message: "+str(msg.payload) ); + msgs(msg.payload) + +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() + + + +o1 = 0 +o2 = 0 + +#Funktion Setze Bit in Variable / Function Set Bit in byte +def set_bit(value, bit): + #print(value | (1< 7: + o2 = set_bit(o2, i-8) + bus.write_byte(0x21,255-o2) + else: + o1 = set_bit(o1, i) + bus.write_byte(0x3f,255-o1) + +def off(i): + global o1 + global o2 +# Change I2C Output-Card + if i > 7: + o2 = clear_bit(o2, i-8) + bus.write_byte(0x21,255-o2) + else: + o1 = clear_bit(o1, i) + bus.write_byte(0x3f,255-o1) + +def switch(i,speed=0.5): + on(i) + time.sleep(speed) + off(i) + +commands = { "flur" : 0, "baellebad" : 1, "lounge-front": 2, "lounge-back" : 3,"baellebad-ein" : 4, "lounge-ein" : 5, "cantina-ein" : 6, "zentral-aus" : 7, "flur-strobo": 9, "cantina" : 8} + +def msgs(inp): + c = inp.decode("utf-8") + l = len(c) + + if l < 3: + try: + msg = int(inp) + except ValueError: + print ( "No valid value") + return + if (msg == 9): + for i in range(1000): + switch(0, speed=0.05) + time.sleep(0.06) + elif (msg == 99): + for i in range(50): + switch(8, speed=0.05) + time.sleep(0.06) + else: + switch(msg) + + else: + cmds = c.split(",") + print ( cmds[0], len(cmds) ) + if len(cmds) == 2: + try: + command = commands[cmds[0]] + except KeyError: + return + try: + arg = int (cmds[1]) + except ValueError: + return + switch(command,speed = 3*arg/100+0.7 ) + if ( command == 9 ): + for i in range(arg): + switch(0, speed=0.05) + time.sleep(0.06) + + else: + try: + switch(commands[c]) + except KeyError: + return + + +init_mqtt() +while 1: + time.sleep(10)