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

mqtt_simple example: mqtt_connect error -12

Hello everyone,

I started an evaluation for the Nordic Thingy91 using the "mqtt_simple" example from the SDK1.6.0 version. The Thingy91 is HW-version 1.4.0. Programming is done in SES V5.50c.

The standard-example works (which connects to mqtt.eclipseprojects.io).

I tried to connect to another server without a username and a password and it also worked as intended. I did this by changing the "MQTT_BROKER_HOSTNAME" in Kconfig and the "CONFIG_MQTT_BROKER_HOSTNAME" in prj.conf. I know, only the prj.conf needs to be changed, I changed both to be sure.

Then I changed the host a server which need a specific username or a password. TLS is not used.

I added the username and password in the main.c:

The result is  "mqtt_connect -12":

I found this topic (mqtt connect error - Nordic Q&A - Nordic DevZone - Nordic DevZone (nordicsemi.com))  and increased the buffer size in the prj.conf and Kconfig to 512:

CONFIG_MQTT_MESSAGE_BUFFER_SIZE=512
CONFIG_MQTT_PAYLOAD_BUFFER_SIZE=512

In the mentioned topic, the connect error -12 was solved by increasing buffer sizes. For me, the problem persisted, as shown in the second picture.

What can I do to find the error?

With other MQTT Clients I can connect to the server using the same username and password,

