This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

MQTTS connection speed that does not require any certificate is very slow.

I verify the TLS connection without a certificate in the matt_samples routine. I found that the connection to the server is very slow, it takes more than 20 seconds, how can I solve this problem?

I use NBIOT and ncs1.5.0.

Here is my mqtt_init:

static int client_init(struct mqtt_client *client)
{
	int err;

	mqtt_client_init(client);

	err = broker_init();
	if (err) {
		LOG_ERR("Failed to initialize broker connection");
		return err;
	}

	/* MQTT client configuration */
	client->broker = &broker;
	client->evt_cb = mqtt_evt_handler;
	client->client_id.utf8 = (uint8_t *)CONFIG_MQTT_CLIENT_ID;
	client->client_id.size = strlen(CONFIG_MQTT_CLIENT_ID);
	client->password = NULL;
	client->user_name = NULL;
	client->protocol_version = MQTT_VERSION_3_1_1;

	/* MQTT buffers configuration */
	client->rx_buf = rx_buffer;
	client->rx_buf_size = sizeof(rx_buffer);
	client->tx_buf = tx_buffer;
	client->tx_buf_size = sizeof(tx_buffer);

	/* MQTT transport configuration */
	struct mqtt_sec_config *tls_cfg = &(client->transport).tls.config;
	static sec_tag_t sec_tag_list[] = { CONFIG_MQTT_TLS_SEC_TAG };

	LOG_INF("TLS enabled");
	client->transport.type = MQTT_TRANSPORT_SECURE;

	tls_cfg->peer_verify = TLS_PEER_VERIFY_NONE;
	tls_cfg->cipher_count = 0;
	tls_cfg->cipher_list = NULL;
	tls_cfg->sec_tag_count = 0;
	tls_cfg->sec_tag_list = NULL;
	tls_cfg->hostname = CONFIG_MQTT_BROKER_HOSTNAME;

	tls_cfg->session_cache = IS_ENABLED(CONFIG_MQTT_TLS_SESSION_CACHING) ?
					    TLS_SESSION_CACHE_ENABLED :
					    TLS_SESSION_CACHE_DISABLED;

	return err;
}

prj.conf:

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

# Networking
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y

# LTE link control
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_NETWORK_MODE_NBIOT=y
CONFIG_LTE_LEGACY_PCO_MODE=y
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n

# Modem library
CONFIG_NRF_MODEM_LIB=y

# AT Host
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_AT_HOST_LIBRARY=y

# MQTT
CONFIG_MQTT_LIB=y
CONFIG_MQTT_LIB_TLS=y
CONFIG_MQTT_CLEAN_SESSION=y

# Application
CONFIG_MQTT_PUB_TOPIC="my/publish/topic"
CONFIG_MQTT_SUB_TOPIC="my/subscribe/topic"
CONFIG_MQTT_CLIENT_ID="my/test/id"
CONFIG_MQTT_BROKER_HOSTNAME="47.106.164.80"
CONFIG_MQTT_BROKER_PORT=18883

# Button support
CONFIG_DK_LIBRARY=y

# Enable logging
CONFIG_LOG=y
CONFIG_MQTT_SIMPLE_LOG_LEVEL_DBG=y

# Memory
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=2048

Serial log:

Non-secure callable region 0 placed in flash region 0 with size 32.


[17:52:21.884]收←◆SRAM region		Domain		Permissions
00 07 0x00000 0x10000 	Secure		rwxl
08 31 0x10000 0x40000 	Non-Secure	rwxl

