[SOLVED] Arduino Portenta H7 hangs when connecting

New user here, I have used Arduinos and Python before, but am just starting in OpenMV with my new Portenta H7 and vision shield. I have taken some code from the examples and put them together into something that scans a QR code with WiFi details formatted like this: ssid-pass::IP to ping (I have not done anything with the IP yet but have a few ideas). I want it to connect to that WiFi network and then get data from the host “google.com” like in the examples. When I run the code, it scans the QR code correctly and gets the WiFi details right, but hangs when actually connecting. I get an “OSError: [Errno 110] ETIMEDOUT”… I am sure the WiFi details are correct and don’t know what the problem might be. Can anyone help me?
The code works the first time I try it then fails the rest of the time after changing nothing

Hardware/software details:
Windows 10
OpenMV IDE 4.1.4-[latest]
Arduino Portenta H7 with ethernet vision shield
USBC-USBC connection


#WiFi getter and connector

import sensor, image, time, pyb, network, socket

redLED = pyb.LED(1) # built-in red LED
greenLED = pyb.LED(2) # built-in green LED
blueLED = pyb.LED(3) # built-in blue LED

sensor.set_windowing((240, 240)) # look at center 240x240 pixels of the VGA resolution.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
clock = time.clock()

img = sensor.snapshot()
ssid = ""
password = ""
ip = ""
is_handled = False
while is_handled == False:
    img = sensor.snapshot()
    for code in img.find_qrcodes():
        if (code != None):
            img.draw_rectangle(code.rect(), color = 127)
            #print(code[4])    # prints URL/payload
            print("Payload: " + code.payload())       # also prints URL/payload
            # Payload is formatted as ssid-pass::IP
            payload = str(code.payload())
            ssid_pos = payload.find("-")
            ssid = payload[0:ssid_pos]
            pass_pos = ssid_pos + 1
            #password = payload[pass_pos:]
            ip_pos = payload.find("::") + 2
            ip = payload[ip_pos:]
            password = payload[pass_pos:ip_pos - 2]
            is_handled = True
    # commented this out so I could see the debug output



print("SSID:--" + ssid + "--")
print("Password:--" + password + "--")
print("IP to ping:--" + ip + "--")


PORT = 80
HOST = "www.google.com"
#HOST = ip

# Init wlan module and connect to network
print("Trying to connect. Note this may take a while...")

wlan = network.WLAN(network.STA_IF)
wlan.connect(ssid, password, timeout=60000)

# We should have a valid IP now via DHCP
print("WiFi Connected ", wlan.ifconfig())

# Get addr info via DNS
addr = socket.getaddrinfo(HOST, PORT)[0][4]

# Create a new socket and connect to addr
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Set timeout

# Send HTTP request and recv response
client.send("GET / HTTP/1.1\r\nHost: %s\r\n\r\n"%(HOST))

# Close socket

The code works the first time I try it then fails the rest of the time after changing nothing

Can you explain this more ? How do you run this code from the IDE ? Are you saying the second time you run it, it fails ?

Can you show me how to generate that QR code for my network for testing ?

Thank you for the quick reply.
After being away from my computer for a couple hours doing other things, I come back and try the code. It works, then I try again and it does not work.

QR code generator: https://www.qr-code-generator.com/
I open it on my phone and scan it from there

Arduino Portenta H7 is connected via USB C port. Running code by clicking the connect button and clicking the green triangle

QRCodeFinder.py - OpenMV IDE 12_26_2021 9_26_29 PM (2)

Hi, I tested it with (and without) the QR code, and I noticed it sometimes fails to connect if the antenna is pointing away from the the access point. So I think that you just need to make sure the board is closer to the access point, you could also retry a few times:

for i in range(0, 5):
        wlan.connect(ssid, password, timeout=10000)
    except OSError:

Thank you SO much iabdalkader. I added the retry code and it works perfectly every time!