nRF7002_nrf5340DK : mqtt_connect() error : -22 It seems to be a problem with the zsock_connect() function, wifi DNS, or a ca-cert problem, but I can't figure it out clearly.

Hello.
We are developing it thanks to Nordic's friendly guide. GrinningGrinning
We are working on linking with Azure IoT hub with nrf7002_nrf5340dk board.

Nordic kindly provided the azure iot hub sample during the integration work.

I tracked down the error occurring in the zscok_connect part, but it is difficult to determine the exact cause thereafter.


Let’s look at the log I printed.

*** Booting nRF Connect SDK v2.5.0 ***

OK


OK


OK


OK


OK


OK


[00:00:00.014,801] <inf> spi_nor: mx25r6435f@0: 8 MiBy flash

[00:00:00.223,236] <inf> fs_nvs: 2 Sectors of 4096 bytes

[00:00:00.223,236] <inf> fs_nvs: alloc wra: 0, fe8

[00:00:00.223,236] <inf> fs_nvs: data wra: 0, 0

*** Booting nRF Connect SDK v2.5.0 ***

[00:00:00.223,663] <inf> azure_iot_hub_sample: Azure IoT Hub sample started

[00:00:00.223,663] <inf> azure_iot_hub_sample: Bringing network interface up and connecting to the network

[00:00:01.598,175] <inf> azure_iot_hub_sample: Device ID: innopia-device-1

[00:00:01.598,205] <inf> azure_iot_hub_sample: Host name: InnopiaNordic.azure-devices.net

[00:00:01.598,236] <inf> azure_iot_hub_sample: innopia : work_init() IN

[00:00:01.598,266] <inf> azure_iot_hub_sample: innopia : work_init() OUT

[00:00:01.598,297] <inf> azure_iot_hub_sample: innopia : cJSON_Init() IN

[00:00:01.598,297] <inf> azure_iot_hub_sample: innopia : cJSON_Init() OUT

[00:00:06.911,987] <inf> net_dhcpv4: Received: 192.168.45.131

[00:00:06.912,261] <inf> azure_iot_hub_sample: Network connectivity established and IP address assigned

[00:00:06.912,872] <inf> azure_iot_hub_sample: Connected to network

[00:00:06.912,872] <inf> azure_iot_hub: innopia : azure_iot_hub_init() IN

[00:00:06.913,635] <inf> azure_fota: Current firmware version: 0.0.0-dev

[00:00:06.913,635] <dbg> azure_iot_hub: azure_iot_hub_init: Azure FOTA initialized

[00:00:06.913,665] <inf> azure_iot_hub: azure_iot_hub_init() err : Failed to initialize Azure FOTA, error

[00:00:06.913,696] <dbg> azure_iot_hub: iot_hub_state_set: State transition: STATE_UNINIT --> STATE_DISCONNECTED

[00:00:06.913,696] <inf> azure_iot_hub_sample: Azure IoT Hub library initialized

[00:00:06.913,726] <dbg> azure_iot_hub: azure_iot_hub_connect: innopia : azure_iot_hub_connect() IN

[00:00:06.913,787] <dbg> azure_iot_hub: iot_hub_state_set: State transition: STATE_DISCONNECTED --> STATE_CONNECTING

[00:00:06.913,787] <inf> azure_iot_hub_sample: AZURE_IOT_HUB_EVT_CONNECTING

[00:00:06.913,787] <inf> azure_iot_hub: innopia : az_iot_hub_client_init() IN

[00:00:06.913,879] <dbg> azure_iot_hub: azure_iot_hub_connect: User name: InnopiaNordic.azure-devices.net/innopia-device-1/?api-version=2020-09-30&DeviceClientType=azsdk-c%2F1.4.0-beta.2

[00:00:06.913,879] <dbg> azure_iot_hub: azure_iot_hub_connect: User name buffer size is 160, actual user name size is: 112

