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

    Can you provide a link to the MQTT sample which you used?

    Best regards,
    Dejan

  • Hi Allen,

    allenyang said:
    I've changed my MQTT broker to broker.hivemq.com and include CONFIG_WPA_SUPP_LOG_LEVEL_DBG=y, but it's still the same output.

    I apologize for not providing complete information. I have found out that in order for CONFIG_WPA_SUPP_LOG_LEVEL_DBG=y to become active you need to comment out CONFIG_WPA_SUPP_LOG_LEVEL_ERR=y in your nrf7002dk_nrf5340_cpuapp.conf file. In addition to CONFIG_WPA_SUPP_LOG_LEVEL_DBG=y, please add CONFIG_LOG_MODE_IMMEDIATE=y to your prj.conf and provide the logs in both cases.

    Best regards,
    Dejan

  • Hi,

    Now I can see more log messages after making the changes.

    -

    custom board

    with set mac address function

    00> [00:00:00.444,274] <inf> wifi_nrf: nrf_wifi_fmac_otp_mac_addr_get: MAC addr not programmed in OTP
    00> 
    00> [00:00:00.445,098] <err> wifi_nrf: nrf_wifi_if_start_zep: Invalid MAC address: 00:00:00:00:00:00
    00> 
    00> [00:00:00.451,568] <inf> fs_nvs: 6 Sectors of 4096 bytes
    00> [00:00:00.451,873] <inf> fs_nvs: alloc wra: 0, fe8
    00> [00:00:00.452,178] <inf> fs_nvs: data wra: 0, 0
    00> *** Booting nRF Connect SDK v2.5.2 ***
    00> [00:00:00.452,819] <dbg> wpa_supp: wpa_printf_impl: wpa_supplicant v2.11-devel
    00> [00:00:00.454,315] <inf> wpa_supp: Successfully initialized wpa_supplicant
    00> <Inf> Setting MAC address
    00> [00:00:00.456,542] <dbg> mqtt_helper: mqtt_state_set: State transition: MQTT_STATE_UNINIT --> MQTT_STATE_DISCONNECTED
    00> [00:00:00.457,214] <dbg> mqtt_helper: mqtt_helper_poll_loop: Waiting for connection_poll_sem
    00> [00:00:00.579,345] <dbg> wpa_supp: wpa_printf_impl: Event: -805240828
    00> [00:00:00.579,711] <dbg> wpa_supp: wpa_printf_impl: Adding interface wlan0
    00> 
    00> [00:00:00.580,230] <dbg> wpa_supp: wpa_printf_impl: Calling wpa_cli: interface_add, argc: 5
    00> 
    00> [00:00:00.580,657] <dbg> wpa_supp: wpa_printf_impl: argv[0]: interface_add
    00> 
    00> [00:00:00.581,054] <dbg> wpa_supp: wpa_printf_impl: argv[1]: wlan0
    00> 
    00> [00:00:00.581,420] <dbg> wpa_supp: wpa_printf_impl: argv[2]: zephyr
    00> 
    00> [00:00:00.581,817] <dbg> wpa_supp: wpa_printf_impl: argv[3]: zephyr
    00> 
    00> [00:00:00.582,183] <dbg> wpa_supp: wpa_printf_impl: argv[4]: zephyr
    00> 
    00> [00:00:00.584,014] <dbg> wpa_supp: wpa_printf_impl: RX global ctrl_iface - hexdump_ascii(len=44):
    00> [00:00:00.584,442] <dbg> wpa_supp: _wpa_hexdump_ascii: 
    00>                                    49 4e 54 45 52 46 41 43  45 5f 41 44 44 20 77 6c |INTERFAC E_ADD wl
    00>                                    61 6e 30 09 7a 65 70 68  79 72 09 7a 65 70 68 79 |an0.zeph yr.zephy
    00>                                    72 09 7a 65 70 68 79 72  09 09 09 09             |r.zephyr ....    
    00> [00:00:00.586,822] <dbg> wpa_supp: wpa_printf_impl: CTRL_IFACE GLOBAL INTERFACE_ADD 'wlan0  zephyr  zephyr  zephyr        '
    00> [00:00:00.587,921] <dbg> wpa_supp: wpa_printf_impl: Initializing interface 'wlan0' conf 'zephyr' driver 'zephyr' ctrl_interface 'zephyr' bridge 'N/A'
    00> wake up interface err: 0
    00> [00:00:00.592,895] <dbg> wpa_supp: wpa_printf_impl: Add interface wlan0 to a new radio N/A
    00> [00:00:00.596,588] <err> wifi_nrf: nrf_wifi_wpa_supp_event_get_wiphy: Invalid parameters
    00> 
    00> [00:00:10.583,038] <err> wpa_supp: 'INTERFACE_ADD wlan0  zephyr  zephyr  zephyr        ' command timed out.
    00> 
    00> [00:00:10.583,496] <err> wpa_supp: Failed to add interface: wlan0
    00> [00:00:10.583,892] <inf> network: Bringing network interface up and connecting to the network
    00> [00:00:10.594,787] <dbg> wpa_supp: wpa_printf_impl: l2_packet_init: iface wlan0 ifindex 1
    00> [00:00:10.595,428] <dbg> wpa_supp: wpa_printf_impl: wlan0: Own MAC address: f0:ce:36:00:00:4a
    00> [00:00:10.596,008] <dbg> wpa_supp: wpa_printf_impl: wlan0: RSN: flushing PMKID list in the driver
    00> [00:00:10.596,557] <dbg> wpa_supp: wpa_printf_impl: wlan0: State: DISCONNECTED -> INACTIVE
    00> [00:00:10.598,114] <dbg> wpa_supp: wpa_printf_impl: MBO: Update non-preferred channels, non_pref_chan=N/A
    00> [00:00:10.598,846] <dbg> wpa_supp: wpa_printf_impl: wlan0: Added interface wlan0
    00> [00:00:10.599,395] <dbg> wpa_supp: wpa_printf_impl: wlan0: State: INACTIVE -> DISCONNECTED

    -

    without set mac address function

    00> [00:00:00.447,814] <inf> wifi_nrf: nrf_wifi_fmac_otp_mac_addr_get: MAC addr not programmed in OTP
    00> 
    00> [00:00:00.448,638] <err> wifi_nrf: nrf_wifi_if_start_zep: Invalid MAC address: 00:00:00:00:00:00
    00> 
    00> [00:00:00.455,291] <inf> fs_nvs: 6 Sectors of 4096 bytes
    00> [00:00:00.455,596] <inf> fs_nvs: alloc wra: 0, fe8
    00> [00:00:00.455,871] <inf> fs_nvs: data wra: 0, 0
    00> *** Booting nRF Connect SDK v2.5.2 ***
    00> [00:00:00.456,542] <dbg> wpa_supp: wpa_printf_impl: wpa_supplicant v2.11-devel
    00> [00:00:00.457,855] <inf> wpa_supp: Successfully initialized wpa_supplicant
    00> [00:00:00.459,320] <inf> network: Bringing network interface up and connecting to the network
    00> [00:00:00.459,777] <dbg> mqtt_helper: mqtt_state_set: State transition: MQTT_STATE_UNINIT --> MQTT_STATE_DISCONNECTED
    00> [00:00:00.460,479] <dbg> mqtt_helper: mqtt_helper_poll_loop: Waiting for connection_poll_sem

    -

    nrf7002dk

    00> [00:00:00.519,531] <inf> fs_nvs: 6 Sectors of 4096 bytes
    00> [00:00:00.519,836] <inf> fs_nvs: alloc wra: 0, fe8
    00> [00:00:00.520,141] <inf> fs_nvs: data wra: 0, 0
    00> *** Booting nRF Connect SDK v2.5.2 ***
    00> <Inf> Setting MAC address
    00> wake up interface err: -120
    00> <Err> Couldn't wake up interface
    00> [00:00:00.523,254] <inf> network: Bringing network interface up and connecting to the network
    00> [00:00:00.523,712] <dbg> mqtt_helper: mqtt_state_set: State transition: MQTT_STATE_UNINIT --> MQTT_STATE_DISCONNECTED
    00> [00:00:00.526,306] <dbg> mqtt_helper: mqtt_helper_poll_loop: Waiting for connection_poll_sem
    00> OK
    00> OK
    00> OK
    00> OK
    00> OK
    00> OK
    00> OK
    00> OK
    00> OK
    00> OK
    00> OK
    00> [00:00:02.457,885] <inf> wifi_mgmt_ext: Connection requested
    00> [00:00:33.470,855] <err> wpa_supp: Connection timed out, timeout: 30 seconds
    00> OK

    Regards,

    Allen

  • Hi Allen,

    Timeout error usually indicates misconfiguration. Have you checked that your connection parameters are set correctly?

    I see in your previously provided log that you used password which is less than 8 characters long. Is this password actually used? If not, can you set the password to be of length minimum 8 characters and provide new logs?

    Best regards,
    Dejan

  • Hi,

    That password is not actually be used, I use another password, the length is greater than 8 characters.

    I think the connection parameters are correct, since the application works fine and connected to MQTT broker normally when not adding “set MAC address “ function in nRF7002dk build.

    Regards,

    Allen

  • Hi Allen,

    For testing purposes, you could try to use  CONFIG_WIFI_FIXED_MAC_ADDRESS Kconfig option.

    Best regards,
    Dejan

Reply Children
No Data
Related