MQTT sample problem

I' m using custom board of nrf5340 with nrf7002, and the SDK version is ncs v2.5.2.

I've tried to build and run, but the log in RTT viewer is only print the line "Bringing network interface up and connecting to the network", It seems the program is stuck, and it didn't proceed to execute.

  

and I wondered it is because of the MAC address, so I added the "set_mac _address" function into "network.c" and executed it in the function "network_task", but it printed other errors below.

prj.conf

#prj.conf
#
# Copyright (c) 2023 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

# Common configurations for all boards

CONFIG_WIFI=y
CONFIG_WIFI_NRF700X=y
CONFIG_NET_L2_WIFI_MGMT=y
CONFIG_HEAP_MEM_POOL_SIZE=25000

# System settings
CONFIG_NEWLIB_LIBC=y

# Networking
CONFIG_NETWORKING=y
CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_NATIVE=y
CONFIG_NET_OFFLOAD=y

CONFIG_INIT_STACKS=y

# Memories
CONFIG_MAIN_STACK_SIZE=4096

# General
CONFIG_HW_ID_LIBRARY=y
CONFIG_ASSERT=y
CONFIG_WIFI_CREDENTIALS_STATIC_SSID="allen"
CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD="123" 
CONFIG_WIFI_CREDENTIALS_STATIC_TYPE_OPEN=y

CONFIG_MQTT_SAMPLE_TRANSPORT_BROKER_HOSTNAME="test.mosquitto.org"
CONFIG_MQTT_SAMPLE_TRANSPORT_PUBLISH_TOPIC="my/publish/topic"
CONFIG_MQTT_SAMPLE_TRANSPORT_SUBSCRIBE_TOPIC="my/subscribe/topic"

CONFIG_MQTT_SAMPLE_LED=n
CONFIG_DK_LIBRARY=n

# CONFIG_MQTT_SAMPLE_TRIGGER_THREAD_STACK_SIZE=2048
# Logging
CONFIG_LOG=y
# CONFIG_NET_LOG=y
# CONFIG_MQTT_LOG_LEVEL_DBG=y
# Networking
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_NET_IPV4=y
CONFIG_NET_TCP=y
CONFIG_NET_NATIVE=y
CONFIG_NET_CONNECTION_MANAGER=y

# ZBus
CONFIG_ZBUS=y

# Zephyr state framework
CONFIG_SMF=y

# MQTT
CONFIG_MQTT_HELPER=y
CONFIG_MQTT_CLEAN_SESSION=y

#config RTT
CONFIG_LOG=y
CONFIG_LOG_PRINTK=y
# CONFIG_LOG_MODE_DEFERRED=y
CONFIG_SERIAL=n
CONFIG_UART_CONSOLE=n
CONFIG_USE_SEGGER_RTT=y
CONFIG_CONSOLE=y
CONFIG_RTT_CONSOLE=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_LOG_BACKEND_UART=n
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=4096
CONFIG_LOG_BUFFER_SIZE=4096
# Disable internal DCDC
CONFIG_BOARD_ENABLE_DCDC_APP=n
CONFIG_BOARD_ENABLE_DCDC_NET=n
CONFIG_BOARD_ENABLE_DCDC_HV=n

# Use internal capacitor for 32M xtal and set capacitor value to 8.5pF 
# (ranging from 7.0 pF to 20.0 pF in 0.5 pF steps
# can be enabled on pins XC1 and XC2. This option specifies doubled
# capacitance value for the two capacitors. Set it to 14 to get 7.0 pF
# for each capacitor, 15 to get 7.5 pF, and so on.
# )
CONFIG_SOC_HFXO_CAP_INTERNAL=y
CONFIG_SOC_HFXO_CAP_INT_VALUE_X2=17
 
#  Use internal capacitor for 32k xtal and set capacitor value to 7pF 
# (only 4 options of internal capacitor value settings for 32k xtal
#   -INT_6PF
#   -INT_7PF
#   -INT_9PF
#   - USE external capacitor)
CONFIG_SOC_ENABLE_LFXO=y
CONFIG_SOC_LFXO_CAP_INT_7PF=y

Below is the function what I added. And this function is executed normally in the Scan sample.

int8_t wifi_set_mac_address()
{
	struct net_if *iface = net_if_get_default();
	uint8_t mac_addr[6] = {0xF0, 0xCE, 0x36, 0x00, 0x00, 0x4A};

	struct ethernet_req_params eth_param;
	memcpy(eth_param.mac_address.addr, mac_addr, 6);

	// if (net_if_down(iface))
	// {
	// 	printk("<Err> Couldn't shutdown WiFi interface\n");
	// 	return -1;
	// }
	printk("<Inf> Setting MAC address\n");
	if (net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, iface, &eth_param, sizeof(struct ethernet_req_params)))
	{
		printk("<Err> Couldn't set new MAC Address\n");
		return -2;
	}

	int ret = memcmp(net_if_get_link_addr(iface)->addr, mac_addr, sizeof(mac_addr));
	if (ret)
	{
		printk("<Err> Couldn't change MAC address\n");
	}

	if (net_if_up(iface))
	{
		printk("<Err> Couldn't wake up interface\n");
		return -3;
	}

	return 0;
}

How can I run the MQTT sample correctly?

