Skip to content
Permalink
Browse files

Require sub topics to be a list. Move OTA logic out of mqtt_client fn

  • Loading branch information...
DavidVentura committed Jul 11, 2019
1 parent d6df15c commit f576c69b6d0d8fea1087e58e9a6bbe358965bebe
Showing with 13 additions and 18 deletions.
  1. +8 −13 firmware/common.py
  2. +2 −2 firmware/curtains_433.py
  3. +1 −1 firmware/nightlamp.py
  4. +1 −1 firmware/temp.py
  5. +1 −1 firmware/wall_light.py
@@ -8,7 +8,6 @@
WIFI_CONNECTION_TIMEOUT = 10 # seconds
WIFI_SSID = 'cuevita'
WIFI_PASSWORD = 'salander'
PUBLISH_INTERVAL = 60 # seconds
OTA_TOPIC = None
CLIENT_ID = "ID_NOT_SET"
LOGSERVER = '192.168.2.189'
@@ -63,20 +62,15 @@ def connect_mqtt(m):
log(e)
time.sleep_ms(200)

def mqtt_client(MQTT_HOST, callback=None, subtopic=None):
def mqtt_client(MQTT_HOST, callback=None, subtopic):
assert type(subtopic) is list
mqtt = MQTTClient(CLIENT_ID, MQTT_HOST)
if callback:
mqtt.set_callback(callback)
connect_mqtt(mqtt)
mqtt.subscribe(OTA_TOPIC)
if subtopic is not None:
if type(subtopic) is list:
for topic in subtopic:
log('Subscribing to %s' % topic)
mqtt.subscribe(topic)
else:
log('Subscribing to %s' % subtopic)
mqtt.subscribe(subtopic)
for topic in subtopic:
log('Subscribing to %s' % topic)
mqtt.subscribe(topic)
return mqtt

def OTA_wrapper(callback):
@@ -85,6 +79,7 @@ def OTA(topic, msg):
callback(topic, msg)
return

log("Receiving OTA update..")
if len(msg) < 45: # 40 is the length of the sha1
log("Something wrong with the message")
log(msg)
@@ -158,9 +153,9 @@ def loop(_id, setup_fn, loop_fn, callback, subtopic):
global CLIENT_ID
CLIENT_ID = _id
try:
OTA_TOPIC = ("%s/OTA" % CLIENT_ID).encode('ascii')
STA = setup_wifi()
mqtt = mqtt_client(MQTT_HOST, callback=OTA_wrapper(callback), subtopic=subtopic)
OTA_TOPIC = ("%s/OTA" % CLIENT_ID).encode('ascii')
mqtt = mqtt_client(MQTT_HOST, callback=OTA_wrapper(callback), subtopic=subtopic+[OTA_TOPIC])
if setup_fn is not None:
setup_fn()
while True:
@@ -5,7 +5,7 @@

CLIENT_ID = 'ALL_CURTAINS'
TOPIC_PREFIX = "%s/set" % CLIENT_ID
SUBTOPIC = [b"ALL_CURTAINS/set/#" % TOPIC_PREFIX, b"RFPOWER/set/#"]
SUBTOPICS = [b"ALL_CURTAINS/set/#" % TOPIC_PREFIX, b"RFPOWER/set/#"]

relay_up = Pin(16, Pin.OUT) # D0
relay_down = Pin(4, Pin.OUT) # D2
@@ -71,6 +71,6 @@ def setup():

def main():
setup()
common.loop(CLIENT_ID, setup_fn=None, loop_fn=[], callback=sub_cb, subtopic=SUBTOPIC)
common.loop(CLIENT_ID, setup_fn=None, loop_fn=[], callback=sub_cb, subtopic=SUBTOPICS)

main()
@@ -48,6 +48,6 @@ def setup():
button.irq(handler=handle_button, trigger=Pin.IRQ_RISING),
led(1) # Turn off LED, it is inverted
def main():
common.loop(CLIENT_ID, setup_fn=setup, loop_fn=[read_dht], callback=sub_cb, subtopic=SUBTOPIC)
common.loop(CLIENT_ID, setup_fn=setup, loop_fn=[read_dht], callback=sub_cb, subtopic=[SUBTOPIC])

main()
@@ -19,6 +19,6 @@ def setup():
led(1) # Turn off LED, it is inverted

def main():
common.loop(CLIENT_ID, setup_fn=setup, loop_fn=[read_dht], callback=sub_cb, subtopic=SUBTOPIC)
common.loop(CLIENT_ID, setup_fn=setup, loop_fn=[read_dht], callback=sub_cb, subtopic=[SUBTOPIC])

main()
@@ -64,7 +64,7 @@ def setup():
sw.irq(trigger=Pin.IRQ_FALLING, handler=btn_callback)

def main():
common.loop(CLIENT_ID, setup_fn=setup, loop_fn=[], callback=sub_cb, subtopic=SUBTOPIC)
common.loop(CLIENT_ID, setup_fn=setup, loop_fn=[], callback=sub_cb, subtopic=[SUBTOPIC])

e = Encoder(4, 0, min_val=1, max_val=MAX_STEPS_ENCODER, callback=rotary_cb) # d2, d3 => order matters for rotation direction
main()

0 comments on commit f576c69

Please sign in to comment.
You can’t perform that action at this time.