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

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

    Scheduled Pinned Locked Moved FiPy
    socket connectwififipy
    4 Posts 2 Posters 898 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.
    • J Offline
      Joseph
      last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • danD Offline
        dan
        last edited by

        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.

        J 1 Reply Last reply Reply Quote 0
        • J Offline
          Joseph @dan
          last edited by Joseph

          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!

          danD 1 Reply Last reply Reply Quote 0
          • danD Offline
            dan @Joseph
            last edited by dan

            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)?

            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