probably final
This commit is contained in:
		
							
								
								
									
										127
									
								
								power_mqtt1.py
									
									
									
									
									
								
							
							
						
						
									
										127
									
								
								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<<bit)
 | 
			
		||||
@@ -78,6 +48,28 @@ def set_bit(value, bit):
 | 
			
		||||
def clear_bit(value, bit):
 | 
			
		||||
    return value & ~(1<<bit)
 | 
			
		||||
 | 
			
		||||
def i2c_status_thread():
 | 
			
		||||
    global state
 | 
			
		||||
    #ports = (0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F)
 | 
			
		||||
    #         bael loun cant flur
 | 
			
		||||
    #         leba e    ina
 | 
			
		||||
    #         d
 | 
			
		||||
    names = { "flur", "baellebad", "lounge-front", "lounge-back", "baellebad-ein", "lounge-ein", "cantina-ein", "zentral-aus", "cantina"}
 | 
			
		||||
    ports = (0xF7,0xFF,0xFF,0xFE,0xFD,0xFF,0xFF,0xFF,0xFB)
 | 
			
		||||
    inputs = (0x23,0x21)   
 | 
			
		||||
    while True:
 | 
			
		||||
        byte = bus.read_byte(inputs[0])
 | 
			
		||||
        update = map( lambda x: 1 if (byte | x == x) else 0 , ports)
 | 
			
		||||
        byte = bus.read_byte(inputs[1])
 | 
			
		||||
        update[:8] = map( lambda x: 1 if (byte | x == x) else 0 , ports[:8])
 | 
			
		||||
        for i,j,n in state,update,names:
 | 
			
		||||
            if i != j:
 | 
			
		||||
                if n in power:
 | 
			
		||||
                    mqtc.publish("foobar/oben/" + n + "/strom/status", states[j], qos=0, retain=False)
 | 
			
		||||
                else:
 | 
			
		||||
                    mqtc.publish("foobar/oben/" + n + "/licht/status", states[j], qos=0, retain=False)
 | 
			
		||||
 | 
			
		||||
        time.sleep(5)
 | 
			
		||||
 | 
			
		||||
#Buttonbefehle
 | 
			
		||||
def switch(i,speed=0.5):
 | 
			
		||||
@@ -96,40 +88,50 @@ def switch(i,speed=0.5):
 | 
			
		||||
            o = clear_bit(o,i)
 | 
			
		||||
            bus.write_byte(0x3f,255-o)
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
state = [0,0,0,0,0,0,0,0,0,0]
 | 
			
		||||
states = {0: "OFF", 1: "ON"}
 | 
			
		||||
commands = { "flur" : 0, "baellebad" : 1, "lounge-front": 2, "lounge-back" : 3,"baellebad-ein" : 4, "lounge-ein" : 5, "cantina-ein" : 6, "zentral-aus" : 7, "cantina" : 8}
 | 
			
		||||
power = { "zentral-aus" : 7 , "baellebad" : 4, "lounge" : 5, "cantina" : 6 }
 | 
			
		||||
power = { "zentral" : 7 , "baellebad" : 4, "lounge" : 5, "cantina" : 6 }
 | 
			
		||||
light = { "flur" : 0, "baellebad" : 1, "lounge-front" : 2, "lounge-back" : 3, "cantina" : 8 }
 | 
			
		||||
state  = []
 | 
			
		||||
 | 
			
		||||
def decode_topic(topic):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# foobar/oben/lounge   /licht/action
 | 
			
		||||
#             cantina  /strom/status
 | 
			
		||||
#             flur
 | 
			
		||||
#             baellebad
 | 
			
		||||
#             zentral 
 | 
			
		||||
 | 
			
		||||
def switch_state(i, state):
 | 
			
		||||
    if state[i] != state: # changed
 | 
			
		||||
        switch(i)
 | 
			
		||||
        state[i] = state
 | 
			
		||||
        return True
 | 
			
		||||
    return False
 | 
			
		||||
 | 
			
		||||
def decode_topic(topic, state):
 | 
			
		||||
    #lazy
 | 
			
		||||
    parts = topic.explode("/")
 | 
			
		||||
    if parts[0] == "foobar" and parts[4] == "action":
 | 
			
		||||
        if parts[2] == "licht":
 | 
			
		||||
            return (parts[3],lights)
 | 
			
		||||
        elif parts[2] == "power":
 | 
			
		||||
            return (parts[3],power)
 | 
			
		||||
    return ("err", [] )
 | 
			
		||||
    print("dummy")
 | 
			
		||||
    list = topic.split('/')
 | 
			
		||||
    if list[3] == "licht":
 | 
			
		||||
        if list[2] in light:
 | 
			
		||||
            if switch_state(light[list[2]], state):
 | 
			
		||||
                mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
 | 
			
		||||
    elif list[3] == "strom":
 | 
			
		||||
        if list[2] in power:
 | 
			
		||||
            if switch_state(power[list[2]], state):
 | 
			
		||||
                mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
 | 
			
		||||
 | 
			
		||||
def msgs(inp, topic):
 | 
			
		||||
    c = inp.decode("utf-8")
 | 
			
		||||
    l = len(c)
 | 
			
		||||
    # supporting number commands
 | 
			
		||||
    if c == "OFF" or c == "ON":
 | 
			
		||||
        (name,lookup) = decode_topic(topic)
 | 
			
		||||
        if name == "err":
 | 
			
		||||
        if c.find("ON") >= 0:
 | 
			
		||||
            decode_topic(topic, 1)
 | 
			
		||||
        elif c.find("OFF") >= 0:
 | 
			
		||||
            decode_topic(topic, 0)
 | 
			
		||||
        return
 | 
			
		||||
        switch(lookup[name])
 | 
			
		||||
        if i in state:
 | 
			
		||||
            if state[i] == 0:
 | 
			
		||||
                state[i] = 1
 | 
			
		||||
            else:
 | 
			
		||||
                state[i] = 0
 | 
			
		||||
        else:
 | 
			
		||||
            state[i] = 1
 | 
			
		||||
        #later: send status based on state[i]
 | 
			
		||||
        
 | 
			
		||||
    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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user