#!/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() #Funktion Setze Bit in Variable / Function Set Bit in byte def set_bit(value, bit): return value | (1< 7: o = set_bit(o,i-8) bus.write_byte(0x21,255-o) time.sleep(speed) o = clear_bit(o,i) bus.write_byte(0x21,255-o) else: o = set_bit(o, i) bus.write_byte(0x3f,255-o) time.sleep(speed) o = clear_bit(o,i) bus.write_byte(0x21,255-o) 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": 100, "cantina" : 8, "cantina-strobo": 99} def msgs(inp): c = inp.decode("utf-8") l = len(c) # supporting number commands if l < 3: try: msg = int(inp) switch(msg) except ValueError: print ( "No valid value") return #supporting string commmands else: cmds = c.split(",") print ( "Command:", cmds[0], "Number of Parameters:", len(cmds) ) #suporting string commands with an arg separated by ',' if len(cmds) == 2: #error checking try: command = commands[cmds[0]] except KeyError: return try: arg = int (cmds[1]) except ValueError: return #strobo if ( command == 100 or command == 99 ): if arg < 100: for i in range(arg): switch((100-command)*8, speed=0.05) time.sleep(0.06) #command with parameter used for dimming else: switch(command,speed = 3*arg/100+0.7 ) #single string command without parameter else: try: switch(commands[c]) except KeyError: return init_mqtt() while 1: time.sleep(10)