C
@robert-hh Thank You.
mqtt_client_lock = _thread.allocate_lock()
def mqtt_check_msg_thread(client,lock):
while True:
try:
#utime.sleep(1)
with lock:
client.check_msg()
except OSError:
print('MQTT failed to check_msg')
with lock:
if client.connect() != 0:
utime.sleep(1)
except:
print('MQTT check_msg thread failure')
def mqtt_setup():
global mqtt_client
global l99_serial
is_mqtt_connected = False
with mqtt_client_lock:
mqtt_client = MQTTClient(l99_serial, '192.168.2.107', 1883)
mqtt_client.set_last_will('L99/{}/io/state/offline'.format(l99_serial), '', qos=1)
mqtt_client.set_callback(mqtt_output_command_callback)
is_mqtt_connected = mqtt_client.connect() == 0
if is_mqtt_connected:
#Timer.Alarm(handler=mqtt_check_msg, ms=1000, arg=None, periodic=True)
on_mqtt_connected(True)
else:
on_mqtt_connected(False)
if is_mqtt_connected:
_thread.start_new_thread(mqtt_check_msg_thread, (mqtt_client, mqtt_client_lock))