[00:00:06.913,909] <inf> azure_iot_hub: User name: InnopiaNordic.azure-devices.net/innopia-device-1/?api-version=2020-09-30&DeviceClientType=azsdk-c%2F1.4.0-beta.2

[00:00:06.913,940] <inf> azure_iot_hub: innopia : User name buffer size is 160, actual user name size is: 112

[00:00:06.913,940] <inf> azure_iot_hub: innopia : mqtt_helper_connect() IN

[00:00:06.913,940] <inf> mqtt_helper: innopia : client_connect() IN

[00:00:06.913,970] <inf> net_mqtt: innopia : mqtt_client_init() IN

[00:00:06.914,001] <inf> mqtt_helper: innopia : mqtt_client_init() OUT

[00:00:06.914,001] <inf> mqtt_helper: innopia : ==================



[00:00:06.914,001] <inf> mqtt_helper: innopia : ==================



[00:00:06.914,001] <inf> mqtt_helper: innopia : broker_init() IN

[00:00:06.914,062] <inf> mqtt_helper: Resolving IP address for InnopiaNordic.azure-devices.net

[00:00:06.914,062] <inf> mqtt_helper: innopia : getaddrinfo() IN 

[00:00:08.446,624] <inf> mqtt_helper: innopia : getaddrinfo() OUT

[00:00:08.446,624] <inf> mqtt_helper: innopia : AF_INET IPv4 IP Address used!!!

[00:00:08.446,655] <inf> mqtt_helper: innopia : inet_ntop() IN

[00:00:08.446,716] <inf> mqtt_helper: IPv4 Address found 20.189.109.193 (AF_INET) port num = 536904494

[00:00:08.446,716] <inf> mqtt_helper: innopia : inet_ntop() OUT

[00:00:08.446,746] <inf> mqtt_helper: innopia : broker_init() OUT

[00:00:08.446,746] <inf> mqtt_helper: innopia : ==================



[00:00:08.446,746] <inf> mqtt_helper: innopia : certificates_provision() IN

[00:00:08.446,777] <inf> mqtt_helper: innopia : ca : 1205 private : 1705 / device : 1311 

[00:00:08.446,777] <inf> mqtt_helper: innopia : ca_cert.pem file check ...

[00:00:08.446,807] <inf> mqtt_helper: innopia : tls_credential_add Return = 0 (-17 : File exists  ,  -12 : Not enough space , )

[00:00:08.446,807] <inf> mqtt_helper: innopia : tls_credential_add return 0 => TLS credential successfully added.

[00:00:08.446,838] <inf> net_mqtt: innopia : mqtt_connect() IN

[00:00:08.446,838] <inf> net_mqtt: innopia : client_connect() IN

[00:00:08.446,868] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : ============================

[00:00:08.446,868] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : mqtt_client_tls_connect() IN

[00:00:08.446,868] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : ============================

[00:00:08.446,929] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : 
                                                                               ******************  broker = 34.179.20.189 

[00:00:08.446,929] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : zsock_socket() IN

[00:00:08.447,540] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : zsock_socket() OUT

[00:00:08.447,570] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: (0x20004f08): Created socket 9

[00:00:08.447,570] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : zsock_setsockopt() 1 IN

[00:00:08.447,601] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : zsock_setsockopt() 1 PASS

[00:00:08.447,601] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : zsock_setsockopt() 3 IN

[00:00:08.447,631] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : zsock_setsockopt() 3 PASS

[00:00:08.447,631] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : zsock_setsockopt() 4 IN

[00:00:08.447,662] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : zsock_setsockopt() 4 PASS

[00:00:08.447,692] <dbg> net_mqtt_sock_tls: mqtt_client_tls_connect: innopia : tls_config->cert_nocopy = 0

[00:00:08.447,692] <inf> net_mqtt_sock_tls: innopia : zsock_connect() IN

