Websocket connection has timeout too frequently

I am evaluating the nRF7002's WiFi capabilities and starting off with the nRF52840DK + nRF7002EK and a project that connects to a socket and sends data. I've primarily used the Websocket Client example (NCS v2.9.1, zephyr/samples/net/sockets/websocket_client) though I mixed in some code from the WiFi station project to ensure I connect to my local WiFi first (NCS v2.9.1, nrf/samples/wifi/sta). The server socket is one I set up with a simple Python script that simply reports any data sent to it.

Functionally my code is working: able to connect to WiFi, connect to socket, and send some data. For sending data, I'm having it repeatedly call websocket_connect(); it will return with an error but my server still gets the data (which is acceptable at this time of evaluation).

However, once my code gets to the connect() call as part of connect_socket(), it will occasionally (roughly 50% of the time) timeout after the default 3sec. I modified it to repeatedly call connect() until it succeeds, and noticed it may take 2-3 tries to connect. But this means that it's taking almost 10sec at most just to simply connect to a socket.

Could you please help look into why this operation is taking so long? We've created custom boards with nRF for years, and recently also with nRF7002. While it was developed through Linux drivers and such rather than with NCS/VS-Code/etc., we determined it can connect to WiFi, connect to socket, and send a little data all comfortably in <10sec. So I'm expecting similar performance.

More info: I am using a Windows 11 machine, VS Code, & nRF Connect Extension. It was already mentioned earlier but we're also using NCS v2.9.1, nRF52840DK, and nRF7002EK.

--------------------------------------------------------

