Mqtt over wifi lesson 4 exercise 1 not able to build with nrf7002dk_nrf5340_cpuapp

Hi,

I am unable to build the MQTT over Wi-Fi lesson 4, exercise 1 using the nrf7002dk_nrf5340_cpuapp board configuration, but the same code builds successfully with nrf7002dk_nrf5340_cpuapp_ns.

How can I build it with nrf7002dk_nrf5340_cpuapp?

i got the below error,Also in wifi_credentials.c file all ready  wifi_credentials_internal.h available,

/home/chiragbhavsar/NRF_VS_CODE/v2.6.1/nrf/subsys/net/lib/wifi_credentials/wifi_credentials.c:331: undefined reference to `wifi_credentials_delete_entry'

Parents
  • Hi,

    The lesson only supports building with TF-M, that is, building for the _ns board variant.

    To get it to work without TF-M, you can add support for storing the network credentials in settings storage and use the wifi connect shell command to connect to the network instead.

    # For storing Wi-Fi credentials
    CONFIG_WIFI_CREDENTIALS_BACKEND_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y
    CONFIG_SETTINGS_NVS=y
    # Enable Wi-Fi shell
    CONFIG_NET_L2_WIFI_SHELL=y

    After the device has started up and you see the "Waiting to connect to Wi-Fi" log, use the following command to connect to the network:

    wifi connect -s <ssid> -k 1 -p <password>

    Make sure to replace <ssid> and <password> with the SSID and password of the network you want to connect to.

    Best regards,
    Marte

  • I use a static Wi-Fi SSID and password for the initial connection.

    After establishing the MQTT connection, the wifi custom mobile app provides a new SSID and password on mqtt. How can I change the SSID and password at runtime?

    Is the method below correct?

    uint8_t _CusSsid[] = "xxxxx";
    uint8_t _CusPasswod[] = "xxxxx";
    cnx_params.ssid = _CusSsid;
    cnx_params.ssid_length = strlen(cnx_params.ssid);
    cnx_params.psk = _CusPasswod;
    cnx_params.psk_length = strlen(cnx_params.psk);
  • Hi,

    You can set it in runtime using wifi_credentials_set_personal or wifi_credentials_set_personal_struct(), for example like this:

    static struct wifi_credentials_personal wifi_cred;
    
    void set_wifi_credentials_struct() 
    {
    
    	char *wifi_ssid = "Myssid";
    	char *wifi_password = "Mypassword";
    
    	wifi_cred.header.type = WIFI_SECURITY_TYPE_PSK;
    	memset(wifi_cred.header.bssid, 0, sizeof(wifi_cred.header.bssid));
    	wifi_cred.header.flags = 0;
    	wifi_cred.header.channel = WIFI_CHANNEL_ANY;
    	wifi_cred.header.timeout = 10;
    
    	wifi_cred.header.ssid_len = strlen(wifi_ssid);
    	memcpy(wifi_cred.header.ssid, wifi_ssid, wifi_cred.header.ssid_len);
    	
    	wifi_cred.password_len = strlen(wifi_password);
    	memcpy(wifi_cred.password, wifi_password, wifi_cred.password_len);
    
    	int ret = wifi_credentials_set_personal_struct(&wifi_cred);
    
    	if (ret != 0) {
    		LOG_ERR("Failed to set credentials, err: %d", ret);
    	}
    }

    Best regards,
    Marte

  • Hi,

    The following two parameters are not available in wifi_credentials_personal. Could this cause any issues?
    wifi_cred.header.channel = WIFI_CHANNEL_ANY;
    wifi_cred.header.timeout = 10;
    Also, I added the security configuration below. Could this cause any issues?
    wifi_cred.header.type = CONFIG_STA_KEY_MGMT_WPA2;
  • Hi,

    ChiragBhavsar said:
    The following two parameters are not available in wifi_credentials_personal. Could this cause any issues?

    No, this should not cause any issues. My code was based on v2.7.1, where these parameters are included, but in v2.6.1, you do not need them.

    ChiragBhavsar said:
    Also, I added the security configuration below. Could this cause any issues?

    The security type is a variable of type wifi_security_type, so you need to use one of the elements in that enum.

    /** @brief IEEE 802.11 security types. */
    enum wifi_security_type {
    	/** No security. */
    	WIFI_SECURITY_TYPE_NONE = 0,
    	/** WPA2-PSK security. */
    	WIFI_SECURITY_TYPE_PSK,
    	/** WPA2-PSK-SHA256 security. */
    	WIFI_SECURITY_TYPE_PSK_SHA256,
    	/** WPA3-SAE security. */
    	WIFI_SECURITY_TYPE_SAE,
    	/** GB 15629.11-2003 WAPI security. */
    	WIFI_SECURITY_TYPE_WAPI,
    	/** EAP security - Enterprise. */
    	WIFI_SECURITY_TYPE_EAP,
    	/** WEP security. */
    	WIFI_SECURITY_TYPE_WEP,
    	/** WPA-PSK security. */
    	WIFI_SECURITY_TYPE_WPA_PSK,
    	/** WPA/WPA2/WPA3 PSK security. */
    	WIFI_SECURITY_TYPE_WPA_AUTO_PERSONAL,
    
    /** @cond INTERNAL_HIDDEN */
    	__WIFI_SECURITY_TYPE_AFTER_LAST,
    	WIFI_SECURITY_TYPE_MAX = __WIFI_SECURITY_TYPE_AFTER_LAST - 1,
    	WIFI_SECURITY_TYPE_UNKNOWN
    /** @endcond */
    };

    For WPA2-PSK (CONFIG_STA_KEY_MGMT_WPA2), the correct value is WIFI_SECURITY_TYPE_PSK.

    Best regards,
    Marte

  • I merge the WIFI and TCP/IP code so can you please check this once and please give your valuable feedback.

    /*
     * Copyright (c) 2023 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    #include <errno.h>
    #include <stddef.h>
    #include <string.h>
    #include <stdio.h>
    #include <zephyr/kernel.h>
    #include <zephyr/types.h>
    #include <zephyr/logging/log.h>
    
    #include <dk_buttons_and_leds.h>
    
    /* STEP 3 - Include the necessary header files */
    #include <zephyr/net/wifi.h>
    #include <zephyr/net/wifi_mgmt.h>
    #include <zephyr/net/net_mgmt.h>
    #include <net/wifi_mgmt_ext.h>
    
    /* STEP 12.4 - Include the header file for the Wi-FI credentials library */
    #include <net/wifi_credentials.h>
    #include <zephyr/net/socket.h>
    
    #include <zephyr/net/net_if.h>
    
    #define SERVER_IP_ADDR "192.168.1.10" // Replace with your server's IP
    #define SERVER_PORT 8080
    
    LOG_MODULE_REGISTER(Lesson2_Exercise2, LOG_LEVEL_INF);
    
    /* STEP 4 - Define a macro for the relevant network events */
    #define EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED)
    
    /* STEP 5 - Declare the callback structure for Wi-Fi events */
    static struct net_mgmt_event_callback mgmt_cb;
    
    /* STEP 6.1 - Define the boolean connected and the semaphore run_app */
    static bool connected;
    static K_SEM_DEFINE(run_app, 0, 1);
    
    /* STEP 6.2 - Define the callback function for network events */
    static void net_mgmt_event_handler(struct net_mgmt_event_callback *cb,
    			  uint32_t mgmt_event, struct net_if *iface)
    {
    	if ((mgmt_event & EVENT_MASK) != mgmt_event) {
    		return;
    	}
    	if (mgmt_event == NET_EVENT_L4_CONNECTED) {
    		LOG_INF("Network connected");
    		connected = true;
    		dk_set_led_on(DK_LED1);
    		k_sem_give(&run_app);
    		return;
    	}
    	if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
    		if (connected == false) {
    			LOG_INF("Waiting for network to be connected");
    		} else {
    			dk_set_led_off(DK_LED1);
    			LOG_INF("Network disconnected");
    			connected = false;
    		}
    		k_sem_reset(&run_app);
    		return;
    	}
    }
    
    /* STEP 7 - Define the function to populate the Wi-Fi credential parameters */
    //static int wifi_args_to_params(struct wifi_connect_req_params *params)
    //{
    
    	/* STEP 7.1 Populate the SSID and password */
    	//params->ssid = CONFIG_WIFI_CREDENTIALS_STATIC_SSID;
    	//params->ssid_length = strlen(params->ssid);
    
    	//params->psk = CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD;
    	//params->psk_length = strlen(params->psk);
    
    	/* STEP 7.2 - Populate the rest of the relevant members */
    	//params->channel = WIFI_CHANNEL_ANY;
    	//params->security = WIFI_SECURITY_TYPE_PSK;
    	//params->mfp = WIFI_MFP_OPTIONAL;
    	//params->timeout = SYS_FOREVER_MS;
    	//params->band = WIFI_FREQ_BAND_UNKNOWN;
    	//#if NCS_VERSION_NUMBER > 0x20600
    	//memset(params->bssid, 0, sizeof(params->bssid));
    	//#endif
    	//return 0;
    //}
    
    static struct wifi_credentials_personal wifi_cred;
    void set_wifi_credentials_struct() 
    {
    
    	char *wifi_ssid = "Myssid";
    	char *wifi_password = "Mypassword";
    
    	wifi_cred.header.type = WIFI_SECURITY_TYPE_PSK;
    	memset(wifi_cred.header.bssid, 0, sizeof(wifi_cred.header.bssid));
    	wifi_cred.header.flags = 0;
    	//wifi_cred.header.channel = WIFI_CHANNEL_ANY;
    	//wifi_cred.header.timeout = 10;
    
    	wifi_cred.header.ssid_len = strlen(wifi_ssid);
    	memcpy(wifi_cred.header.ssid, wifi_ssid, wifi_cred.header.ssid_len);
    	
    	wifi_cred.password_len = strlen(wifi_password);
    	memcpy(wifi_cred.password, wifi_password, wifi_cred.password_len);
    
    	int ret = wifi_credentials_set_personal_struct(&wifi_cred);
    
    	if (ret != 0) {
    		LOG_ERR("Failed to set credentials, err: %d", ret);
    	}
    }
    
    int main(void)
    {
    	/* STEP 8.1 - Declare the variable for the network configuration parameters */
    	//struct wifi_connect_req_params cnx_params;
    
    
    
    	/* STEP 8.2 - Get the network interface */
    	struct net_if *iface = net_if_get_first_wifi();
    	if (iface == NULL) {
    		LOG_ERR("Returned network interface is NULL");
    		return -1;
    	}
    
    	if (dk_leds_init() != 0) {
    		LOG_ERR("Failed to initialize the LED library");
    	}
    
    	/* Sleep to allow initialization of Wi-Fi driver */
    	k_sleep(K_SECONDS(1));
    
    	/* STEP 9 - Initialize and add the callback function for network events */
    	net_mgmt_init_event_callback(&mgmt_cb, net_mgmt_event_handler, EVENT_MASK);
    	net_mgmt_add_event_callback(&mgmt_cb);
    
    	/* STEP 10 - Populate cnx_params with the network configuration */
    	//wifi_args_to_params(&cnx_params);
    
        set_wifi_credentials_struct();
    	/* STEP 11 - Call net_mgmt() to request the Wi-Fi connection */
    	LOG_INF("Connecting to Wi-Fi");
    	int err = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &wifi_cred, sizeof(struct wifi_credentials_personal));
    	if (err) {
    		LOG_ERR("Connecting to Wi-Fi failed, err: %d", err);
    		return ENOEXEC;
    	}
    
    	k_sem_take(&run_app, K_FOREVER);
    
    	int sock;
        struct sockaddr_in server_addr;
    	char buffer[128];
    
    	// Create a TCP socket
    	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (sock < 0) {
    	printk("Socket creation failed\n");
    	return;
    	}
    
    	// Configure server address
    	server_addr.sin_family = AF_INET;
    	server_addr.sin_port = htons(SERVER_PORT);
    	inet_pton(AF_INET, SERVER_IP_ADDR, &server_addr.sin_addr);
    
    	// Connect to the server
    	if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
    	printk("Connection failed\n");
    	close(sock);
    	return;
    	}
    
    	// Send data
    	const char *msg = "Hello, Server!";
    	send(sock, msg, strlen(msg), 0);
    
    	// Receive data
    	recv(sock, buffer, sizeof(buffer), 0);
    	printk("Received: %s\n", buffer);
    
    	// Close the socket
    	close(sock);
    
    		return 0;
    }

Reply
  • I merge the WIFI and TCP/IP code so can you please check this once and please give your valuable feedback.

    /*
     * Copyright (c) 2023 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    #include <errno.h>
    #include <stddef.h>
    #include <string.h>
    #include <stdio.h>
    #include <zephyr/kernel.h>
    #include <zephyr/types.h>
    #include <zephyr/logging/log.h>
    
    #include <dk_buttons_and_leds.h>
    
    /* STEP 3 - Include the necessary header files */
    #include <zephyr/net/wifi.h>
    #include <zephyr/net/wifi_mgmt.h>
    #include <zephyr/net/net_mgmt.h>
    #include <net/wifi_mgmt_ext.h>
    
    /* STEP 12.4 - Include the header file for the Wi-FI credentials library */
    #include <net/wifi_credentials.h>
    #include <zephyr/net/socket.h>
    
    #include <zephyr/net/net_if.h>
    
    #define SERVER_IP_ADDR "192.168.1.10" // Replace with your server's IP
    #define SERVER_PORT 8080
    
    LOG_MODULE_REGISTER(Lesson2_Exercise2, LOG_LEVEL_INF);
    
    /* STEP 4 - Define a macro for the relevant network events */
    #define EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED)
    
    /* STEP 5 - Declare the callback structure for Wi-Fi events */
    static struct net_mgmt_event_callback mgmt_cb;
    
    /* STEP 6.1 - Define the boolean connected and the semaphore run_app */
    static bool connected;
    static K_SEM_DEFINE(run_app, 0, 1);
    
    /* STEP 6.2 - Define the callback function for network events */
    static void net_mgmt_event_handler(struct net_mgmt_event_callback *cb,
    			  uint32_t mgmt_event, struct net_if *iface)
    {
    	if ((mgmt_event & EVENT_MASK) != mgmt_event) {
    		return;
    	}
    	if (mgmt_event == NET_EVENT_L4_CONNECTED) {
    		LOG_INF("Network connected");
    		connected = true;
    		dk_set_led_on(DK_LED1);
    		k_sem_give(&run_app);
    		return;
    	}
    	if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
    		if (connected == false) {
    			LOG_INF("Waiting for network to be connected");
    		} else {
    			dk_set_led_off(DK_LED1);
    			LOG_INF("Network disconnected");
    			connected = false;
    		}
    		k_sem_reset(&run_app);
    		return;
    	}
    }
    
    /* STEP 7 - Define the function to populate the Wi-Fi credential parameters */
    //static int wifi_args_to_params(struct wifi_connect_req_params *params)
    //{
    
    	/* STEP 7.1 Populate the SSID and password */
    	//params->ssid = CONFIG_WIFI_CREDENTIALS_STATIC_SSID;
    	//params->ssid_length = strlen(params->ssid);
    
    	//params->psk = CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD;
    	//params->psk_length = strlen(params->psk);
    
    	/* STEP 7.2 - Populate the rest of the relevant members */
    	//params->channel = WIFI_CHANNEL_ANY;
    	//params->security = WIFI_SECURITY_TYPE_PSK;
    	//params->mfp = WIFI_MFP_OPTIONAL;
    	//params->timeout = SYS_FOREVER_MS;
    	//params->band = WIFI_FREQ_BAND_UNKNOWN;
    	//#if NCS_VERSION_NUMBER > 0x20600
    	//memset(params->bssid, 0, sizeof(params->bssid));
    	//#endif
    	//return 0;
    //}
    
    static struct wifi_credentials_personal wifi_cred;
    void set_wifi_credentials_struct() 
    {
    
    	char *wifi_ssid = "Myssid";
    	char *wifi_password = "Mypassword";
    
    	wifi_cred.header.type = WIFI_SECURITY_TYPE_PSK;
    	memset(wifi_cred.header.bssid, 0, sizeof(wifi_cred.header.bssid));
    	wifi_cred.header.flags = 0;
    	//wifi_cred.header.channel = WIFI_CHANNEL_ANY;
    	//wifi_cred.header.timeout = 10;
    
    	wifi_cred.header.ssid_len = strlen(wifi_ssid);
    	memcpy(wifi_cred.header.ssid, wifi_ssid, wifi_cred.header.ssid_len);
    	
    	wifi_cred.password_len = strlen(wifi_password);
    	memcpy(wifi_cred.password, wifi_password, wifi_cred.password_len);
    
    	int ret = wifi_credentials_set_personal_struct(&wifi_cred);
    
    	if (ret != 0) {
    		LOG_ERR("Failed to set credentials, err: %d", ret);
    	}
    }
    
    int main(void)
    {
    	/* STEP 8.1 - Declare the variable for the network configuration parameters */
    	//struct wifi_connect_req_params cnx_params;
    
    
    
    	/* STEP 8.2 - Get the network interface */
    	struct net_if *iface = net_if_get_first_wifi();
    	if (iface == NULL) {
    		LOG_ERR("Returned network interface is NULL");
    		return -1;
    	}
    
    	if (dk_leds_init() != 0) {
    		LOG_ERR("Failed to initialize the LED library");
    	}
    
    	/* Sleep to allow initialization of Wi-Fi driver */
    	k_sleep(K_SECONDS(1));
    
    	/* STEP 9 - Initialize and add the callback function for network events */
    	net_mgmt_init_event_callback(&mgmt_cb, net_mgmt_event_handler, EVENT_MASK);
    	net_mgmt_add_event_callback(&mgmt_cb);
    
    	/* STEP 10 - Populate cnx_params with the network configuration */
    	//wifi_args_to_params(&cnx_params);
    
        set_wifi_credentials_struct();
    	/* STEP 11 - Call net_mgmt() to request the Wi-Fi connection */
    	LOG_INF("Connecting to Wi-Fi");
    	int err = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &wifi_cred, sizeof(struct wifi_credentials_personal));
    	if (err) {
    		LOG_ERR("Connecting to Wi-Fi failed, err: %d", err);
    		return ENOEXEC;
    	}
    
    	k_sem_take(&run_app, K_FOREVER);
    
    	int sock;
        struct sockaddr_in server_addr;
    	char buffer[128];
    
    	// Create a TCP socket
    	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (sock < 0) {
    	printk("Socket creation failed\n");
    	return;
    	}
    
    	// Configure server address
    	server_addr.sin_family = AF_INET;
    	server_addr.sin_port = htons(SERVER_PORT);
    	inet_pton(AF_INET, SERVER_IP_ADDR, &server_addr.sin_addr);
    
    	// Connect to the server
    	if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
    	printk("Connection failed\n");
    	close(sock);
    	return;
    	}
    
    	// Send data
    	const char *msg = "Hello, Server!";
    	send(sock, msg, strlen(msg), 0);
    
    	// Receive data
    	recv(sock, buffer, sizeof(buffer), 0);
    	printk("Received: %s\n", buffer);
    
    	// Close the socket
    	close(sock);
    
    		return 0;
    }

Children
Related