[00:00:08.447,692] <inf> net_mqtt_sock_tls: innopia : client->transport.tls.sock = 9

[00:00:08.447,753] <inf> net_mqtt_sock_tls: innopia : client->broker : address = 34.179.20.189

[00:00:08.447,753] <inf> net_mqtt_sock_tls: innopia : peer_addr_size = 8

[00:00:08.447,784] <inf> net_mqtt_sock_tls: innopia : zsock_connect() error! ====================== -1

[00:00:08.448,059] <inf> net_mqtt_sock_tls: innopia : Error --> Socket Close

[00:00:08.448,059] <inf> net_mqtt: innopia : mqtt_transport_connect error : -22

[00:00:08.448,089] <err> mqtt_helper: mqtt_connect, error: -22

[00:00:08.448,089] <err> azure_iot_hub: mqtt_helper_connect failed, error: -22

[00:00:08.448,089] <inf> azure_iot_hub: mqtt_helper_connect() error

[00:00:08.448,150] <dbg> azure_iot_hub: iot_hub_state_set: State transition: STATE_CONNECTING --> STATE_DISCONNECTED

[00:00:08.448,150] <err> azure_iot_hub_sample: azure_iot_hub_connect failed: -22
There is no need to read the entire log. There are a few things I would like to ask.

1. The nrf7002 board is connected via wifi, and the wifi is using an IP assigned as ipv4 from the DNS server. Is error -22 a problem with the wifi connection?

2. Can the ca-cert.pem file be the same as the .pem file uploaded to the certificate on Azure IoT Hub?

3. I am curious if there is a separate environment suitable for the nrf7002 board.

There are no major modifications, so I will upload the config.
If there is anything else you would like to check, please leave a reply!  Joy

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

# General config
CONFIG_REBOOT=y

# Logging
CONFIG_LOG=y
CONFIG_LOG_MODE_DEFERRED=y

# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=y
CONFIG_NET_IPV4=y
CONFIG_NET_CONNECTION_MANAGER=y

# Azure IoT Hub library
CONFIG_AZURE_IOT_HUB=y
CONFIG_AZURE_IOT_HUB_DEVICE_ID="innopia-device-1"
# Host name must be configured if DPS is not used
# CONFIG_AZURE_IOT_HUB_HOSTNAME="innopia-device-1.eastasia-1.ts.eventgrid.azure.net"
CONFIG_AZURE_IOT_HUB_HOSTNAME="InnopiaNordic.azure-devices.net"

# Change the security tag to the tag where Azure IoT Hub certificates
# are provisioned
CONFIG_MQTT_HELPER_SEC_TAG=10
CONFIG_MQTT_HELPER_SECONDARY_SEC_TAG=11
CONFIG_MQTT_HELPER_STACK_SIZE=8192
# CONFIG_MQTT_HELPER_PORT=

# Uncomment to get more verbose logging when debugging
CONFIG_AZURE_IOT_HUB_LOG_LEVEL_DBG=y

# MQTT - Maximum MQTT keepalive timeout specified by Azure IoT Hub
# CONFIG_MQTT_KEEPALIVE=1767 
CONFIG_MQTT_KEEPALIVE=300

# cJSON
CONFIG_CJSON_LIB=y

# Settings, needed for Azure Device Provisioning Service
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_FCB=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_FCB=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y

# LOG
CONFIG_NET_LOG=y
CONFIG_MQTT_LOG_LEVEL_DBG=y

# Device_id (HW id 사용?)
CONFIG_AZURE_IOT_HUB_SAMPLE_DEVICE_ID_USE_HW_ID=n
# hw info enable?
# CONFIG_HWINFO=y

# open thread enabel?
# CONFIG_OPENTHREAD_NRF_SECURITY=y
# CONFIG_OPENTHREAD_NRF_SECURITY_CHOICE=y
# CONFIG_OPENTHREAD_SECURITY=y
# CONFIG_CUSTOM_OPENTHREAD_SECURITY=y


