Re-subscribe to MQTT topic after reconnect
This commit is contained in:
parent
65d4a70ce7
commit
6f4f3eb39c
1 changed files with 12 additions and 8 deletions
18
server.py
18
server.py
|
@ -56,18 +56,22 @@ def make_app():
|
|||
(r'/statejson', StatejsonHandler),
|
||||
])
|
||||
|
||||
if __name__ == '__main__':
|
||||
mqttc = mqtt.Client()
|
||||
mqttc.connect(MQTT_HOST)
|
||||
mqttc.loop_start()
|
||||
|
||||
def on_message(client, userdata, message):
|
||||
def on_message(client, userdata, message):
|
||||
global current_state, current_state_change
|
||||
if message.topic == 'bitlair/state':
|
||||
current_state = message.payload == b'open'
|
||||
current_state_change = time.time()
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe('bitlair/state', qos=2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
mqttc = mqtt.Client()
|
||||
mqttc.on_message = on_message
|
||||
mqttc.subscribe('bitlair/state', qos=2)
|
||||
mqttc.on_connect = on_connect
|
||||
|
||||
mqttc.connect(MQTT_HOST)
|
||||
mqttc.loop_start()
|
||||
|
||||
app = make_app()
|
||||
app.listen(8888)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue