FiPy Wi-Fi Node-to-Node Socket Error (OSError: -1)



  • Hello!

    Apologies if someone has already posted this issue (or a similar one).

    I'm attempting to connect two FiPy's directly (no router) through Wi-Fi with one of the Fipy's as the client, and the other as the access point. I've managed to get the client to connect to the access point. However, when I try to create a socket on the client side, I get OSError: -1.

    Potential issues (based on Python documentation) and attempted solutions:

    1. Memory issues. Updated the firmware to 1.17.5.b6.
    2. Socket connection issues. Double checked IP and socket connection parameters. Please see below for the client code.
    # This file is prototype source code for Wi-Fi master that establishes
    # uni-directional communication (Master -> Slave) with Wi-Fi slave.
    
    from network import WLAN
    import socket
    import pycom
    import time
    
    networkFound = Falseo
    netSSID = None
    
    pycom.heartbeat(False)
    pycom.rgbled(0xff3333) # red
    time.sleep(1)
    print("Starting MASTER up!")
    
    wlan = WLAN()
    wlan.init(mode=WLAN.STA_AP, ssid="FiPy1_WiFi", auth=(WLAN.WPA2,"fipy1wifiprototype"), channel=11, antenna=WLAN.EXT_ANT)
    # wlan.init(mode=WLAN.STA)
    # network_found = False
    
    while networkFound is False:
        print("Commencing scan!")
        nets = wlan.scan()
        print(nets)
        time.sleep(5)
        for net in nets:
            # Connect to Wi-Fi slave access point
            if net.ssid == "FiPy2_WiFi":
                # network_found = True
                print(net.ssid)
                time.sleep(1)
                networkFound = True
                wlan.connect(net.ssid, auth=(net.sec, "fipy2wifiprototype"), timeout=5000)
                netSSID = net.ssid
                pycom.rgbled(0x33ff33) # green
                time.sleep(1)
                # wlan.ifconfig(id=0)
                time.sleep(10)
                print(wlan.isconnected())
                time.sleep(1)
                break
            else:
                pycom.rgbled(0xff3333) # red
                time.sleep(1)
                print("NOT CONNECTED!")
    
    # print (wlan.ifconfig())
    
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        pycom.rgbled(1000) # blue
        time.sleep(1)
        print ("Socket successfully created!")
    except socket.error as err:
        pycom.rgbled(0xff3333) # red
        time.sleep(1)
        print ("Socket failed to be created with error %s" %err)
    
    print(netSSID)
    time.sleep(2)
    print(wlan.isconnected())
    time.sleep(2)
    # port = 12345
    # host_ip = '192.168.4.1'
    print(socket.getaddrinfo("192.168.4.1", 80))
    time.sleep(2)
    print(socket.getaddrinfo("192.168.4.1", 80)[0][-1])
    time.sleep(2)
    s.connect(socket.getaddrinfo("192.168.4.1", 80)[0][-1])
    pycom.rgbled(0xffbb33) # orange
    time.sleep(2)
    print("The socket has successfully connected to your boy on port = %s" %(host_ip))
    
    dataString = "Sent from FiPy1"
    dataBytes = bytes(dataString, "utf-8")
    
    # Send data as quickly as possible
    for iteration in range(10):
        s.sendto(dataBytes, host_ip)
        pycom.rgbled(0x33ffdd) # green/blue hybrid
        print("Sent Data!")
    
    print("End of program!")
    
    # inputs = s.recv(1024)
    # barray = binascii.hexlify(inputs)
    # barray=barray.decode('utf-8')
    # color = hex(int(barray,16) + int ("0x00",16))
    # color = int(color)
    # pycom.rgbled(color)
    

    For reference, below is the access point code.

    # This file is prototype source code for Wi-Fi slave that establishes
    # uni-directional communication (Master -> Slave) with Wi-Fi master.
    
    from network import WLAN
    import socket
    import pycom
    import time
    
    pycom.heartbeat(False)
    pycom.rgbled(0xff3333) # red
    time.sleep(1)
    print("Starting SLAVE up!")
    
    conn = None
    
    # Configure access point
    wlan = WLAN()
    wlan.init(mode=WLAN.AP, ssid="FiPy2_WiFi", auth=(WLAN.WPA2, 'fipy2wifiprototype'), channel=11, antenna=WLAN.EXT_ANT)
    # wlan.init(mode=WLAN.STA)
    
    time.sleep(10)
    wlan.ifconfig(id=1)
    time.sleep(10)
    print(wlan.isconnected())
    time.sleep(1)
    
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        pycom.rgbled(0x33ff33) # green
        time.sleep(1)
        print ("Socket successfully created!")
    except socket.error as err:
        pycom.rgbled(0xff3333) # red
        time.sleep(1)
        print ("socket failed to be created with error %s" %err)
    
    port = 12345
    host_ip = ''
    s.bind((host_ip,port))
    s.listen()
    pycom.rgbled(1000) # blue
    time.sleep(1)
    print("Listening for connection requests!")
    time.sleep(1)
    
    # Keep looping until connected.`
    # 1. One socket for listening for connection requests.
    # 2. One socket for actually receiving and sending data to client.
    while conn is None:
        print("Reached!")
        conn, addr = s.accept()
    
    pycom.rgbled(0xffbb33) # orange
    time.sleep(1)
    
    # Receive data from socket buffer
    while True:
        data = conn.recv(256)
        data = data.decode("utf-8")
        pycom.rgbled(0x33ffdd) # green/blue hybrid
        time.sleep(1)
        print(data)
    

    I'm somewhat baffled as I have been able to successfully connect to a router and create a socket on the client side.

    Any insights or suggestions would be greatly appreciated.

    Thank you!



  • Hi @joseph,
    No, not yet. I have spent a few days on it, but no progress. I keep getting Connection refused when trying to connect, so something must be wrong with my server code. Have you tried connecting it to a different device (e.g your computer)?



  • Hi @dan,

    Were you able to find anything more about the issue?

    And if I may, could I ask you to test the on a pair of LoPy's? If the code works on the LoPy's, we'll probably purchase several LoPy's.

    Thanks!



  • Hi @Joseph,

    I'm looking into it, it indeed doesn't work. I was able to get a more "descriptive" error OSError: [Errno 104] ECONNRESET by adding try-except to s.connect(...). I'll get back to you when I have more info.



Pycom on Twitter