Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    while True and serial/telnet connectivity

    Scheduled Pinned Locked Moved MicroPython
    3 Posts 2 Posters 352 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      cmisztur
      last edited by

      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-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @cmisztur
        last edited by

        @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.

        1 Reply Last reply Reply Quote 0
        • C Offline
          cmisztur
          last edited by cmisztur

          @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))
          
          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom