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, 

    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

  • Hi Allen,

    I have tested your project. I have built the project for nrf7002dk_nrf5340_cpuapp in NCS v2.5.2. To successfully flash it, I needed to make changes to LOG_MODULE_REGISTER and K_THREAD_DEFINE

    LOG_MODULE_REGISTER(network, CONFIG_MQTT_SAMPLE_ERROR_LOG_LEVEL);
    
    K_THREAD_DEFINE(network_task_id,
    				CONFIG_MQTT_SAMPLE_TRIGGER_THREAD_STACK_SIZE,
    				network_task, NULL, NULL, NULL, 3, 0, 0);

    The result is shown below.
    OK
    ors of 4096 bytes
    [00:00:00.526,367] <inf> fs_nvs: alloc wra: 0, fe8
    [00:00:00.526,397] <inf> fs_nvs: data wra: 0, 0
    *** Booting nRF Connect SDK v2.5.2 ***
    <Inf> Setting MAC address
    wake up interface err: -120
    <Err> Couldn't wake up interface
    [00:00:00.529,113] <inf> network: Bringing network interface up and connecting to the network
    [00:00:00.529,235] <dbg> mqtt_helper: mqtt_state_set: State transition: MQTT_STATE_UNINIT --> MQTT_STATE_DISCONNECTED
    [00:00:00.531,494] <dbg> mqtt_helper: mqtt_helper_poll_loop: Waiting for connection_poll_sem
    [00:00:02.456,359] <inf> wifi_mgmt_ext: Connection requested
    OK
    [00:00:33.474,365] <err> wpa_supp: Connection timed out, timeout: 30 seconds
    

    Please note that your board target in NCS v2.6.1 should be nrf7002dk_nrf5340_cpuapp_ns.
    I could not get the same output as you. Is there anything that you have done differently or additionally? 

    Best regards,
    Dejan

  • Hi,

    these two line "CONFIG_MQTT_SAMPLE_NETWORK_LOG_LEVEL

    " and "CONFIG_MQTT_SAMPLE_NETWORK_THREAD_STACK_SIZE

    " is auto generated after building the sample

    The file "nrf7002dk_nrf5340_cpuapp.conf" need to be used, and it is already in the boards folder of the sample project.

    I 'd encountered "LOG_MODULE_REGISTER and K_THREAD_DEFINE" errors when I didn't use "nrf7002dk_nrf5340_cpuapp.conf"

    "Please note that your board target in NCS v2.6.1 should be nrf7002dk_nrf5340_cpuapp_ns."

    I 've only tried NCS v2.5.2 and NCS v2.6.1 on "cpuapp" but I didn't try "cpuapp_ns".

    "I could not get the same output as you. Is there anything that you have done differently or additionally? "

    No, there isn't. But I see there is the line "<Err> Couldn't wake up interface" in your output, so is it mean it still has error when setting mac address?

    Is there any right way to set the mac address except using OTP?

    I 've found the line in "nrf7002dk_nrf5340_cpuapp.conf", is it related to mac address setting?

    Regards,

    Allen

Reply
  • Hi,

    these two line "CONFIG_MQTT_SAMPLE_NETWORK_LOG_LEVEL

    " and "CONFIG_MQTT_SAMPLE_NETWORK_THREAD_STACK_SIZE

    " is auto generated after building the sample

    The file "nrf7002dk_nrf5340_cpuapp.conf" need to be used, and it is already in the boards folder of the sample project.

    I 'd encountered "LOG_MODULE_REGISTER and K_THREAD_DEFINE" errors when I didn't use "nrf7002dk_nrf5340_cpuapp.conf"

    "Please note that your board target in NCS v2.6.1 should be nrf7002dk_nrf5340_cpuapp_ns."

    I 've only tried NCS v2.5.2 and NCS v2.6.1 on "cpuapp" but I didn't try "cpuapp_ns".

    "I could not get the same output as you. Is there anything that you have done differently or additionally? "

    No, there isn't. But I see there is the line "<Err> Couldn't wake up interface" in your output, so is it mean it still has error when setting mac address?

    Is there any right way to set the mac address except using OTP?

    I 've found the line in "nrf7002dk_nrf5340_cpuapp.conf", is it related to mac address setting?

    Regards,

    Allen

Children
No Data
Related