Peripheral		Domain		Status
00 NRF_P0               Non-Secure	OK
01 NRF_CLOCK            Non-Secure	OK
02 NRF_RTC0             Non-Secure	OK
03 NRF_RTC1             Non-Secure	OK
04 NRF_NVMC             Non-Secure	OK
05 NRF_UARTE1           Non-Secure	OK
06 NRF_UARTE2           Secure		SKIP
07 NRF_TWIM2            Non-Secure	OK
08 NRF_SPIM3            Non-Secure	OK
09 NRF_TIMER0           Non-Secure	OK
10 NRF_TIMER1           Non-Secure	OK
11 NRF_TIMER2           Non-Secure	OK
12 NRF_SAADC            Non-Secure	OK
13 NRF_PWM0             Non-Secure	OK
14 NRF_PWM1             Non-Secure	OK
15 NRF_PWM2             Non-Secure	OK
16 NRF_PWM3             Non-Secure	OK
17 NRF_WDT              Non-Secure	OK
18 NRF_IPC              Non-Secure	OK
19 NRF_VMC              Non-Secure	OK
20 NRF_FPU              Non-Secure	OK
21 NRF_EGU1             Non-Secure	OK
22 NRF_EGU2             Non-Secure	OK
23 NRF_DPPIC            Non-Secure	OK
24 NRF_REGULATORS       Non-Secure	OK
25 NRF_GPIOTE1          Non-Secure	OK

SPM: NS image at 0xc000
SPM: NS MSP at 0x20019740
SPM: NS reset vector at 0x10021
SPM: prepare to jump to Non-Secure image.

[17:52:22.196]收←◆*** Booting Zephyr OS build v2.4.99-ncs1  ***

[17:52:23.200]收←◆[00:00:00.205,413] <inf> mqtt_simple: The MQTT simple sample started
[00:00:00.205,413] <inf> mqtt_simple: Provisioning certificates
[00:00:00.205,413] <inf> mqtt_simple: Disabling PSM and eDRX
[00:00:00.207,000] <inf> mqtt_simple: LTE Link Connecting...
[00:00:00.216,705] <inf> lte_lc: Using legacy LTE PCO mode...

[17:52:23.946]收←◆+CEREG: 
[17:52:23.978]收←◆2,"1D2C","0D61D950",9,0,0,"11100000","11100000"

[17:52:24.560]收←◆+CSCON: 1

[17:52:26.341]收←◆+CEREG: 1,"1D2C","0D61D950",9,,,"11100000","11100000"

[17:52:26.516]收←◆+CSC
[17:52:26.544]收←◆ON: 0

[17:52:27.346]收←◆[00:00:04.35
[17:52:27.372]收←◆0,799] <inf> mqtt_simple: LTE Link Connected!
[00:00:04.351,196] <inf> mqtt_simple: IPv4 Address found 47.106.164.80
[00:00:04.351,226] <inf> mqtt_simple: TLS enabled

[17:52:29.634]收←◆+CSCON: 1

[17:52:29.955]收←◆+CSCON: 0

[17:52:32.176]收←◆+CSCON: 1

[17:52:34.195]收←◆+CSCON: 0

[17:52:37.291]收←◆+CSCON: 1

[17:52:39.053]收←◆+CSCON: 0

[17:52:42.416]收←◆+CSCON:
[17:52:42.436]收←◆ 1

[17:52:43.329]收←◆+CSCON: 0

[17:52:44.995]收←◆+CSCON: 1

[17:52:46.699]收←◆+CSCON: 0

[17:52:47.531]收←◆+CSCON: 1

[17:52:47.669]收←◆+CSCON: 0

[17:52:54.576]收←◆+CSCON: 1

[17:52:54.754]收←◆+CSCON: 0

[17:52:57.772]收←◆+CSCON: 1

[17:52:58.029]收←◆+CSCON: 0

[17:53:00.354]收←◆+CSCON: 1

[17:53:00.673]收←◆+CSCON: 0

[17:53:02.896]收←◆+CSCON:
[17:53:02.923]收←◆ 1

[17:53:03.164]收←◆+CSCON: 0
[17:53:03.191]收←◆

