FiPy Socket Connection Issues: Potential Firmware Issue?



  • Hello,

    I'm trying to connect two FiPy's directly through Wi-Fi (no router). However, I'm getting socket connection issues (OSError: -1). I've double-checked my code and am wondering if it may be a potential bug in the FiPy firmware (currently on 1.18.1).

    To duplicate my error, please refer to the client and server code below.

    Client FiPy:

    # 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 = False
    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)
    
    port = 9999
    host_ip = '192.168.4.1'
    s.connect((host_ip, port))
    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)
    

    Server FiPy:

    # 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(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 = 9999
    host_ip = ''
    s.bind((host_ip, port))
    s.listen(180)
    pycom.rgbled(1000) # blue
    print("Listening for connection requests!")
    
    # 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:
        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)
    

    Thank you.



  • Hi @timh,

    Yes. The server IP 192.168.4.1, and client IP is 192.168.4.2.



  • Have you confirmed what the client and server IP addresses are ?
    Do you know that the servers IP is 192.168.4.1 and have you confirmed that the clients IP address is not the same and on the same subnet.

    T


 

Pycom on Twitter