WiPy 3.0 Socket Server connection issues

  • I have a very basic socket server running on my WiPy 3 housed by the Pysense 3. If I continuously provide input to the server, it will work without issue for over 24 hours (until I got tired of sending requests). If I fail to send data to the server for anywhere from 60-120 seconds, it becomes non-responsive and requires a restart of the device.

    My boot.py:

    import machine
    from network import WLAN
    wlan = WLAN() # get current object, without changing the mode
    if machine.reset_cause() != machine.SOFT_RESET:
        wlan.init(mode=WLAN.STA, power_save=False)
        # configuration below MUST match your home router settings!!
        wlan.ifconfig(config=('', '', '', ''))
    if not wlan.isconnected():
        # change the line below to match your network ssid, security and password
        wlan.connect('sid', auth=(WLAN.WPA2, 'mypassword'), timeout=5000)
        while not wlan.isconnected():
            machine.idle() # save power while waiting

    My main.py:

    import usocket
    import _thread
    import time
    import time
    import pycom
    from pysense import Pysense
    import machine
    from LIS2HH12 import LIS2HH12
    from SI7006A20 import SI7006A20
    from LTR329ALS01 import LTR329ALS01
    from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE
    # Thread for handling a client
    def client_thread(clientsocket,n):
        # Receive maxium of 12 bytes from the client
        r = clientsocket.recv(12)
        # If recv() returns with 0 the other end closed the connection
        if len(r) == 0:
            # Do something wth the received data...
            print("Received: {}".format(str(r)))
        # Sends back some data
    #    clientsocket.send(str(n))
        # Close the socket and terminate the thread
    # Set up server socket
    serversocket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
    serversocket.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
    serversocket.bind(("", 6543))
    # Accept maximum of 5 connections at the same time
    # Unique data to send back
    c = 0
    py = Pysense()
    lt = LTR329ALS01(py)
    while True:
        # Accept the connection of the clients
        print("waiting to accept")
        (clientsocket, address) = serversocket.accept()
        # Start a new thread to handle the client
        print("new thread")
        data0, data1 = lt.light();
        client_thread(clientsocket, str(data0))
        #_thread.start_new_thread(client_thread, (clientsocket, str(data0)))
        c = c+1

    I had reduced the server portion to be single threaded to try to take multithreading out of the equation.

    Thanks for any insight on the issue.


  • @Xykon Hi, appreciate the response.

    I get back -97 for the RSSI from the router.

    I've run this same server from the WiPy acting as an access point with no change in behavior. The behavior has been confirmed using both an integrated application and netcat itself from more than one device.

    Is there any logging from the WiPy that can be accessed to show reasons it may potentially lose connectivity or disassociate from an access point?


  • administrators

    @not_karl Hello Karl,

    I tested your code on a WiPy3 in our office and both the normal and threaded code work as expected.

    The only thing I can recommend is turning off pybytes and wifi on boot when using the latest firmware:

    import pycom

    It might be related to the wifi router used. You can check the signal strength with a scan:

    from network import WLAN
    wl = WLAN(mode=WLAN.STA)
    for x in wl.scan():
        print('{}: {}'.format(x.ssid,x.rssi))

    Have you tried setting up the module as an access point and connecting directly to it?

  • I've updated to the latest firmware (or at least updated the firmware on both the Pysense and WiPy 3.0).

    My boot.py above inconsistently results in a connection to the WIFI. More often than not I have to attach via USB and manually execute my boot.py again. When I first login, the wlan.isconnected() does return True.

    Additionally, I still see the same issue with the device becoming unavailable over the network even though the wlan still claims to be connected.

    Is there some sort of sleep going on the background or something that can be logged or traced to understand where things are getting held up?


  • @jcaron

    Boot up displays:

    Pycom MicroPython 1.18.2.r1 [v1.8.6-849-e0fb68e] on 2018-12-08; WiPy with ESP32

    I am not using PyBytes.


  • This post is deleted!

  • This post is deleted!

  • @not_karl Can you clarify what firmware version you use, and whether PyBytes is disabled?

Log in to reply

Pycom on Twitter