[17:53:04.045]收←◆[00:00:41.050,811] <inf> mqtt_simple: MQTT client connected
[00:00:41.050,842] <inf> mqtt_simple: Subscribing to: my/subscribe/topic len 18

[17:53:05.474]收←◆+CSCON:
[17:53:05.496]收←◆ 1

[17:53:06.164]收←◆+CSCON: 0

[17:53:06.642]收←◆[00:00:43.647,888] <inf> mqtt_simple: SUBACK packet id: 1234

I use MQTT BOX for connection test, the connection speed is very fast. The following are my configuration parameters. 

   

  • Hello,

    can you set

    CONFIG_NET_LOG=y
    CONFIG_MQTT_LOG_LEVEL_DBG=y

    for more detailed log output?

  • I replaced a 0.9.0 DK for testing and found that the connection speed is normal.

    log:

    [14:38:54.742]收←◆*** Booting Zephyr OS build v2.4.99-ncs1  ***
    
    [14:38:55.752]收←◆[00:00:00.211,486] <inf> mqtt_simple: The MQTT simple sample started
    [00:00:00.211,486] <inf> mqtt_simple: Provisioning certificates
    [00:00:00.211,486] <inf> mqtt_simple: Disabling PSM and eDRX
    [00:00:00.225,280] <inf> mqtt_simple: LTE Link Connecting...
    [00:00:00.235,137] <inf> lte_lc: Using legacy LTE PCO mode...
    
    [14:38:58.079]收←◆+CEREG: 2,"1D2C","0D61D950",9,0,0,"11100000","11100000"
    
    [14:38:58.646]收←◆+CSCON: 1
    
    [14:38:59.826]收←◆+CEREG: 1,"1D2C","0D61D950",9,,,"11100000","11100000"
    
    [14:39:00.832]收←◆[00:00:05.290,588] <inf> mqtt_simple: LTE Link Connected!
    [00:00:05.291,015] <inf> mqtt_simple: IPv4 Address found 47.106.164.80
    [00:00:05.291,015] <inf> mqtt_simple: TLS enabled
    [00:00:05.291,412] <dbg> net_mqtt_sock_tls.mqtt_client_tls_connect: (0x20014ae8): Created socket 1
    
    [14:39:04.680]收←◆[00:00:10
    [14:39:04.710]收←◆.138,397] <dbg> net_mqtt_sock_tls.mqtt_client_tls_connect: (0x20014ae8): Connect completed
    [00:00:10.138,427] <dbg> net_mqtt_enc.connect_request_encode: (0x20014ae8): Encoding Protocol Description. Str:MQTT Size:00000004.
    [00:00:10.138,427] <dbg> net_mqtt_enc.pack_utf8_str: (0x20014ae8): >> str_size:00000006 cur:0x20015e0f,
    [14:39:04.733]收←◆ end:0x20015e8a
    [00:00:10.138,458] <dbg> net_mqtt_enc.pack_uint16: (0x20014ae8): >> val:0004 cur:0x20015e0f, end:0x20015e8a
    [00:00:10.138,458] <dbg> net_mqtt_enc.connect_request_encode: (0x20014ae8): Encoding Protocol Version 04.
    [00:00:10.138,458] <dbg> net_mqtt_enc.pack_uint8: (0x20014ae8): >> val:04 cur:0x20015e15, end:0x20015e8a
    [00:00:10.138,488] <dbg> net_mqtt_enc.pack_uint8: (0x20014ae8): >> val:00 cur:0x20015e16, end:0x20015e8a
    [00:00:10.138,488] <dbg> net_mqtt_enc.connect_request_encode: (0x20014ae8): Encoding Keep Alive Time 003c.
    [00:00:10.138,488] <dbg> net_mqtt_enc.pack_uint16: (0x20014ae8): >> val:003c cur:0x20015e17, end:0x20015e8a
    [00:00:10.138,488] <dbg> net_mqtt_enc.connect_request_encode: (0x20014ae8): Encoding Client Id. Str:my/test/id Size:0000000a.
    [00:00:10.138,519] <dbg> net_mqtt_enc.pack_utf8_str: (0x20014ae8): >> str_size:0000000c cur:0x20015e19, end:0x20015e8a
    [00:00:10.138,519] <dbg> net_mqtt_enc.pack_uint16: (0x20014ae8): >> val:000a cur:0x20015e19, end:0x20015e8a
    [00:00:10.138,549] <dbg> net_mqtt_enc.mqtt_encode_fixed_header: (0x20014ae8): << msg type:0x10 length:0x00000016
    [00:00:10.138,549] <dbg> net_mqtt_enc.packet_length_encode: (0x20014ae8): >> length:0x00000016 cur:(nil), end:(nil)
    [00:00:10.138,549] <dbg> net_mqtt_enc.mqtt_encode_fixed_header: (0x20014ae8): Fixed header length = 02
    [00:00:10.138,580] <dbg> net_mqtt_enc.pack_uint8: (0x20014ae8): >> val:10 cur:0x20015e0d, end:0x20015e8a
    [00:00:10.138,580] <dbg> net_mqtt_enc.packet_length_encode: (0x20014ae8): >> length:0x00000016 cur:0x20015e0e, end:0x20015e8a
    [00:00:10.139,343] <dbg> net_mqtt.client_connect: (0x20014ae8): Connect completed
    [00:00:10.327,514] <dbg> net_mqtt_dec.unpack_uint8: (0x20014ae8): << val:00
    --- 7 messages dropped ---
    [00:00:10.327,514] <dbg> net_mqtt_dec.unpack_uint8: (0x20014ae8): << val:00
    [00:00:10.327,545] <dbg> net_mqtt_dec.connect_ack_decode: (0x20014ae8): [CID 0x20014c98]: session_present_flag: 0
    [00:00:10.327,545] <dbg> net_mqtt_rx.mqtt_handle_packet: (0x20014ae8): [CID 0x20014c98]: return_code: 0
    [00:00:10.327,575] <inf> mqtt_simple: MQTT client connected
    [00:00:10.327,575] <inf> mqtt_simple: Subscribing to: my/subscribe/topic len 18
    [00:00:10.327,575] <dbg> net_mqtt.mqtt_subscribe: (0x20014ae8): [CID 0x20014c98]:[State 0x06]: >> message id 0x04d2 topic count 0x0001
    [00:00:10.327,606] <dbg> net_mqtt_enc.pack_uint16: (0x20014ae8): >> val:04d2 cur:0x20015e0f, end:0x20015e8a
    [00:00:10.327,606] <dbg> net_mqtt_enc.pack_utf8_str: (0x20014ae8): >> str_size:00000014 cur:0x20015e11, end:0x20015e8a
    [00:00:10.327,636] <dbg> net_mqtt_enc.pack_uint16: (0x20014ae8): >> val:0012 cur:0x20015e11, end:0x20015e8a
    [00:00:10.327,636] <dbg> net_mqtt_enc.pack_uint8: (0x20014ae8): >> val:01 cur:0x20015e25, end:0x20015e8a
    [00:00:10.327,636] <dbg> net_mqtt_enc.mqtt_encode_fixed_header: (0x20014ae8): << msg type:0x82 length:0x00000017
    [00:00:10.327,667] <dbg> net_mqtt_enc.packet_length_encode: (0x20014ae8): >> length:0x00000017 cur:(nil), end:(nil)
    [00:00:10.327,667] <dbg> net_mqtt_enc.mqtt_encode_fixed_header: (0x20014ae8): Fixed header length = 02
    [00:00:10.327,697] <dbg> net_mqtt_enc.pack_uint8: (0x20014ae8): >> val:82 cur:0x20015e0d, end:0x20015e8a
    [00:00:10.327,728] <dbg> net_mqtt_enc.packet_length_encode: (0x20014ae8): >> length:0x00000017 cur:0x20015e0e, end:0x20015e8a
    [00:00:10.327,758] <dbg> net_mqtt.client_write: (0x20014ae8): [0x20014c98]: Transport writing 25 bytes.
    [00:00:10.328,491] <dbg> net_mqtt.client_write: (0x20014ae8): [0x20014c98]: Transport write complete.
    [00:00:10.328,521] <dbg> net_mqtt.mqtt_subscribe: (0x20014ae8): [CID 0x20014c98]:[State 0x06]: << result 0x00000000
    [00:00:10.518,341] <dbg> net_mqtt.mqtt_input: (0x20014ae8): state:0x00000006
    [00:00:10.518,402] <dbg> net_mqtt_dec.unpack_uint8: (0x20014ae8): >> cur:0x20015d8a, end:0x20015d8c
    [00:00:10.518,402] <dbg> net_mqtt_dec.unpack_uint8: (0x20014ae8): << val:90
    [00:00:10.518,402] <dbg> net_mqtt_dec.packet_length_decode: (0x20014ae8): length:0x00000003
    [00:00:10.518,463] <dbg> net_mqtt_rx.mqtt_handle_packet: (0x20014ae8): [CID 0x20014c98]: Received MQTT_PKT_TYPE_SUBACK!
    [00:00:10.518,493] <dbg> net_mqtt_dec.unpack_uint16: (0x20014ae8): >> cur:0x20015d8c, end:0x20015d8f
    [00:00:10.518,493] <dbg> net_mqtt_dec.unpack_uint16: (0x20014ae8): << val:04d2
    [00:00:10.518,493] <dbg> net_mqtt_dec.unpack_data: (0x20014ae8): >> cur:0x20015d8e, end:0x20015d8f
    [00:00:10.518,493] <dbg> net_mqtt_dec.unpack_data: (0x20014ae8): << bin len:00000001
    [00:00:10.518,524] <inf> mqtt_simple: SUBACK packet id: 1234

    I will report the problem to you after I catch it.

  • Hi,

    I changed to another 91 chip and burned the same program. -45 error occurred in mqtt connection : Operation not supported on socket. Here is my log:

    [00:00:00.243,316] <inf> main: Connecting to LTE network.
    [00:00:00.243,316] <inf> main: This may take several minutes.
    [00:00:00.243,377] <inf> flash_control: No apn found, use default apn
    [00:00:00.249,908] <inf> main: Set to the default APN CMNBIOT2.
    [00:00:00.258,087] <inf> lte_lc: Using legacy LTE PCO mode...
    
    [18:48:48.897]收←◆[00:00:01.725,158] <inf> mai
    [18:48:48.919]收←◆n: LTE ERROR
    [00:00:02.258,117] <inf> main: CSCON : 1
    
    [18:48:50.833]收←◆[00:00:04.283,386] <inf> main: Connected to LTE network.
    [00:00:04.283,477] <inf> main: LTE SUCCESS
    [00:00:04.286,163] <inf> mqtt_engine: IPv4 Address found 47.106.164.80
    [00:00:04.286,193] <inf> mqtt_engine: TLS enabled
    [00:00:04.286,468] <dbg> net_mqtt_sock_tls.mqtt_client_tls_connect: (0x20015220): Created socket 1
    [00:00:04.660,552] <err> mqtt_engine: mqtt_connect -45
    [00:00:04.660,583] <err> mqtt_engine: ERROR: mqtt_disconnect -57

    -45 is EOPNOTSUPP, and typically indicates that you have written wrong certificates to the device. But I had peer_verify set to 0. 

  • As far as I know you should not attempt to use TLS without a CA certificate on the device, even though you are not using it.

  • I randomly wrote a CA certificate at sec_tag 24.

    The problem is still not solved,Error log:

    Where do I need to write the certificate to ensure normal connection?

Related