while True and serial/telnet connectivity



  • Should I expect to be able to telnet or connect over serial if I introduce this into my main.py script?

    while True:
            machine.idle()
            try:
                mqtt_client.check_msg()
            except OSError:
                if mqtt_client.connect() != 0: utime.sleep(1)```


  • @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))
    


  • @cmisztur No. You get the REPL prompt only when no other script besides the REPL is running. Interrupt service routine however are served, and you can also start a thread for doing something, e.g. the code above, while you main code terminates and gets back into REPL.


Log in to reply
 

Pycom on Twitter