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'

  • 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;
    }

  • Hi,

    When storing the network credentials using wifi_credentials_set_personal_struct(), you need to use NET_REQUEST_WIFI_CONNECT_STORED when connecting to the network, and not NET_REQUEST_WIFI_CONNECT:

    net_mgmt(NET_REQUEST_WIFI_CONNECT_STORED, iface, NULL, 0);

    Other than that, the code should work. Please try testing it after changing to NET_REQUEST_WIFI_CONNECT_STORED.

    Best regards,
    Marte

Related