How to Find Port Number to connect to WiFi Network using socket.connect(). OSError: [Errno 104] ECONNRESET

  • Using a WiPy 3.0 and Expansion board, I am looking to connect to Wifi Networks using WLAN() and calculate the ping between the WiPy and the network using Socket. I can connect to Wifi just fine, but when opening a socket to connect to the network I get the error OSError: [Errno 104] ECONNRESET after socket.connect() is run. I believe it is because I am using the wrong port number, but I don't know how to find the correct port between the two. Does anyone know how to find the right port number, or know of a better way to ping the network.

    Here is my code:

    from network import WLAN
    import time
    import socket
    import ssl
    import machine
    wlan = WLAN(mode=WLAN.STA)
    #Scan networks
    networks = wlan.scan()
    for network in networks:
    	if network.ssid=='WIFI NAME':
    		print('Wifi found...')
    		wlan.connect(ssid=network.ssid, auth=(network.sec, 'WIFI PASSWORD),timeout=500)
    		while not wlan.isconnected():
    if wlan.isconnected():
    	print('WLAN Connected!')
    config = wlan.ifconfig()
    ip = config[0]
    port = 80
    addinfo = socket.getaddrinfo( ip , port)[0][-1]
    print('Preparing Socket...')
    s = socket.socket() #Prepare socket
    #s = ssl.wrap_socket(s)
    print('Opening Socket...')
    start = time.ticks_ms() #Start Time
        s.connect(addinfo) #ERROR THROWN HERE OSError: [Errno 104] ECONNRESET
    except OSError as e:
        if str(e) == '199':
            print("In Progress")
            raise e
    rec = s.recv(1024)
    elapsedTime = time.ticks_diff(start,time.ticks_ms()) #calculate elapsedTime``= ping

  • @jc_serg If you are looking for a script: there is one here:

Pycom on Twitter