Parents Reply Children
  • 0250.prj.conf

    #
    # Copyright (c) 2018 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    
    cmake_minimum_required(VERSION 3.13.1)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(mqtt-simple)
    
    # NORDIC SDK APP START
    target_sources(app PRIVATE src/main.c)
    # NORDIC SDK APP END
    

    /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    #include <zephyr.h>
    #include <stdio.h>
    #include <drivers/uart.h>
    #include <string.h>
    #include <random/rand32.h>
    #include <net/mqtt.h>
    #include <net/socket.h>
    #include <modem/at_cmd.h>
    #include <modem/lte_lc.h>
    #include <logging/log.h>
    #if defined(CONFIG_MODEM_KEY_MGMT)
    #include <modem/modem_key_mgmt.h>
    #endif
    #if defined(CONFIG_LWM2M_CARRIER)
    #include <lwm2m_carrier.h>
    #endif
    #include <dk_buttons_and_leds.h>
    
    #include "certificates.h"
    
    LOG_MODULE_REGISTER(mqtt_simple, CONFIG_MQTT_SIMPLE_LOG_LEVEL);
    
    /* Buffers for MQTT client. */
    static uint8_t rx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
    static uint8_t tx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
    static uint8_t payload_buf[CONFIG_MQTT_PAYLOAD_BUFFER_SIZE];
    
    /* The mqtt client struct */
    static struct mqtt_client client;
    
    /* MQTT Broker details. */
    static struct sockaddr_storage broker;
    
    /* File descriptor */
    static struct pollfd fds;
    
    #if defined(CONFIG_MQTT_LIB_TLS)
    static int certificates_provision(void)
    {
    	int err = 0;
    
    	LOG_INF("Provisioning certificates");
    
    #if defined(CONFIG_NRF_MODEM_LIB) && defined(CONFIG_MODEM_KEY_MGMT)
    
    	err = modem_key_mgmt_write(CONFIG_MQTT_TLS_SEC_TAG,
    				   MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
    				   CA_CERTIFICATE,
    				   strlen(CA_CERTIFICATE));
    	if (err) {
    		LOG_ERR("Failed to provision CA certificate: %d", err);
    		return err;
    	}
    
    #elif defined(CONFIG_BOARD_QEMU_X86) && defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
    
    	err = tls_credential_add(CONFIG_MQTT_TLS_SEC_TAG,
    				 TLS_CREDENTIAL_CA_CERTIFICATE,
    				 CA_CERTIFICATE,
    				 sizeof(CA_CERTIFICATE));
    	if (err) {
    		LOG_ERR("Failed to register CA certificate: %d", err);
    		return err;
    	}
    
    #endif
    
    	return err;
    }
    #endif /* defined(CONFIG_MQTT_LIB_TLS) */
    
    #if defined(CONFIG_NRF_MODEM_LIB)
    
    /**@brief Recoverable modem library error. */
    void nrf_modem_recoverable_error_handler(uint32_t err)
    {
    	LOG_ERR("Modem library recoverable error: %u", (unsigned int)err);
    }
    
    #endif /* defined(CONFIG_NRF_MODEM_LIB) */
    
    #if defined(CONFIG_LWM2M_CARRIER)
    K_SEM_DEFINE(carrier_registered, 0, 1);
    int lwm2m_carrier_event_handler(const lwm2m_carrier_event_t *event)
    {
    	switch (event->type) {
    	case LWM2M_CARRIER_EVENT_BSDLIB_INIT:
    		LOG_INF("LWM2M_CARRIER_EVENT_BSDLIB_INIT");
    		break;
    	case LWM2M_CARRIER_EVENT_CONNECTING:
    		LOG_INF("LWM2M_CARRIER_EVENT_CONNECTING");
    		break;
    	case LWM2M_CARRIER_EVENT_CONNECTED:
    		LOG_INF("LWM2M_CARRIER_EVENT_CONNECTED");
    		break;
    	case LWM2M_CARRIER_EVENT_DISCONNECTING:
    		LOG_INF("LWM2M_CARRIER_EVENT_DISCONNECTING");
    		break;
    	case LWM2M_CARRIER_EVENT_DISCONNECTED:
    		LOG_INF("LWM2M_CARRIER_EVENT_DISCONNECTED");
    		break;
    	case LWM2M_CARRIER_EVENT_BOOTSTRAPPED:
    		LOG_INF("LWM2M_CARRIER_EVENT_BOOTSTRAPPED");
    		break;
    	case LWM2M_CARRIER_EVENT_REGISTERED:
    		LOG_INF("LWM2M_CARRIER_EVENT_REGISTERED");
    		k_sem_give(&carrier_registered);
    		break;
    	case LWM2M_CARRIER_EVENT_DEFERRED:
    		LOG_INF("LWM2M_CARRIER_EVENT_DEFERRED");
    		break;
    	case LWM2M_CARRIER_EVENT_FOTA_START:
    		LOG_INF("LWM2M_CARRIER_EVENT_FOTA_START");
    		break;
    	case LWM2M_CARRIER_EVENT_REBOOT:
    		LOG_INF("LWM2M_CARRIER_EVENT_REBOOT");
    		break;
    	case LWM2M_CARRIER_EVENT_LTE_READY:
    		LOG_INF("LWM2M_CARRIER_EVENT_LTE_READY");
    		break;
    	case LWM2M_CARRIER_EVENT_ERROR:
    		LOG_ERR("LWM2M_CARRIER_EVENT_ERROR: code %d, value %d",
    			((lwm2m_carrier_event_error_t *)event->data)->code,
    			((lwm2m_carrier_event_error_t *)event->data)->value);
    		break;
    	default:
    		LOG_WRN("Unhandled LWM2M_CARRIER_EVENT: %d", event->type);
    		break;
    	}
    
    	return 0;
    }
    #endif /* defined(CONFIG_LWM2M_CARRIER) */
    
    /**@brief Function to print strings without null-termination
     */
    static void data_print(uint8_t *prefix, uint8_t *data, size_t len)
    {
    	char buf[len + 1];
    
    	memcpy(buf, data, len);
    	buf[len] = 0;
    	LOG_INF("%s%s", log_strdup(prefix), log_strdup(buf));
    }
    
    /**@brief Function to publish data on the configured topic
     */
    static int data_publish(struct mqtt_client *c, enum mqtt_qos qos,
    	uint8_t *data, size_t len)
    {
    	struct mqtt_publish_param param;
    
    	param.message.topic.qos = qos;
    	param.message.topic.topic.utf8 = CONFIG_MQTT_PUB_TOPIC;
    	param.message.topic.topic.size = strlen(CONFIG_MQTT_PUB_TOPIC);
    	param.message.payload.data = data;
    	param.message.payload.len = len;
    	param.message_id = sys_rand32_get();
    	param.dup_flag = 0;
    	param.retain_flag = 0;
    
    	data_print("Publishing: ", data, len);
    	LOG_INF("to topic: %s len: %u",
    		CONFIG_MQTT_PUB_TOPIC,
    		(unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC));
    
    	return mqtt_publish(c, &param);
    }
    
    /**@brief Function to subscribe to the configured topic
     */
    static int subscribe(void)
    {
    	struct mqtt_topic subscribe_topic = {
    		.topic = {
    			.utf8 = CONFIG_MQTT_SUB_TOPIC,
    			.size = strlen(CONFIG_MQTT_SUB_TOPIC)
    		},
    		.qos = MQTT_QOS_1_AT_LEAST_ONCE
    	};
    
    	const struct mqtt_subscription_list subscription_list = {
    		.list = &subscribe_topic,
    		.list_count = 1,
    		.message_id = 1234
    	};
    
    	LOG_INF("Subscribing to: %s len %u", CONFIG_MQTT_SUB_TOPIC,
    		(unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC));
    
    	return mqtt_subscribe(&client, &subscription_list);
    }
    
    /**@brief Function to read the published payload.
     */
    static int publish_get_payload(struct mqtt_client *c, size_t length)
    {
    	if (length > sizeof(payload_buf)) {
    		return -EMSGSIZE;
    	}
    
    	return mqtt_readall_publish_payload(c, payload_buf, length);
    }
    
    /**@brief MQTT client event handler
     */
    void mqtt_evt_handler(struct mqtt_client *const c,
    		      const struct mqtt_evt *evt)
    {
    	int err;
    
    	switch (evt->type) {
    	case MQTT_EVT_CONNACK:
    		if (evt->result != 0) {
    			LOG_ERR("MQTT connect failed: %d", evt->result);
    			break;
    		}
    
    		LOG_INF("MQTT client connected");
    		subscribe();
    		break;
    
    	case MQTT_EVT_DISCONNECT:
    		LOG_INF("MQTT client disconnected: %d", evt->result);
    		break;
    
    	case MQTT_EVT_PUBLISH: {
    		const struct mqtt_publish_param *p = &evt->param.publish;
    
    		LOG_INF("MQTT PUBLISH result=%d len=%d",
    			evt->result, p->message.payload.len);
    		err = publish_get_payload(c, p->message.payload.len);
    
    		if (p->message.topic.qos == MQTT_QOS_1_AT_LEAST_ONCE) {
    			const struct mqtt_puback_param ack = {
    				.message_id = p->message_id
    			};
    
    			/* Send acknowledgment. */
    			mqtt_publish_qos1_ack(&client, &ack);
    		}
    
    		if (err >= 0) {
    			data_print("Received: ", payload_buf,
    				p->message.payload.len);
    			/* Echo back received data */
    			data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				payload_buf, p->message.payload.len);
    		} else {
    			LOG_ERR("publish_get_payload failed: %d", err);
    			LOG_INF("Disconnecting MQTT client...");
    
    			err = mqtt_disconnect(c);
    			if (err) {
    				LOG_ERR("Could not disconnect: %d", err);
    			}
    		}
    	} break;
    
    	case MQTT_EVT_PUBACK:
    		if (evt->result != 0) {
    			LOG_ERR("MQTT PUBACK error: %d", evt->result);
    			break;
    		}
    
    		LOG_INF("PUBACK packet id: %u", evt->param.puback.message_id);
    		break;
    
    	case MQTT_EVT_SUBACK:
    		if (evt->result != 0) {
    			LOG_ERR("MQTT SUBACK error: %d", evt->result);
    			break;
    		}
    
    		LOG_INF("SUBACK packet id: %u", evt->param.suback.message_id);
    		break;
    
    	case MQTT_EVT_PINGRESP:
    		if (evt->result != 0) {
    			LOG_ERR("MQTT PINGRESP error: %d", evt->result);
    		}
    		break;
    
    	default:
    		LOG_INF("Unhandled MQTT event type: %d", evt->type);
    		break;
    	}
    }
    
    /**@brief Resolves the configured hostname and
     * initializes the MQTT broker structure
     */
    static int broker_init(void)
    {
    	int err;
    	struct addrinfo *result;
    	struct addrinfo *addr;
    	struct addrinfo hints = {
    		.ai_family = AF_INET,
    		.ai_socktype = SOCK_STREAM
    	};
    
    	err = getaddrinfo(CONFIG_MQTT_BROKER_HOSTNAME, NULL, &hints, &result);
    	if (err) {
    		LOG_ERR("getaddrinfo failed: %d", err);
    		return -ECHILD;
    	}
    
    	addr = result;
    
    	/* Look for address of the broker. */
    	while (addr != NULL) {
    		/* IPv4 Address. */
    		if (addr->ai_addrlen == sizeof(struct sockaddr_in)) {
    			struct sockaddr_in *broker4 =
    				((struct sockaddr_in *)&broker);
    			char ipv4_addr[NET_IPV4_ADDR_LEN];
    
    			broker4->sin_addr.s_addr =
    				((struct sockaddr_in *)addr->ai_addr)
    				->sin_addr.s_addr;
    			broker4->sin_family = AF_INET;
    			broker4->sin_port = htons(CONFIG_MQTT_BROKER_PORT);
    
    			inet_ntop(AF_INET, &broker4->sin_addr.s_addr,
    				  ipv4_addr, sizeof(ipv4_addr));
    			LOG_INF("IPv4 Address found %s", log_strdup(ipv4_addr));
    
    			break;
    		} else {
    			LOG_ERR("ai_addrlen = %u should be %u or %u",
    				(unsigned int)addr->ai_addrlen,
    				(unsigned int)sizeof(struct sockaddr_in),
    				(unsigned int)sizeof(struct sockaddr_in6));
    		}
    
    		addr = addr->ai_next;
    	}
    
    	/* Free the address. */
    	freeaddrinfo(result);
    
    	return err;
    }
    
    #if defined(CONFIG_NRF_MODEM_LIB)
    #define IMEI_LEN 15
    #define CGSN_RESPONSE_LENGTH 19
    #define CLIENT_ID_LEN sizeof("nrf-") + IMEI_LEN
    #else
    #define RANDOM_LEN 10
    #define CLIENT_ID_LEN sizeof(CONFIG_BOARD) + 1 + RANDOM_LEN
    #endif /* defined(CONFIG_NRF_MODEM_LIB) */
    
    /* Function to get the client id */
    static const uint8_t* client_id_get(void)
    {
    	static uint8_t client_id[MAX(sizeof(CONFIG_MQTT_CLIENT_ID),
    				     CLIENT_ID_LEN)];
    
    	if (strlen(CONFIG_MQTT_CLIENT_ID) > 0) {
    		snprintf(client_id, sizeof(client_id), "%s",
    			 CONFIG_MQTT_CLIENT_ID);
    		goto exit;
    	}
    
    #if defined(CONFIG_NRF_MODEM_LIB)
    	char imei_buf[CGSN_RESPONSE_LENGTH + 1];
    	int err;
    
    	if (!IS_ENABLED(CONFIG_AT_CMD_SYS_INIT)) {
    		err = at_cmd_init();
    		if (err) {
    			LOG_ERR("at_cmd failed to initialize, error: %d", err);
    			goto exit;
    		}
    	}
    
    	err = at_cmd_write("AT+CGSN", imei_buf, sizeof(imei_buf), NULL);
    	if (err) {
    		LOG_ERR("Failed to obtain IMEI, error: %d", err);
    		goto exit;
    	}
    
    	imei_buf[IMEI_LEN] = '\0';
    
    	snprintf(client_id, sizeof(client_id), "nrf-%.*s", IMEI_LEN, imei_buf);
    #else
    	uint32_t id = sys_rand32_get();
    	snprintf(client_id, sizeof(client_id), "%s-%010u", CONFIG_BOARD, id);
    #endif /* !defined(NRF_CLOUD_CLIENT_ID) */
    
    exit:
    	LOG_DBG("client_id = %s", log_strdup(client_id));
    
    	return client_id;
    }
    
    /**@brief Initialize the MQTT client structure
     */
    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 = client_id_get();
    	client->client_id.size = strlen(client->client_id.utf8);
            client->password = "user"; // CONFIG_MQTT_BROKER_USERNAME;
            client->user_name = "password";// CONFIG_MQTT_BROKER_PASSWORD;
    	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 */
    #if defined(CONFIG_MQTT_LIB_TLS)
    	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 = CONFIG_MQTT_TLS_PEER_VERIFY;
    	tls_cfg->cipher_count = 0;
    	tls_cfg->cipher_list = NULL;
    	tls_cfg->sec_tag_count = ARRAY_SIZE(sec_tag_list);
    	tls_cfg->sec_tag_list = sec_tag_list;
    	tls_cfg->hostname = CONFIG_MQTT_BROKER_HOSTNAME;
    
    #if defined(CONFIG_NRF_MODEM_LIB)
    	tls_cfg->session_cache = IS_ENABLED(CONFIG_MQTT_TLS_SESSION_CACHING) ?
    					    TLS_SESSION_CACHE_ENABLED :
    					    TLS_SESSION_CACHE_DISABLED;
    #else
    	/* TLS session caching is not supported by the Zephyr network stack */
    	tls_cfg->session_cache = TLS_SESSION_CACHE_DISABLED;
    
    #endif
    
    #else
    	client->transport.type = MQTT_TRANSPORT_NON_SECURE;
    #endif
    
    	return err;
    }
    
    /**@brief Initialize the file descriptor structure used by poll.
     */
    static int fds_init(struct mqtt_client *c)
    {
    	if (c->transport.type == MQTT_TRANSPORT_NON_SECURE) {
    		fds.fd = c->transport.tcp.sock;
    	} else {
    #if defined(CONFIG_MQTT_LIB_TLS)
    		fds.fd = c->transport.tls.sock;
    #else
    		return -ENOTSUP;
    #endif
    	}
    
    	fds.events = POLLIN;
    
    	return 0;
    }
    
    #if defined(CONFIG_DK_LIBRARY)
    static void button_handler(uint32_t button_states, uint32_t has_changed)
    {
    	if (has_changed & button_states &
    	    BIT(CONFIG_BUTTON_EVENT_BTN_NUM - 1)) {
    		int ret;
    
    		ret = data_publish(&client,
    				   MQTT_QOS_1_AT_LEAST_ONCE,
    				   CONFIG_BUTTON_EVENT_PUBLISH_MSG,
    				   sizeof(CONFIG_BUTTON_EVENT_PUBLISH_MSG)-1);
    		if (ret) {
    			LOG_ERR("Publish failed: %d", ret);
    		}
    	}
    }
    #endif
    
    /**@brief Configures modem to provide LTE link. Blocks until link is
     * successfully established.
     */
    static int modem_configure(void)
    {
    #if defined(CONFIG_LTE_LINK_CONTROL)
    	/* Turn off LTE power saving features for a more responsive demo. Also,
    	 * request power saving features before network registration. Some
    	 * networks rejects timer updates after the device has registered to the
    	 * LTE network.
    	 */
    	LOG_INF("Disabling PSM and eDRX");
    	lte_lc_psm_req(false);
    	lte_lc_edrx_req(false);
    
    	if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
    		/* Do nothing, modem is already turned on
    		 * and connected.
    		 */
    	} else {
    #if defined(CONFIG_LWM2M_CARRIER)
    		/* Wait for the LWM2M_CARRIER to configure the modem and
    		 * start the connection.
    		 */
    		LOG_INF("Waitng for carrier registration...");
    		k_sem_take(&carrier_registered, K_FOREVER);
    		LOG_INF("Registered!");
    #else /* defined(CONFIG_LWM2M_CARRIER) */
    		int err;
    
    		LOG_INF("LTE Link Connecting...");
    		err = lte_lc_init_and_connect();
    		if (err) {
    			LOG_INF("Failed to establish LTE connection: %d", err);
    			return err;
    		}
    		LOG_INF("LTE Link Connected!");
    #endif /* defined(CONFIG_LWM2M_CARRIER) */
    	}
    #endif /* defined(CONFIG_LTE_LINK_CONTROL) */
    
    	return 0;
    }
    
    void main(void)
    {
    	int err;
    	uint32_t connect_attempt = 0;
    
    	LOG_INF("The MQTT simple sample started");
    
    #if defined(CONFIG_MQTT_LIB_TLS)
    	err = certificates_provision();
    	if (err != 0) {
    		LOG_ERR("Failed to provision certificates");
    		return;
    	}
    #endif /* defined(CONFIG_MQTT_LIB_TLS) */
    
    	do {
    		err = modem_configure();
    		if (err) {
    			LOG_INF("Retrying in %d seconds",
    				CONFIG_LTE_CONNECT_RETRY_DELAY_S);
    			k_sleep(K_SECONDS(CONFIG_LTE_CONNECT_RETRY_DELAY_S));
    		}
    	} while (err);
    
    	err = client_init(&client);
    	if (err != 0) {
    		LOG_ERR("client_init: %d", err);
    		return;
    	}
    
    #if defined(CONFIG_DK_LIBRARY)
    	dk_buttons_init(button_handler);
    #endif
    
    do_connect:
    	if (connect_attempt++ > 0) {
    		LOG_INF("Reconnecting in %d seconds...",
    			CONFIG_MQTT_RECONNECT_DELAY_S);
    		k_sleep(K_SECONDS(CONFIG_MQTT_RECONNECT_DELAY_S));
    	}
    	err = mqtt_connect(&client);
    	if (err != 0) {
    		LOG_ERR("mqtt_connect %d", err);
    		goto do_connect;
    	}
    
    	err = fds_init(&client);
    	if (err != 0) {
    		LOG_ERR("fds_init: %d", err);
    		return;
    	}
    
    	while (1) {
    		err = poll(&fds, 1, mqtt_keepalive_time_left(&client));
    		if (err < 0) {
    			LOG_ERR("poll: %d", errno);
    			break;
    		}
    
    		err = mqtt_live(&client);
    		if ((err != 0) && (err != -EAGAIN)) {
    			LOG_ERR("ERROR: mqtt_live: %d", err);
    			break;
    		}
    
    		if ((fds.revents & POLLIN) == POLLIN) {
    			err = mqtt_input(&client);
    			if (err != 0) {
    				LOG_ERR("mqtt_input: %d", err);
    				break;
    			}
    		}
    
    		if ((fds.revents & POLLERR) == POLLERR) {
    			LOG_ERR("POLLERR");
    			break;
    		}
    
    		if ((fds.revents & POLLNVAL) == POLLNVAL) {
    			LOG_ERR("POLLNVAL");
    			break;
    		}
    	}
    
    	LOG_INF("Disconnecting MQTT client...");
    
    	err = mqtt_disconnect(&client);
    	if (err) {
    		LOG_ERR("Could not disconnect MQTT client: %d", err);
    	}
    	goto do_connect;
    }
    

    #
    # Copyright (c) 2020 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    
    menu "MQTT simple sample"
    config MQTT_PUB_TOPIC
    	string "MQTT publish topic"
    	default "chrisiot/publish/topic"
    
    config MQTT_SUB_TOPIC
    	string "MQTT subscribe topic"
    	default "chrisiot/subscribe/topic"
    
    config MQTT_CLIENT_ID
    	string "MQTT Client ID"
    	help
    	  Use a custom Client ID string. If not set, the client ID will be
    	  generated based on IMEI number (for nRF9160 based targets) or
    	  randomly (for other platforms).
    	default "chrisiot_Thingy91_01"
    
    config MQTT_BROKER_HOSTNAME
    	string "MQTT broker hostname"
    	default "xxx.xxx.xxx.xxx"
    
    config MQTT_BROKER_PORT
    	int "MQTT broker port"
    	default 1883
    
    config MQTT_MESSAGE_BUFFER_SIZE
    	int "MQTT message buffer size"
    	default 512
    
    config MQTT_PAYLOAD_BUFFER_SIZE
    	int "MQTT payload buffer size"
    	default 512
    	
    config MQTT_BROKER_USERNAME
    	string "MQTT broker username"
    	default "user"
    
    config MQTT_BROKER_PASSWORD
    	string "MQTT broker password"
    default "password"
    
    config BUTTON_EVENT_PUBLISH_MSG
    	string "The message to publish on a button event"
    	default "Hello from nRF91 MQTT Simple Sample"
    
    config BUTTON_EVENT_BTN_NUM
    	int "The button number"
    	default 1
    
    config MQTT_RECONNECT_DELAY_S
    	int "Seconds to delay before attempting to reconnect to the broker."
    	default 60
    
    config LTE_CONNECT_RETRY_DELAY_S
    	int "Seconds to delay before attempting to retry LTE connection."
    	default 120
    
    config MQTT_TLS_SEC_TAG
    	int "TLS credentials security tag"
    	default 24
    
    config MQTT_TLS_SESSION_CACHING
    	bool "Enable TLS session caching"
    
    config MQTT_TLS_PEER_VERIFY
    	int "Set peer verification level"
    	default 2
    	help
    		Set to 0 for VERIFY_NONE, 1 for VERIFY_OPTIONAL, and 2 for
    		VERIFY_REQUIRED.
    
    endmenu
    
    menu "Zephyr Kernel"
    source "Kconfig.zephyr"
    endmenu
    
    module = MQTT_SIMPLE
    module-str = MQTT Simple
    source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"
    

    Dear Øyvind,

    thank you for your answer. Above, I have attached the main.c, the Kconfig (I had to add.txt, so I could upload the file), the prj.conf and CMakeLists.txt.

    Please let me know, if I can provide more information.

    Have a good day,

    Chris

  • Dear Øyvind,

    I would like to add an information to avoid confusion:

    in my uploaded main.c, the user is entered in the password field and the password is entered in the user field. In the main.c that I use for building, the correct information is entered. Nevertheless, the output is the same:



    Have a good day,

    Chris

  • Hi Chris, 

    I've discussed the issue with one of our developers. You need to add the size of each as well:

    client->password->utf8 = "user";
    client->password->size = strlen("user");
    client->user_name->utf8 = "password";
    client->user_name->size = strlen("password");

    Let me know how that works for you. 

    Kind regards,
    Øyvind

  • Dear Øyvind,

    I set the username and password as suggested by you in the main.c. 

    After starting the debug and running the program, the serial console shows:

    After a minute, the debugger stops here:

    When I press "Continue Execution" in SES the first time, the debugger stops at the same code position immediately. When pressing "Continue Execution" again a second time, a minute later, the debugger stops at the same position again. When I continue execution a third time, the debugger stops there again, but the serial console is updated:

    At this point, the error seems to be reproducible.

    I am happy to try out more ideas, just let me know,

    Chris

  • Hey Chris, 

    Sorry, I forgot to clarify that both password and user_name are structs with a pointer and a size  Ref. zephyr\include\net\mqtt.h

    /** @brief Abstracts UTF-8 encoded strings. */
    struct mqtt_utf8 {
    	const uint8_t *utf8;       /**< Pointer to UTF-8 string. */
    	uint32_t size;             /**< Size of UTF string, in bytes. */
    };

    Please retry with the following, tested by our developer and should work with your application:

     

    struct mqtt_utf8 password = {
     .utf8 = "password",
     .size = strlen("password")
     };
     
     struct mqtt_utf8 username = {
     .utf8 = "username",
     .size = strlen("username")
     };
     
     /* MQTT client configuration */
     client->broker = &broker;
     client->evt_cb = mqtt_evt_handler;
     client->client_id.utf8 = client_id_get();
     client->client_id.size = strlen(client->client_id.utf8);
     client->password = &password;
     client->user_name = &username;
     client->protocol_version = MQTT_VERSION_3_1_1;

    Kind regards,
    Øyvind

Related