Parents Reply Children
  • Hi,

    I added in the function "network_task()" of "network.c".

    int8_t wifi_set_mac_address()
    {
    	struct net_if *iface = net_if_get_default();
    	uint8_t mac_addr[6] = {0xF0, 0xCE, 0x36, 0x00, 0x00, 0x4A};
    
    	struct ethernet_req_params eth_param;
    	memcpy(eth_param.mac_address.addr, mac_addr, 6);
    
    	// if (net_if_down(iface))
    	// {
    	// 	printk("<Err> Couldn't shutdown WiFi interface\n");
    	// 	return -1;
    	// }
    	printk("<Inf> Setting MAC address\n");
    	if (net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_ADDRESS, iface, &eth_param, sizeof(struct ethernet_req_params)))
    	{
    		printk("<Err> Couldn't set new MAC Address\n");
    		return -2;
    	}
    
    	int ret = memcmp(net_if_get_link_addr(iface)->addr, mac_addr, sizeof(mac_addr));
    	if (ret)
    	{
    		printk("<Err> Couldn't change MAC address\n");
    	}
    
    	if (net_if_up(iface))
    	{
    		printk("<Err> Couldn't wake up interface\n");
    		return -3;
    	}
    
    	return 0;
    }
    
    static void network_task(void)
    {
    	wifi_set_mac_address();// added
    	int err;
    
    	/* Setup handler for Zephyr NET Connection Manager events. */
    	net_mgmt_init_event_callback(&l4_cb, l4_event_handler, L4_EVENT_MASK);
    	net_mgmt_add_event_callback(&l4_cb);
    
    	/* Setup handler for Zephyr NET Connection Manager Connectivity layer. */
    	net_mgmt_init_event_callback(&conn_cb, connectivity_event_handler, CONN_LAYER_EVENT_MASK);
    	net_mgmt_add_event_callback(&conn_cb);
    
    	/* Connecting to the configured connectivity layer.
    	 * Wi-Fi or LTE depending on the board that the sample was built for.
    	 */
    	LOG_INF("Bringing network interface up and connecting to the network");
    
    	err = conn_mgr_all_if_up(true);
    
    	if (err)
    	{
    		LOG_ERR("conn_mgr_all_if_up, error: %d", err);
    		SEND_FATAL_ERROR();
    		return;
    	}
    
    	/* Resend connection status if the sample is built for Native Posix.
    	 * This is necessary because the network interface is automatically brought up
    	 * at SYS_INIT() before main() is called.
    	 * This means that NET_EVENT_L4_CONNECTED fires before the
    	 * appropriate handler l4_event_handler() is registered.
    	 */
    	if (IS_ENABLED(CONFIG_BOARD_NATIVE_POSIX))
    	{
    		// LOG_INF("test5");
    		conn_mgr_mon_resend_status();
    	}
    }

    and also include these libraries to build the set_mac_address functon correctly.

    #include <zephyr/net/net_if.h>
    #include <zephyr/net/ethernet.h>
    #include <zephyr/net/ethernet_mgmt.h>
    #include "net_private.h"
    and there exists a redeclaration error of "enum net_event_ethernet_cmd{}" between "conn_mgr_connectivity.h" and "ethernet_mgmt.h"
    , so I modified the code in "conn_mgr_connectivity.h" to "enum net_event_ethernet_cmd_1{}".
    enum net_event_ethernet_cmd_1 {
    	NET_EVENT_CONN_CMD_IF_TIMEOUT = 1,
    	NET_EVENT_CONN_CMD_IF_FATAL_ERROR,
    };
    After done the steps above, the sample can be built without errors, but when running the program, the error which I've posted occurs.
    Regards,
    Allen
  • Hi Allen,

    I tried to reproduce your error in both NCS v2.5.2 and v2.6.1. I have seen the first log showing only "Bringing network interface up and connecting to the network", but I got various build errors when building with your changes. Apart from the changes you provided, it seems that additional changes are necessary. Could you adapt your project so that it can be built successfully on nrf7002-dk?

    Best regards,
    Dejan

  • Hi, 

    sorry my bad.

    I found that this library "#include "net_private.h" is not needed.

    so only need to add these three libraries

    #include <zephyr/net/net_if.h>
    #include <zephyr/net/ethernet.h>
    #include <zephyr/net/ethernet_mgmt.h>
    I've tried to build on nrf7002DK, it can be built successfully.
    and I've added this line 
    "CONFIG_MQTT_HELPER_LOG_LEVEL_DBG=y" in prj.conf, so now I can see more log message, 
    it showed the message below
    00> [00:00:00.348,693] <inf> network: Bringing network interface up and connecting to the network
    00> [00:00:00.348,815] <dbg> mqtt_helper: mqtt_state_set: State transition: MQTT_STATE_UNINIT --> MQTT_STATE_DISCONNECTED
    00> [00:00:00.349,060] <dbg> mqtt_helper: mqtt_helper_poll_loop: Waiting for connection_poll_sem
    and these errors still exist.
    [00:00:00.483,917] <err> wifi_nrf: nrf_wifi_wpa_supp_event_get_wiphy: Invalid parameters
    
    [00:00:10.474,578] <err> wpa_supp: 'INTERFACE_ADD wlan0  zephyr  zephyr  zephyr        ' command timed out.
    
    [00:00:10.474,670] <err> wpa_supp: Failed to add interface: wlan0
    According to the sample output, next line must be this line "[00:00:04.224,426] <inf> network: Network connectivity established" but it didn't appear,
    so it's stuck in network connection steps, and it's because of the mac address didn't set.(I've tried the WIFI STA sample, I can connect to WIFI only if adding the mac address, if didn't set it, it will be failed.)
    Regards,
    Allen
  • Hi Allen,

    I am still not able to build your project (on nrf7002-dk) with all the changes you previously specified. Can you share your project and provide your build command?

    Best regards,
    Dejan

  • Hi, 

    this is my project(on nrf7002dk_nrf5340)

    mqtt_2.zip

    and build command

    Regards,

    Allen

Related