Note that I also tried one of Nordic's official networking samples - the HTTPS client one (NCS v2.9.1, nrf/samples/net/https_client). But I'm not able to proceed that far because it's unable to connect to my local WiFi.

  • Made a build config
    • Board target: nrf52840dk/nrf52840
    • Config files: prj.conf, boards/native_sim.conf
      • Had to add native_sim.conf or else I would've had build errors
    • CMake arguments: -DSHIELD="nrf7002ek"
  • Tried the build from above, but command line wouldn't respond to any user input, so I couldn't type in the "wifi_cred" command
  • Then tried modifying prj.conf to have the static WiFi credentials, but it still wouldn't proceed past "Connecting to the network"
  • It has been a week since the posting and we've only gotten small suggestions that are not helping whatsoever. Please look into this more in depth and give us a solid concrete solution for us to try.

  • Hi,

     

    esmart-engineering said:
    My log is from before making the change,

    It is important that you disclose such information when posting, to avoid any confusion, such is the scenario right now.

    esmart-engineering said:
    It has been a week since the posting and we've only gotten small suggestions that are not helping whatsoever. Please look into this more in depth and give us a solid concrete solution for us to try.

    I understand that this issue is blocking your evaluation and development, and I aim to help you with this. However, I am going to stress this as much as I can, please do not truncate any information that you share on devzone. Logs show warnings and errors, and leaving them out makes it hard to be able to help you.

     

    Can you please share your configurations and settings, so I can recreate this in the STA sample?

    You are combining the nRF52840 with the nRF7002, which is a tight fit wrt. RAM, meaning that it needs to be optimized quite heavily.

     

    Kind regards,

    Håkon

  • Could be a RAM issue, but I also tested on an nRF7002DK (using nRF53 which has 2x RAM as nRF52) and saw the same issue.

    I've attached the whole project here you can use. Please build based off the build configuration presets I have for the nRF52+nRF70, as well as the nRF7002DK I just tried.

    wifi_station_w_sockets.zip

  • Hi,

     

    This is the log that I am seeing:

    *** Booting nRF Connect SDK v2.9.2-4ab7b98fc76f ***
    *** Using Zephyr OS v3.7.99-aa34a5632971 ***
    [00:00:00.688,720] <inf> net_config: Initializing network
    [00:00:00.688,751] <inf> net_config: Waiting interface 1 (0x20001290) to be up...
    [00:00:00.688,903] <inf> net_config: IPv4 address: 192.168.32.99
    [00:00:00.688,964] <inf> net_config: Running dhcpv4 client...
    [00:00:00.689,788] <inf> sta: Starting nrf7002dk with CPU frequency: 64 MHz
    [00:00:00.691,467] <inf> wifi_supplicant: wpa_supplicant initialized
    [00:00:01.689,971] <inf> sta: Static IP address (overridable): 192.168.32.99/255.255.255.0 -> 192.168.32.1
    [00:00:03.239,898] <inf> wifi_mgmt_ext: Connection requested
    [00:00:03.239,929] <inf> sta: Connection requested
    [00:00:03.239,990] <inf> sta: ==================
    [00:00:03.240,020] <inf> sta: State: SCANNING
    [00:00:03.540,161] <inf> sta: ==================
    [00:00:03.540,191] <inf> sta: State: SCANNING
    [00:00:03.840,332] <inf> sta: ==================
    [00:00:03.840,362] <inf> sta: State: SCANNING
    [00:00:04.140,502] <inf> sta: ==================
    [00:00:04.140,533] <inf> sta: State: SCANNING
    [00:00:04.440,673] <inf> sta: ==================
    [00:00:04.440,704] <inf> sta: State: SCANNING
    [00:00:04.740,844] <inf> sta: ==================
    [00:00:04.740,875] <inf> sta: State: SCANNING
    [00:00:05.041,015] <inf> sta: ==================
    [00:00:05.041,015] <inf> sta: State: SCANNING
    [00:00:05.341,186] <inf> sta: ==================
    [00:00:05.341,186] <inf> sta: State: SCANNING
    [00:00:05.641,326] <inf> sta: ==================
    [00:00:05.641,357] <inf> sta: State: SCANNING
    [00:00:05.941,528] <inf> sta: ==================
    [00:00:05.941,558] <inf> sta: State: SCANNING
    [00:00:06.241,668] <inf> sta: ==================
    [00:00:06.241,699] <inf> sta: State: SCANNING
    [00:00:06.541,870] <inf> sta: ==================
    [00:00:06.541,900] <inf> sta: State: SCANNING
    [00:00:06.842,041] <inf> sta: ==================
    [00:00:06.842,041] <inf> sta: State: SCANNING
    [00:00:07.142,211] <inf> sta: ==================
    [00:00:07.142,242] <inf> sta: State: SCANNING
    [00:00:07.442,352] <inf> sta: ==================
    [00:00:07.442,382] <inf> sta: State: SCANNING
    [00:00:07.742,523] <inf> sta: ==================
    [00:00:07.742,553] <inf> sta: State: SCANNING
    [00:00:08.042,724] <inf> sta: ==================
    [00:00:08.042,755] <inf> sta: State: AUTHENTICATING
    [00:00:08.342,895] <inf> sta: ==================
    [00:00:08.342,926] <inf> sta: State: AUTHENTICATING
    [00:00:08.452,941] <inf> sta: Connected
    [00:00:12.475,097] <inf> net_dhcpv4: Received: 192.168.32.198
    [00:00:12.475,280] <inf> net_config: IPv4 address: 192.168.32.198
    [00:00:12.475,280] <inf> net_config: Lease time: 36000 seconds
    [00:00:12.475,341] <inf> net_config: Subnet: 255.255.255.0
    [00:00:12.475,372] <inf> net_config: Router: 192.168.32.1
    [00:00:12.475,463] <inf> sta: DHCP IP address: 192.168.32.198
    [00:00:12.657,257] <inf> sta: ==================
    [00:00:12.657,287] <inf> sta: State: COMPLETED
    [00:00:12.657,318] <inf> sta: Interface Mode: STATION
    [00:00:12.657,348] <inf> sta: Link Mode: WIFI 6 (802.11ax/HE)
    [00:00:12.657,379] <inf> sta: SSID: OpenWrt
    [00:00:12.657,409] <inf> sta: BSSID: FC:34:97:0B:F6:DC
    [00:00:12.657,409] <inf> sta: Band: 5GHz
    [00:00:12.657,440] <inf> sta: Channel: 44
    [00:00:12.657,440] <inf> sta: Security: WPA2-PSK
    [00:00:12.657,470] <inf> sta: MFP: Optional
    [00:00:12.657,470] <inf> sta: RSSI: -60
    [00:00:13.657,562] <inf> sta: Try to open as client
    [00:00:13.740,417] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.740,417] <err> sta: No IPv4 connectivity
    [00:00:13.740,905] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.740,905] <err> sta: No IPv4 connectivity
    [00:00:13.741,363] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.741,363] <err> sta: No IPv4 connectivity
    [00:00:13.741,821] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.741,851] <err> sta: No IPv4 connectivity
    [00:00:13.742,309] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.742,309] <err> sta: No IPv4 connectivity
    [00:00:13.742,767] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.742,797] <err> sta: No IPv4 connectivity
    [00:00:13.743,255] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.743,255] <err> sta: No IPv4 connectivity
    [00:00:13.743,713] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.743,713] <err> sta: No IPv4 connectivity
    [00:00:13.744,201] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.744,201] <err> sta: No IPv4 connectivity
    [00:00:13.744,659] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.744,659] <err> sta: No IPv4 connectivity
    [00:00:13.766,021] <inf> sta: Interface down
    [00:00:13.766,143] <inf> wifi_supplicant: Network interface 1 (0x20001290) down
    

    On the server side, I am running a small py script just to setup a server on port 3001:

    #! /usr/bin/python
    # a simple tcp server
    import socket,os
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('192.168.32.166', 3001))
    sock.listen(5)
    while True:
        connection,address = sock.accept()
        buf = connection.recv(1024)
        print(buf)
        connection.send(buf)     
        connection.close()
     

     

    Which prints this each time I connect:

    b'GET / HTTP/1.1\r\nHost: 192.168.32.166\r\nOrigin: http://foobar\r\nSec-WebSocket-Key: lm0W7zOrdWFToQCN8Kkcmg==\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Version: 13\r\n\r\n'
      

    Running the websocketd as described here:

    https://docs.zephyrproject.org/latest/samples/net/sockets/websocket_client/README.html#running-websocket-server-in-linux-host

     

    Shows similar issues as before, ie. that the log spits out:

    [00:00:13.955,291] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:13.955,291] <err> sta: No IPv4 connectivity
    [00:00:14.078,338] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.078,338] <err> sta: No IPv4 connectivity
    [00:00:14.178,863] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.178,894] <err> sta: No IPv4 connectivity
    [00:00:14.279,449] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.279,449] <err> sta: No IPv4 connectivity
    [00:00:14.380,004] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.380,035] <err> sta: No IPv4 connectivity
    [00:00:14.480,621] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.480,651] <err> sta: No IPv4 connectivity
    [00:00:14.581,207] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.581,237] <err> sta: No IPv4 connectivity
    [00:00:14.681,793] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.681,793] <err> sta: No IPv4 connectivity
    [00:00:14.782,348] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.782,379] <err> sta: No IPv4 connectivity
    [00:00:14.882,934] <err> sta: Cannot connect to 192.168.32.166:3001 (err -113)
    [00:00:14.882,934] <err> sta: No IPv4 connectivity

     

    However, if you only run the function send_data_to_server() once, it will show:

    *** Booting nRF Connect SDK v2.9.2-4ab7b98fc76f ***
    *** Using Zephyr OS v3.7.99-aa34a5632971 ***
    [00:00:00.638,793] <inf> net_config: Initializing network
    [00:00:00.638,793] <inf> net_config: Waiting interface 1 (0x20000d98) to be up...
    [00:00:00.638,977] <inf> net_config: IPv4 address: 192.168.32.99
    [00:00:00.639,007] <inf> net_config: Running dhcpv4 client...
    [00:00:00.639,953] <inf> sta: Starting nrf7002dk with CPU frequency: 64 MHz
    [00:00:00.641,845] <inf> wifi_supplicant: wpa_supplicant initialized
    [00:00:01.640,136] <inf> sta: Static IP address (overridable): 192.168.32.99/255.255.255.0 -> 192.168.32.1
    [00:00:03.560,089] <inf> wifi_mgmt_ext: Connection requested
    [00:00:03.560,119] <inf> sta: Connection requested
    [00:00:03.560,180] <inf> sta: ==================
    [00:00:03.560,211] <inf> sta: State: SCANNING
    [00:00:08.062,774] <inf> sta: ==================
    [00:00:08.062,774] <inf> sta: State: SCANNING
    [00:00:08.362,945] <inf> sta: ==================
    [00:00:08.362,976] <inf> sta: State: AUTHENTICATING
    [00:00:08.663,116] <inf> sta: ==================
    [00:00:08.663,146] <inf> sta: State: AUTHENTICATING
    [00:00:08.730,682] <inf> sta: Connected
    [00:00:12.988,555] <inf> sta: ==================
    [00:00:12.988,586] <inf> sta: State: COMPLETED
    [00:00:12.988,616] <inf> sta: Interface Mode: STATION
    [00:00:12.988,647] <inf> sta: Link Mode: WIFI 6 (802.11ax/HE)
    [00:00:12.988,647] <inf> sta: SSID: OpenWrt
    [00:00:12.988,677] <inf> sta: BSSID: FC:34:97:0B:F6:DC
    [00:00:12.988,708] <inf> sta: Band: 5GHz
    [00:00:12.988,708] <inf> sta: Channel: 44
    [00:00:12.988,739] <inf> sta: Security: WPA2-PSK
    [00:00:12.988,769] <inf> sta: MFP: Optional
    [00:00:12.988,769] <inf> sta: RSSI: -55
    [00:00:13.752,960] <inf> net_dhcpv4: Received: 192.168.32.198
    [00:00:13.753,143] <inf> net_config: IPv4 address: 192.168.32.198
    [00:00:13.753,173] <inf> net_config: Lease time: 36000 seconds
    [00:00:13.753,204] <inf> net_config: Subnet: 255.255.255.0
    [00:00:13.753,234] <inf> net_config: Router: 192.168.32.1
    [00:00:13.753,356] <inf> sta: DHCP IP address: 192.168.32.198
    [00:00:13.988,830] <inf> sta: Try to open as client
    [00:00:14.059,570] <err> sta: 12
    [00:00:14.069,183] <inf> sta: Websocket 13 for IPv4 connected.
    [00:00:14.069,183] <inf> sta: Websocket IPv4 13
    

     

    I added these configurations to maximize the amount of sockets / net contexts:

    CONFIG_ZVFS_OPEN_MAX=20
    CONFIG_NET_SOCKETS_POLL_MAX=20
    CONFIG_NET_CONFIG_NEED_IPV6=n
    CONFIG_NET_IPV6=n
    CONFIG_NET_MAX_CONTEXTS=20
    

    Could you try the same and see if it runs accordingly?

     

    Kind regards,

    Håkon

  • Apologies for the late response.

    The constant "Cannot connect to 192.168.32.166:3001 (err -113)" is not the issue I am talking about. My issue is seeing "Cannot connect to IPv4 remote (-116)" which is coming from connect_to_server(). Please carefully re-examine the exact problem I am talking about. Also pay attention to the other details I've shared; for example, the problem occasionally (roughly 50% of the time) occurs.

    Your Python server is set up to receive 1 message, which is not my intention. It should connect to the nRF, receive multiple messages, then require user to Ctrl+C and rerun the server whenever the nRF needs to be run again. I've attached my script and an example of what you should see.

    import socket
    import datetime
    PORT = 3001
    
    ip_addr = '0.0.0.0'
    
    while True:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.bind((ip_addr, PORT))
            s.listen()
            print(f"Server listening on {ip_addr}:{PORT}")
            conn, addr = s.accept()
            with conn:
                print(f"{datetime.datetime.now()}: Connected by {addr}")
                while True:
                    did_receive_data = False
                    while not did_receive_data:
                        data = conn.recv(1024)  # Receive up to 1024 bytes
                        if not data:
                            continue
                        print(f"{datetime.datetime.now()}: Received from client: {data.decode('utf-8')}")
                        response = f"Server received: {data.decode('utf-8')}"
                        conn.sendall(response.encode('utf-8'))
                        did_receive_data = True
            conn.close()
            s.close()
    
    $ python3 forumnrfserver.py
    Server listening on 0.0.0.0:3001
    2025-09-02 14:39:09.733185: Connected by ('192.168.101.143', 44678)
    2025-09-02 14:39:09.839348: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: YVsNcLpQVGiMRgm59KgalA==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:09.901119: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: vRC8G5IrTdSyeUaeW0M4kg==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:10.037705: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: naFmSNGwbziixWIvyMAsNw==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:10.186991: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: 75Vs3+u38V5dawGasrhY5A==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:10.341248: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: ArFh1S9qCOPwpyYI4nWryA==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:10.490259: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: Po6cfaXMYfPUt2x5P7BnCw==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:10.647954: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: TbSY5fwvPJZ77+lFFMI7jw==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:10.788101: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: SjDrtG71Qf8uWQ+H9BfcVA==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:10.936990: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: PM/H9HezqWeN8kO256YoRg==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    
    
    2025-09-02 14:39:11.090528: Received from client: GET / HTTP/1.1
    Host: 192.168.101.98
    Origin: http://foobar
    Sec-WebSocket-Key: UcMtSGmGAuNlk5ohphb43A==
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Version: 13
    

    I went ahead and tried your configurations anyway, but it did not help with my issue.

Related