I've been playing around with a ESP32 and the mqtt_as library (had a lot of stability problems with the umqtttsimple). I want to turn on/off a led with a mqtt message. Then publish a message that confirms the state of the led. The code below works but is this the right way to do it?
Code: Select all
# Subscription callback
def sub_cb(topic, msg, retained):
print((topic, msg, retained))
if topic == b'led/1/state/set' and msg == b'0' :
print('Turn off')
led.value(0)
print(led.value())
loop = asyncio.get_event_loop()
loop.create_task(client.publish('led/1/state', '{}'.format(led.value()), True, qos=1))
elif topic == b'led/1/state/set' and msg == b'1' :
print('Turn on')
led.value(1)
print(led.value())
loop = asyncio.get_event_loop()
loop.create_task(client.publish('led/1/state', '{}'.format(led.value()), True, qos=1))