Skip to content
Permalink
Browse files

use common.publish to avoid publishing to an un-initialized mqtt. Thi…

…s could happen if a callback triggers on subscription when we have multiple subscriptions
  • Loading branch information...
DavidVentura committed Jul 6, 2019
1 parent fee57b7 commit c50b1126afa21cc21a553a56ee79797bb0b23c19
Showing with 40 additions and 25 deletions.
  1. +7 −0 firmware/common.py
  2. +2 −3 firmware/curtains_433.py
  3. +3 −3 firmware/nightlamp.py
  4. +24 −14 firmware/rf433.py
  5. +2 −2 firmware/temp.py
  6. +2 −3 firmware/wall_light.py
@@ -142,6 +142,13 @@ def log(msg):
except Exception as e:
print(e)

def publish(topic, msg, retain=True, qos=0):
if mqtt is not None:
log.info('Publish %s to %s' % (message, topic))
mqtt.publish(topic, msg, retain, qos)
else:
log('Tried to publish but mqtt is not yet setup')

def loop(_id, setup_fn, loop_fn, callback, subtopic):
global mqtt
global OTA_TOPIC
@@ -18,14 +18,13 @@ def set_channel_state(channel, state):
s.on(channel)
else:
s.off(channel)
common.mqtt.publish(PUBTOPIC+b"/%s" % channel, str(state))
common.publish(PUBTOPIC+b"/%s" % channel, str(state))


def set_pin(pin, state):
PUBTOPIC = b"%s/state/pin_%s" % (CLIENT_ID, str(pin))
pin(state)
if common.mqtt is not None:
common.mqtt.publish(PUBTOPIC, str(pin()))
common.publish(PUBTOPIC, str(pin()))

def move_curtains(direction, _time):
if direction == 'up':
@@ -17,7 +17,7 @@
def set_pin(pin, state):
common.log('Setting pin to %s' % state)
pin(state)
common.mqtt.publish(PUBTOPIC, str(pin()))
common.publish(PUBTOPIC, str(pin()))

def sub_cb(topic, msg):
common.log(topic)
@@ -36,8 +36,8 @@ def read_dht():
except Exception as e:
common.log('Failed to get DHT measurements: %s' % e)
return
common.mqtt.publish(TEMPTOPIC, "%.2f" % dht.temperature())
common.mqtt.publish(HUMTOPIC, "%.2f" % dht.humidity())
common.publish(TEMPTOPIC, "%.2f" % dht.temperature())
common.publish(HUMTOPIC, "%.2f" % dht.humidity())

@common.debounce(250)
def handle_button(pin):
@@ -5,10 +5,12 @@
from rfsocket import RFSocket

CLIENT_ID = 'RFPOWER'
SUBTOPIC = b"%s/set/#" % CLIENT_ID
PUBTOPIC = b"%s/state" % CLIENT_ID
TEMPTOPIC = b"TEMP/%s" % CLIENT_ID
HUMTOPIC = b"HUM/%s" % CLIENT_ID
HDMI_SUBTOPIC = b"HDMI/set/#"
HDMI_PUBTOPIC = b"HDMI/state"

LAMP_SUBTOPIC = b"%s/set/#" % CLIENT_ID
LAMP_PUBTOPIC = b"%s/state" % CLIENT_ID
HDMI_PIN = Pin(4, Pin.OUT)

p = Pin(0, Pin.OUT)
s = RFSocket(p, remote_id=41203711, chann=RFSocket.NEXA)
@@ -18,21 +20,29 @@ def set_channel_state(channel, state):
s.on(channel)
else:
s.off(channel)
common.mqtt.publish(PUBTOPIC+b"/%s" % channel, str(state))
common.publish(LAMP_PUBTOPIC+b"/%s" % channel, str(state))

def set_pin(pin, state, pubtopic):
common.log('Setting pin to %s, publishing to %s' % (state, pubtopic))
pin(state)
common.publish(pubtopic, str(pin()))

def sub_cb(topic, msg):
stopic = topic.decode('ascii').split('/')
common.log(topic.decode('ascii'))
if len(stopic) != 3:
common.log(topic)
return
channel = int(stopic[2])
common.log(msg.decode('ascii'))
if msg in (b'0', b'1'):
set_channel_state(channel, int(msg))
common.log('Topic: %s' % topic.decode('ascii'))
common.log('Msg: %s' % msg.decode('ascii'))
if stopic[0] == CLIENT_ID:
if len(stopic) != 3:
common.log(topic)
return
channel = int(stopic[2])
if msg in (b'0', b'1'):
set_channel_state(channel, int(msg))
if stopic[0] == 'HDMI':
set_pin(HDMI_PIN, not HDMI_PIN(), HDMI_PUBTOPIC)

def main():
#led(1) # Turn off LED, it is inverted
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=[LAMP_SUBTOPIC, HDMI_SUBTOPIC])

main()
@@ -12,8 +12,8 @@
@common.debounce(60000)
def read_dht():
dht.measure()
common.mqtt.publish(TEMPTOPIC, "%.2f" % dht.temperature())
common.mqtt.publish(HUMTOPIC, "%.2f" % dht.humidity())
common.publish(TEMPTOPIC, "%.2f" % dht.temperature())
common.publish(HUMTOPIC, "%.2f" % dht.humidity())

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

def set_light(on, value):
light_pwm.duty(on * value)
if common.mqtt:
common.mqtt.publish(PUBTOPIC_ON, str(on))
common.mqtt.publish(PUBTOPIC_VALUE, str(value))
common.publish(PUBTOPIC_ON, str(on))
common.publish(PUBTOPIC_VALUE, str(value))

def update_light():
value = light_intensity * int(1024/MAX_STEPS_ENCODER)

0 comments on commit c50b112

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