🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Code only works from Visual Studio

    Micropython
    micropython
    3
    8
    11.8k
    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.
    • D
      dclaar
      last edited by dclaar

      I have this code:

      def main():
        purple_air = PurpleAir()
        # Reset orientation when A pushed. End test if testing.
        btnA.wasPressed(purple_air.SetOrientation)
        btnB.wasPressed(purple_air.Testing)
      
        wifi_try_count = 0
        while True:  while True:
          if not (wifiCfg.wlan_sta.isconnected()):
            wifi_try_count += 1
            purple_air.ShowError(
              'No WiFi ({:d})'.format(wifi_try_count))
            wifiCfg.doConnect('FBI', 'CIA')
            purple_air.ShowError('WiFi connected')
          else:
            purple_air.ShowError('Trying')
            try:
              purple_air.GetAQI()
            except Error as e:
              purple_air.ShowError(e)
          purple_air.ShowError('Waiting')
          wait_ms(1)
          purple_air.ShowError('Done')
      

      When I run from Visual Studio, it connects to WiFi, prints WiFi Connected, Waiting, Done, Trying all in a row.

      When I disconnect it, choose it from the App screen, it will stop at WiFi Connected. If I yank out the entire else clause, then it will stop at Waiting. In either case, exactly 1 print statement after connecting to WiFi.

      It's not actually completely dead: Pressing the A or B buttons execute the callbacks as expected.

      But I can't figure out why it reacts differently connected vs. unconnected!

      Any ideas?

      1 Reply Last reply Reply Quote 0
      • D
        dclaar
        last edited by

        OK, I have no idea why, but taking out the else: works. (I took out main() first, and that didn't help, but I didn't put it back in once things worked). I haven't run it for a long time, so it might still hang, but initial indications are promising!

        while True:
          if not (wifiCfg.wlan_sta.isconnected()):
            wifi_try_count += 1
            purple_air.ShowError(
              'No WiFi ({:d})'.format(wifi_try_count))
            wifiCfg.doConnect('Mi5', 'double07')
            purple_air.ShowError('WiFi connected')
          try:
            purple_air.GetAQI()
          except Error as e:
            purple_air.ShowError(e)
          wait_ms(1)
        
        1 Reply Last reply Reply Quote 0
        • D
          dclaar
          last edited by

          Nope, spoke too soon. It just takes longer to hang. :(

          1 Reply Last reply Reply Quote 0
          • D
            dclaar
            last edited by dclaar

            I found out that you can actually get a serial console, and it says:

            Traceback (most recent call last):
              File "main.py", line 130, in <module>
              File "main.py", line 128, in <module>
              File "main.py", line 96, in GetAQI
            NameError: name 'wait_ms' isn't defined
            

            Don't ask me why it works when connected to the laptop, though!

            Serves me right for trying to limit the imports. :)

            1 Reply Last reply Reply Quote 0
            • lukasmaximusL
              lukasmaximus
              last edited by

              wait_ms is part of the uiflow class, did you import it at the start of your program?

              1 Reply Last reply Reply Quote 0
              • D
                dclaar
                last edited by dclaar

                I was trying to be "more pythonic" and not import *, i.e.

                from m5stack import lcd
                

                It didn't occur to me that it was in uiflow!
                I've gone back to importing everything. :)

                1 Reply Last reply Reply Quote 0
                • R
                  robalstona
                  last edited by

                  I noticed that some development environments use REPL when transferring files and running. And by the way, they silently import various system libraries / modules. Eg uPyCraft imports os library to display a list of files. By the way, the wait_ms () can be imported from the time library

                  1 Reply Last reply Reply Quote 0
                  • D
                    dclaar
                    last edited by

                    Given that I can now see the console, I determined that it throws OSError if the http GET fails. Which is a weird error to throw, but OK. So I put that in my except: clause, and it's humming away!

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post