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"
  • I already have CONFIG_NET_DHCPV4=y. In addition, I had these which probably explain the IP's/masks in the log:

    • CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.168.1.99"
    • CONFIG_NET_CONFIG_MY_IPV4_NETMASK="255.255.255.0"
    • CONFIG_NET_CONFIG_MY_IPV4_GW="192.168.1.1"
  • Hi,

     

    Can you confirm that you're receiving an IP within 192.168.101.x range? Or with a netmask of 255.255.0.0 if in the 192.168.x.x range?

    Could you please share the full log, and do not take out any of the lines? Feel free to redact the SSID for instance.

     

    Kind regards,

    Håkon

  • Server reports it connected to nRF with an IP 192.168.101.x

    Log only had the following removed. Apologies. Initially it was to avoid confusion on when the nRF connected to the socket, but looking at it now it could be relevant:

    ...
    
    [00:00:09.321,838] <wrn> net_tcp: net_pkt alloc failure
    [00:00:09.626,037] <wrn> net_tcp: net_pkt alloc failure
    [00:00:09.930,236] <wrn> net_tcp: net_pkt alloc failure
    [00:00:10.864,440] <inf> net_dhcpv4: Received: 192.168.101.143
    [00:00:10.864,654] <inf> net_config: IPv4 address: 192.168.101.143
    [00:00:10.864,685] <inf> net_config: Lease time: 86400 seconds
    [00:00:10.864,715] <inf> net_config: Subnet: 255.255.255.0
    [00:00:10.864,776] <inf> net_config: Router: 192.168.101.1
    [00:00:10.864,898] <inf> sta: DHCP IP address: 192.168.101.143
    [00:00:10.951,232] <err> sta: Cannot connect to IPv4 remote (-116)
    [00:00:10.951,232] <err> sta: Cannot create HTTP connection.
    [00:00:12.052,185] <wrn> net_tcp: net_pkt alloc failure
    [00:00:15.052,337] <err> sta: Cannot connect to IPv4 remote (-116)
    [00:00:15.052,337] <err> sta: Cannot create HTTP connection.
    ...

    Regardless, logs indicate 3sec in this particular case between attempting to connect to socket, and receiving the 192.168.101.x IP address. This seems too long.

  • Hi,

     

    You still have packet allocation issues, based on your latest log.

    [00:00:09.321,838] <wrn> net_tcp: net_pkt alloc failure
    [00:00:09.626,037] <wrn> net_tcp: net_pkt alloc failure
    [00:00:09.930,236] <wrn> net_tcp: net_pkt alloc failure

     

    What NET_PKT configuration did you end up using?

     

    Kind regards,

    Håkon

  • As I already mentioned, the issue was resolved with CONFIG_NET_PKT_TX_COUNT. My log is from before making the change, but the log does not differ much with or without the change. And again it does not resolve my primary issue.

    I also tried CONFIG_NET_BUF_DATA_SIZE (to 256, 512, and 4096) and none of these fixed the packet allocation issues like CONFIG_NET_PKT_TX_COUNT did.

    If by "NET_PKT configuration", you mean KConfig flags related to "NET_PKT", then I have CONFIG_NET_PKT_RX_COUNT=8 and CONFIG_NET_PKT_TX_COUNT=16 explicitly stated in my "prj.conf" file. The following is what I see in my build output ".config" file:

    CONFIG_NET_PKT_RX_COUNT=8
    CONFIG_NET_PKT_TX_COUNT=16
    ...
    # CONFIG_NET_PKT_TXTIME is not set
    # CONFIG_NET_PKT_TIMESTAMP is not set
    ...
    # CONFIG_NET_CONTEXT_NET_PKT_POOL is not set
    ...
    CONFIG_NET_PKT_BUF_RX_DATA_POOL_SIZE=4096
    CONFIG_NET_PKT_BUF_TX_DATA_POOL_SIZE=4096
    CONFIG_NET_PKT_BUF_USER_DATA_SIZE=4
    ...
    # CONFIG_NET_PKT_RXTIME_STATS is not set
    # CONFIG_NET_PKT_TXTIME_STATS is not set
    ...
    # CONFIG_NET_PKT_LOG_LEVEL_OFF is not set
    # CONFIG_NET_PKT_LOG_LEVEL_ERR is not set
    # CONFIG_NET_PKT_LOG_LEVEL_WRN is not set
    # CONFIG_NET_PKT_LOG_LEVEL_INF is not set
    # CONFIG_NET_PKT_LOG_LEVEL_DBG is not set
    CONFIG_NET_PKT_LOG_LEVEL_DEFAULT=y
    CONFIG_NET_PKT_LOG_LEVEL=3
    # CONFIG_NET_DEBUG_NET_PKT_ALLOC is not set
    CONFIG_NET_DEBUG_NET_PKT_EXTERNALS=0
    # CONFIG_NET_DEBUG_NET_PKT_NON_FRAGILE_ACCESS is not set
    ...
    # CONFIG_NET_PKT_FILTER is not set
    

    -------

    EDIT #1 - Here's another log, with complete output and with the CONFIG_NET_PKT_TX_COUNT=16 fix.

    [00:00:00.493,316] <inf> wifi_nrf_bus: SPIM spi@4002f000: freq = 8 MHz
    [00:00:00.493,347] <inf> wifi_nrf_bus: SPIM spi@4002f000: latency = 0
    [00:00:00.628,295] <inf> wifi_nrf: Management buffer offload enabled
    
    *** Booting nRF Connect SDK v2.9.1-60d0d6c8d42d ***
    *** Using Zephyr OS v3.7.99-ca954a6216c9 ***
    [00:00:00.758,972] <inf> net_config: Initializing network
    [00:00:00.759,002] <inf> net_config: Waiting interface 1 (0x200018c8) to be up...
    [00:00:00.759,185] <inf> net_config: IPv4 address: 192.168.1.99
    [00:00:00.759,246] <inf> net_config: Running dhcpv4 client...
    [00:00:00.760,192] <inf> sta: Starting nrf52840dk with CPU frequency: 64 MHz
    [00:00:00.762,237] <inf> wifi_supplicant: wpa_supplicant initialized
    [00:00:01.760,375] <inf> sta: Static IP address (overridable): 192.168.1.99/255.255.255.0 -> 192.168.1.1
    [00:00:03.447,082] <inf> wifi_mgmt_ext: Connection requested
    [00:00:03.447,998] <inf> sta: Connection requested
    [00:00:03.448,028] <inf> sta: ==================
    [00:00:03.448,059] <inf> sta: State: SCANNING
    [00:00:03.748,229] <inf> sta: ==================
    [00:00:03.748,260] <inf> sta: State: SCANNING
    [00:00:04.048,400] <inf> sta: ==================
    [00:00:04.048,431] <inf> sta: State: SCANNING
    [00:00:04.348,571] <inf> sta: ==================
    [00:00:04.348,602] <inf> sta: State: SCANNING
    [00:00:04.648,742] <inf> sta: ==================
    [00:00:04.648,773] <inf> sta: State: SCANNING
    [00:00:04.948,913] <inf> sta: ==================
    [00:00:04.948,944] <inf> sta: State: SCANNING
    [00:00:05.249,084] <inf> sta: ==================
    [00:00:05.249,114] <inf> sta: State: SCANNING
    [00:00:05.549,255] <inf> sta: ==================
    [00:00:05.549,285] <inf> sta: State: SCANNING
    [00:00:05.849,426] <inf> sta: ==================
    [00:00:05.849,456] <inf> sta: State: SCANNING
    [00:00:06.149,597] <inf> sta: ==================
    [00:00:06.149,627] <inf> sta: State: SCANNING
    [00:00:06.449,768] <inf> sta: ==================
    [00:00:06.449,798] <inf> sta: State: SCANNING
    [00:00:06.749,938] <inf> sta: ==================
    [00:00:06.749,969] <inf> sta: State: SCANNING
    [00:00:07.050,109] <inf> sta: ==================
    [00:00:07.050,140] <inf> sta: State: SCANNING
    [00:00:07.350,311] <inf> sta: ==================
    [00:00:07.350,341] <inf> sta: State: AUTHENTICATING
    [00:00:07.650,482] <inf> sta: ==================
    [00:00:07.650,512] <inf> sta: State: AUTHENTICATING
    [00:00:07.826,599] <inf> sta: Connected
    [00:00:07.959,411] <inf> sta: ==================
    [00:00:07.959,442] <inf> sta: State: COMPLETED
    [00:00:07.959,472] <inf> sta: Interface Mode: STATION
    [00:00:07.959,503] <inf> sta: Link Mode: WIFI 4 (802.11n/HT)
    [00:00:07.959,533] <inf> sta: SSID: eSmartGuest
    [00:00:07.959,594] <inf> sta: BSSID: ***********
    [00:00:07.959,594] <inf> sta: Band: 2.4GHz
    [00:00:07.959,625] <inf> sta: Channel: 1
    [00:00:07.959,655] <inf> sta: Security: WPA2-PSK
    [00:00:07.959,655] <inf> sta: MFP: Optional
    [00:00:07.959,686] <inf> sta: RSSI: -45
    [00:00:07.959,686] <inf> sta: Try to open as client
    [00:00:08.768,493] <inf> net_dhcpv4: Received: 192.168.101.143
    [00:00:08.768,737] <inf> net_config: IPv4 address: 192.168.101.143
    [00:00:08.768,737] <inf> net_config: Lease time: 86400 seconds
    [00:00:08.768,798] <inf> net_config: Subnet: 255.255.255.0
    [00:00:08.768,829] <inf> net_config: Router: 192.168.101.1
    [00:00:08.768,981] <inf> sta: DHCP IP address: 192.168.101.143
    [00:00:10.592,864] <err> sta: Cannot connect to IPv4 remote (-116)
    [00:00:10.592,864] <err> sta: Cannot create HTTP connection.
    [00:00:14.266,448] <err> sta: Cannot connect to IPv4 remote (-116)
    [00:00:14.266,448] <err> sta: Cannot create HTTP connection.
    [00:00:17.570,404] <err> sta: Cannot connect to 192.168.101.98:3001 (err -113)
    [00:00:17.570,434] <err> sta: No IPv4 connectivity
    [00:00:17.593,536] <inf> wifi_suppli�

Related