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

    Can connect in REPL but not with main.py

    Scheduled Pinned Locked Moved WiPy 2&3
    17 Posts 2 Posters 24 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.
    • S Offline
      SciWax
      last edited by SciWax

      Hello together,

      I can connect to the WiFi network and send data to my server while only using the REPL in VScode and the pymakr plugin. Connecting to the WiFi network is not possible for me when I try to do it in the main.py. I just can't connect to the WiFi network.

      I'm using an older version of VScode, because with newer versions pymakr doesn't work...

      VScode: 1.51.1
      Pymakr 1.1.18

      >>> os.uname(
      (sysname='WiPy', nodename='WiPy', release='1.20.2.r6', version='v1.11-c5a0a97 on 2021-10-28', machine='WiPy with ESP32', pybytes='1.7.1')
      

      The lines I put in the REPL line by line:

      import network
      import time
      import urequests # https://github.com/micropython/micropython-lib/blob/master/urequests/
      wlan = network.WLAN(mode=network.WLAN.STA)
      wlan.connect('networkname', auth = (wlan.WPA2, 'password'), timeout=5000)
      wlan.isconnected()     
      print(wlan.ifconfig())
      dataRec = """{"Number1":11,"Number2":2021}"""
      print(dataRec)
      dataRec = eval(dataRec)
      res = urequests.post("https://website.com/test.php", json=dataRec)
      res.close()
      print(res.status_code)
      res.status_code - 200
      

      I looked at different examples on the official tutorial and here how to connect to the WiFi network, but the suggestions were not successful for me.

      import network
      import time
      import urequests # https://github.com/micropython/micropython-lib/blob/master/urequests/
      import machine
      
      # create WiFi station object
      try:
          wlan = network.WLAN(mode=network.WLAN.STA)
          print("[WiFi] network interface client object created")
      except:
          print("[WiFi] network interface client object (except). Reset board ...")
          machine.reset()  
      
      try:
          wlan.connect('networkname', auth = (wlan.WPA2, 'password'))
          print('Connecting to: ' + 'networkname')
          while not wlan.isconnected():
              time.sleep_ms(100)
              print('#',end='')
          print('WLAN connection succeeded!')
      except:
          print('Could not connect to '+ 'networkname' +', resetting.')
          machine.reset()
      

      I just gets stuck in the while loop:

      See https://docs.pycom.io/smart for details
      [WiFi] network interface client object created
      Connecting to: networkname
      #################################################################
      

      I want to use the WiPy because of the TLS1.2 which comes with it. I don't know what the issue is and if there is another micropython based microcontroller board being shipped with TLS1.2 I'm happy to switch.

      Do you have an idea, what the issue is rob? @robert-hh :)

      Kind regards

      robert-hhR 1 Reply Last reply Reply Quote 0
      • S Offline
        SciWax
        last edited by

        It seems like putting more "sleeps" in the code helped:

        # create WiFi station object
        try:
            wlan = network.WLAN(mode=network.WLAN.STA)
            time.sleep(1)
            print("[WiFi] network interface client object created")
        except:
            print("[WiFi] network interface client object (except). Reset board ...")
            machine.reset()  
        
        try:
            wlan.connect('networkname', auth = (wlan.WPA2, 'password'))
            time.sleep(1)
            print('Connecting to: ' + 'networkname')
            while not wlan.isconnected():
                time.sleep_ms(200)
                print('#',end='')
            print('WLAN connection succeeded!')
        except:
            print('Could not connect to '+ 'networkname' +', resetting.')
            machine.reset()
        

        Still, it doesn't always work. When I reset the WiPy with the Reset-Button, sometimes there is an immediate connection and sometimes not.

        robert-hhR 1 Reply Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh
          last edited by

          That is strange. Your code is similar to what I use regularly.

          def do_connect():
              import network
              import time
              wlan = network.WLAN(mode=network.WLAN.STA) # create station interface
              if not wlan.isconnected():      # check if the station is connected to an AP
                  wlan.connect(ssid=WIFI_SSID, auth=(network.WLAN.WPA2, WIFI_PASSWD))
                  for _ in range(100):
                      if wlan.isconnected():      # check if the station is connected to an AP
                          break
                      print('.', end='')
                      time.sleep_ms(200)
                  else:
                      print("Connect attempt timed out\n")
                      return
              print('\nnetwork config:', wlan.ifconfig())
          
          do_connect()
          
          S 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @SciWax
            last edited by robert-hh

            @SciWax About Another board with TLS. The mainstream MicroPython supports ESP32 boards and TLS 1.2. The Generic ESP32 firmware with or without SPIRAM support should work on WiPy boards. Only the "Pxx" pin names are not supported.

            S 1 Reply Last reply Reply Quote 1
            • S Offline
              SciWax @robert-hh
              last edited by

              @robert-hh Thank you for the advise and fast answer. I really appreciate it. What I really liked about Pycom and their boards was the SSL/TLS support, so I could use it to talk with TLS secured websites out of the box.

              So if I would get one ESP32-S3 board and use micropython, I could immediately send a post request to a https website using TLS for web security reasons? I do not need to add a ssl certificate to an ESP32-S3 board myself, right? It will just work out of the box like for the pycom boards?

              Kind regards

              robert-hhR 2 Replies Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @SciWax
                last edited by robert-hh

                @SciWax There was some discussion in the MicroPython site that for TLS you might have to add your own certificates. Should be in the documentation. Different to Pycom, MicroPython.org does not run any web services.

                S 1 Reply Last reply Reply Quote 0
                • S Offline
                  SciWax @robert-hh
                  last edited by

                  @robert-hh Yeah, I've looked in the forums and tried many examples from here. Also this one. I have to check, if "sleeps" also help here in your example code. I'm using the pycom expansion board and connect it to my notebook. I also tried two different WiPys and behaviour was the same.

                  1 Reply Last reply Reply Quote 0
                  • robert-hhR Offline
                    robert-hh @SciWax
                    last edited by

                    @SciWax About TLS: See https://docs.micropython.org/en/latest/library/ssl.html

                    1 Reply Last reply Reply Quote 1
                    • S Offline
                      SciWax @robert-hh
                      last edited by

                      @robert-hh Thank you. Good to know. I can work around the issue I have with the WiPys, because I just use them as a board to easily send data to my server. I just connect them to my other microcontroller board.

                      1 Reply Last reply Reply Quote 0
                      • robert-hhR Offline
                        robert-hh @SciWax
                        last edited by robert-hh

                        @SciWax That is still strange. What is the level of P12, the antenna selection pin? Did you change something with it? The default is MAN_ANT, which is the level you force P12 to be. See https://docs.pycom.io/firmwareapi/pycom/network/wlan/

                        S 1 Reply Last reply Reply Quote 0
                        • S Offline
                          SciWax @robert-hh
                          last edited by SciWax

                          @robert-hh No, I didn't touch the Pin. I was thinking about it too, but all the tutorials talk about how you only need to touch this Pin, if you want to use an external antenna. Should I pull P12 low just in case? I'm using the internal Antenna.

                          robert-hhR 2 Replies Last reply Reply Quote 0
                          • robert-hhR Offline
                            robert-hh @SciWax
                            last edited by

                            @SciWax You can either put it to a definitive state or use the antanna=WLAN.INT_ANT option in the WLAN constructor. You can as well call wlan.antenna() to see the actual value or call wlan.antenna(WLAN.INT_ANT) to set it to internal antenna.

                            S 1 Reply Last reply Reply Quote 0
                            • robert-hhR Offline
                              robert-hh @SciWax
                              last edited by

                              @SciWax If I have WiFi problems it usually also helps to reboot the Access Point.

                              S 1 Reply Last reply Reply Quote 1
                              • S Offline
                                SciWax @robert-hh
                                last edited by SciWax

                                @robert-hh I've checked the value of the output of

                                wlan.antenna()
                                

                                It was 2 without touching it

                                with

                                wlan.antenna(wlan.INT_ANT)
                                

                                it becomes 0

                                >>> wlan.antenna()
                                0
                                

                                If I upload the code with setting the wlan.antenna to internal nothing changes really:

                                while not wlan.isconnected()
                                

                                The code only works every second time, when I reset the board. During the first time attempt, where I just power it (Connecting the expansion board with the usb port), it gets stuck in the while loop, second time when using the reset button the WiPy gets the Wifi connection immediately. When I continue using the reset button again, it is getting stuck again, using it again, it connects immediately. Hence every second time it works. It is a really weird behaviour.

                                I don't know, if not being aware of the board starting with wlan.antenna() being 2 did something funny with the hardware. Does 2 stand for selecting the external antenna? Maybe that broke the WiPy a little, because this behaviour is so strange. It can't be my code I think.

                                When I have access to my other expansion boards, I will try another one and a fresh WiPy with setting the wlan.antenna to internal. Then Ive checked everything on a hardware level.

                                robert-hhR 1 Reply Last reply Reply Quote 0
                                • S Offline
                                  SciWax @robert-hh
                                  last edited by

                                  @robert-hh Unfortunately during my project, I have no access to the access point and can't reboot it.

                                  1 Reply Last reply Reply Quote 0
                                  • robert-hhR Offline
                                    robert-hh @SciWax
                                    last edited by

                                    @SciWax said in Can connect in REPL but not with main.py:

                                    Does 2 stand for selecting the external antenna?

                                    2 stands for MAN_ANT: taking the value from P12 to select the antenna mode.

                                    S 1 Reply Last reply Reply Quote 0
                                    • S Offline
                                      SciWax @robert-hh
                                      last edited by

                                      @robert-hh okay, makes sense regarding the documentation. thanks for the information

                                      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