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"
Parents
  • Hi,

     

    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.

    Can you share logs and relevant code for this procedure?

    Are you closing your sockets after use?

     

    If you are already connected via a socket, you should not run connect() multiple times.

     

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

    This sample does not have support for the board combination that you're using:

    https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/samples/net/https_client/README.html#requirements

     

    Kind regards,

    Håkon

  • I tested closing my socket after use but it did not help.

    Here's the relevant code:

    int sock4 = -1;
    
    static int wifi_connect(void) { // same definition as WiFi station example }
    
    static int wifi_shutdown(void)
    {   // based off NCS v2.9.1 (nrf/samples/wifi/shutdown)
    	int ret;
    	struct net_if *iface = net_if_get_default();
    
    	if (!net_if_is_admin_up(iface)) {
    		return 0;
    	}
    
    	ret = net_if_down(iface);
    	if (ret) {
    		LOG_ERR("Cannot bring down iface (%d)", ret);
    		return ret;
    	}
    
    	LOG_INF("Interface down");
    
    	return 0;
    }
    
    int connect_to_server(void)
    {   // based off Websocket client example
        
    	struct sockaddr_in addr4;
    	size_t amount;
    	int ret = -1;
    
    	LOG_INF("Try to open as client");
    	while (ret < 0) {
    		if (IS_ENABLED(CONFIG_NET_IPV4)) {
    			ret = connect_socket(AF_INET, SERVER_ADDR4, SERVER_PORT,
    						&sock4, (struct sockaddr *)&addr4,
    						sizeof(addr4));
    		}
    		if (sock4 < 0 || ret < 0) {
    			LOG_ERR("Cannot create HTTP connection.");
    			k_sleep(K_SECONDS(1));
    		}
    	}
    }
    
    int send_data_to_server(void)
    {   // based off Websocket client example
    
    	/* Just an example how to set extra headers */
    	const char *extra_headers[] = {
    		"Origin: http://foobar\r\n",
    		NULL
    	};
    	int websock4 = -1;
    	int32_t timeout = 150;	// units [ms]
    
    	if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
    		struct websocket_request req;
    
    		memset(&req, 0, sizeof(req));
    
    		req.host = SERVER_ADDR4;
    		req.url = "/";
    		req.optional_headers = extra_headers;
    		req.cb = connect_cb;
    		req.tmp_buf = temp_recv_buf_ipv4;
    		req.tmp_buf_len = sizeof(temp_recv_buf_ipv4);
    
    		websock4 = websocket_connect(sock4, &req, timeout, "IPv4");
    		if (websock4 < 0) {
    			LOG_ERR("Cannot connect to %s:%d (err %d)",
    				SERVER_ADDR4, SERVER_PORT,
    				websock4);
    			// close(sock4);
    		}
    	}
    	if (websock4 < 0) {
    		LOG_ERR("No IPv4 connectivity");
    		return -2;
    	}
    	LOG_INF("Websocket IPv4 %d", websock4);
    }
    
    int start_app(void)
    {       // main() calls this; my main() has same definition as main() from WiFi station example
    	LOG_INF("Static IP address (overridable): %s/%s -> %s",
    		CONFIG_NET_CONFIG_MY_IPV4_ADDR,
    		CONFIG_NET_CONFIG_MY_IPV4_NETMASK,
    		CONFIG_NET_CONFIG_MY_IPV4_GW);
    		
    	while (1) {
    	    wifi_connect();
    
    		while (!context.connect_result) {
    			cmd_wifi_status();
    			k_sleep(K_MSEC(STATUS_POLLING_MS));
    		}
    
    		if (context.connected) {
    			cmd_wifi_status();
    
    			connect_to_server();
    			
    			// Send data multiple times
    			for (uint8_t i = 0; i < 10; i ++) {
    			    send_data_to_server();
    			}
    			
    			// Close socket
    			    // NOTE - didn't try with websock4, since I see "No IPv4 connectivity" log output
    			if (sock4 >= 0) {
    			    close(sock4);
    			}
    
    			wifi_shutdown();
    			k_sleep(K_SECONDS(1));
    
    			sys_poweroff();
    		}
    	}
    }

    Here's the log I get:

    [00:00:00.507,476] <inf> wifi_nrf_bus: SPIM spi@4002f000: freq = 8 MHz
    [00:00:00.507,507] <inf> wifi_nrf_bus: SPIM spi@4002f000: latency = 0
    [00:00:00.642,028] <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.773,223] <inf> net_config: Initializing network
    [00:00:00.773,223] <inf> net_config: Waiting interface 1 (0x200018c8) to be up...
    [00:00:00.773,406] <inf> net_config: IPv4 address: 192.168.1.99
    [00:00:00.773,468] <inf> net_config: Running dhcpv4 client...
    [00:00:00.774,505] <inf> sta: Starting nrf52840dk with CPU frequency: 64 MHz
    [00:00:00.777,252] <inf> wifi_supplicant: wpa_supplicant initialized
    ....[00:00:01.774,780] <inf> sta: Static IP address (overridable): 192.168.1.99/255.255.255.0 -> 192.168.1.1
    [00:00:03.476,074] <inf> wifi_mgmt_ext: Connection requested
    [00:00:03.476,104] <inf> sta: Connection requested
    [00:00:03.476,165] <inf> sta: ==================
    [00:00:03.476,196] <inf> sta: State: SCANNING
    [00:00:03.776,336] <inf> sta: ==================
    [00:00:03.776,367] <inf> sta: State: SCANNING
    [00:00:04.076,507] <inf> sta: ==================
    [00:00:04.076,538] <inf> sta: State: SCANNING
    [00:00:04.376,678] <inf> sta: ==================
    [00:00:04.376,708] <inf> sta: State: SCANNING
    [00:00:04.676,849] <inf> sta: ==================
    [00:00:04.676,879] <inf> sta: State: SCANNING
    [00:00:04.977,020] <inf> sta: ==================
    [00:00:04.977,050] <inf> sta: State: SCANNING
    [00:00:05.277,191] <inf> sta: ==================
    [00:00:05.277,221] <inf> sta: State: SCANNING
    [00:00:05.577,362] <inf> sta: ==================
    [00:00:05.577,392] <inf> sta: State: SCANNING
    [00:00:05.877,532] <inf> sta: ==================
    [00:00:05.877,563] <inf> sta: State: SCANNING
    [00:00:06.177,703] <inf> sta: ==================
    [00:00:06.177,734] <inf> sta: State: SCANNING
    [00:00:06.477,874] <inf> sta: ==================
    [00:00:06.477,905] <inf> sta: State: SCANNING
    [00:00:06.778,045] <inf> sta: ==================
    [00:00:06.778,076] <inf> sta: State: SCANNING
    [00:00:07.078,216] <inf> sta: ==================
    [00:00:07.078,247] <inf> sta: State: SCANNING
    [00:00:07.378,967] <inf> sta: ==================
    [00:00:07.378,997] <inf> sta: State: AUTHENTICATING
    [00:00:07.679,168] <inf> sta: ==================
    [00:00:07.679,199] <inf> sta: State: AUTHENTICATING
    [00:00:07.829,711] <inf> sta: Connected
    [00:00:07.994,964] <inf> sta: ==================
    [00:00:07.994,995] <inf> sta: State: COMPLETED
    [00:00:07.995,025] <inf> sta: Interface Mode: STATION
    [00:00:07.995,056] <inf> sta: Link Mode: WIFI 4 (802.11n/HT)
    [00:00:07.995,086] <inf> sta: SSID: ************
    [00:00:07.995,117] <inf> sta: BSSID: ************
    [00:00:07.995,147] <inf> sta: Band: 2.4GHz
    [00:00:07.995,147] <inf> sta: Channel: 1
    [00:00:07.995,178] <inf> sta: Security: WPA2-PSK
    [00:00:07.995,208] <inf> sta: MFP: Optional
    [00:00:07.995,208] <inf> sta: RSSI: -37
    [00:00:07.995,208] <inf> sta: Try to open as client
    [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.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.
    [00:00:16.278,167] <err> sta: Cannot connect to 192.168.101.98:3001 (err -113)
    [00:00:16.278,198] <err> sta: No IPv4 connectivity

    Again, for context:

    • I call connect() multiple times because there's a timeout in trying to connect; it's confirmed on my server too that no connection is established.
    • I see the attempt to connect to socket will timeout if I see "Cannot connect to IPv4 remote (-116)"
    • The data arrives on the server if I see " Cannot connect to 192.168.101.98:3001 (err -113)". The message is misleading but my server does receive data.
  • 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�

  • 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.

Reply Children
  • 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.

  • Hi,

     

    esmart-engineering said:
    The constant "Cannot connect to 192.168.32.166:3001 (err -113)" is not the issue I am talking about

    Your logs show both -113 and -116.

    esmart-engineering said:
    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.

    According to the logs that you posted, you are still running in a for-loop. If you look into what the websocket library does, it will create a new socket for each try. This means that you will exhaust the amount of sockets available if you hammer on.

    Håkon Alseth said:
    However, if you only run the function send_data_to_server() once, it will show:

    To debug an issue, please take in one step at the time, then adjust when you're seeing issues.

    This includes trying different scripts, specifically those intended to work with the sample itself, ie. websocketd for instance.

     

    Kind regards,

    Håkon

Related