nrf7002dk_nrf5340_cpuapp.conf

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

# General
CONFIG_POSIX_CLOCK=y
CONFIG_HW_STACK_PROTECTION=y
CONFIG_HW_ID_LIBRARY=y
CONFIG_HW_ID_LIBRARY_SOURCE_NET_MAC=y
CONFIG_DK_LIBRARY=y

# Heap and stacks
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=153600
CONFIG_NET_RX_STACK_SIZE=2048

# Set newlib C to prevent build error complaining that string.h is not supported
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

# Wi-Fi
CONFIG_WIFI=y
CONFIG_WIFI_NRF700X=y
CONFIG_WIFI_LOG_LEVEL_ERR=y
CONFIG_WIFI_MGMT_EXT=y
CONFIG_WIFI_CREDENTIALS=y
CONFIG_WIFI_CREDENTIALS_BACKEND_SETTINGS=y
CONFIG_WIFI_CREDENTIALS_STATIC=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_NVS=y

# WPA
CONFIG_WPA_SUPP=y
CONFIG_WPA_SUPP_LOG_LEVEL_ERR=y

# Zephyr NET Connection Manager connectivity layer
CONFIG_L2_WIFI_CONNECTIVITY=y
CONFIG_L2_WIFI_CONNECTIVITY_AUTO_DOWN=n

# DNS
CONFIG_DNS_RESOLVER=y

# Wi-Fi
CONFIG_WIFI_CREDENTIALS_STATIC_SSID="SK_WiFiblahblah"
CONFIG_WIFI_CREDENTIALS_STATIC_PASSWORD="passwordblahblah"

# NET sockets
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_IPV4=y
CONFIG_NET_TCP=y
CONFIG_NET_TCP_WORKQ_STACK_SIZE=2048
CONFIG_NET_UDP=y
CONFIG_NET_SOCKETS_OFFLOAD=n
CONFIG_NET_NATIVE=y
CONFIG_NET_DHCPV4=y
CONFIG_NET_CONTEXT_SNDTIMEO=y
CONFIG_NET_CONTEXT_RCVTIMEO=y
CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096

# TLS networking
CONFIG_POSIX_MAX_FDS=14
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
CONFIG_NET_SOCKETS_ENABLE_DTLS=n
CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=2

# nRF Security
CONFIG_NRF_SECURITY=y
CONFIG_PSA_CRYPTO_DRIVER_OBERON=n
CONFIG_CC3XX_BACKEND=y

# mbedTLS
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=120000
CONFIG_MBEDTLS_RSA_C=y

# FOTA
CONFIG_AZURE_FOTA=y
CONFIG_AZURE_FOTA_APP_VERSION_AUTO=n
CONFIG_AZURE_FOTA_TLS=y
# Change the security tag to the tag where the certificates are provisioned
# for the server where the FOTA image is hosted
CONFIG_AZURE_FOTA_SEC_TAG=11
CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_FOTA_DOWNLOAD=y
CONFIG_DFU_TARGET=y
CONFIG_MCUBOOT_IMG_MANAGER=y
CONFIG_IMG_MANAGER=y
CONFIG_STREAM_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_FLASH=y
CONFIG_IMG_ERASE_PROGRESSIVELY=y
CONFIG_DOWNLOAD_CLIENT=y
CONFIG_DOWNLOAD_CLIENT_STACK_SIZE=4096

# Enable external flash to host MCUBoot secondary partition
CONFIG_SPI=y
CONFIG_SPI_NOR=y
CONFIG_SPI_NOR_SFDP_DEVICETREE=y
CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y


overlay-dps.conf
#
# Copyright (c) 2022 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

CONFIG_AZURE_IOT_HUB_DPS=n

# The ID scope can be omitted and provided at run time
CONFIG_AZURE_IOT_HUB_DPS_ID_SCOPE=""


Related