Devacademy L4_e2. mqtt_connect error -12 when connecting to HiveMq serverless cluster.

Hi,

I'm half way through the Cellular course on the dev academy and although I am able to connect to the Nordic and Hivemq publich brokers, when I attempt to connect to my serverless Hivemq cluster, I receive the mqtt_connect error 12.

My client_init is as below (username and password removed).

int client_init(struct mqtt_client* client)
{
int err;
/* Initializes the client instance. */
mqtt_client_init(client);

/* Resolves the configured hostname and initializes the MQTT broker structure */
err = broker_init();
if (err) {
LOG_ERR("Failed to initialise broker connection");
return err;
}

char password[] = "xxxxxxxxx";
char username[] = "xxxxxxxxxx";

struct mqtt_utf8 pass = {
.utf8 = (uint8_t*)password,
.size = strlen(password)
};

struct mqtt_utf8 uname = {
.utf8 = (uint8_t*)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 = &pass;
client->user_name = &uname;
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);

/* STEP 5 - Modify the client client_init() function to use Secure TCP transport instead of non-secure TCP transport. */
client->transport.type = MQTT_TRANSPORT_SECURE;

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");
tls_cfg->peer_verify = CONFIG_MQTT_TLS_PEER_VERIFY;
tls_cfg->cipher_list = NULL;
tls_cfg->cipher_count = 0;
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;
tls_cfg->session_cache = IS_ENABLED(CONFIG_MQTT_TLS_SESSION_CACHING) ? TLS_SESSION_CACHE_ENABLED : TLS_SESSION_CACHE_DISABLED;

return err;
}

Project conf is:

# C++
CONFIG_CPP=y
CONFIG_STD_CPP20=y
CONFIG_REQUIRES_FULL_LIBCPP=y

# Logging
CONFIG_LOG=y

# Button and LED support
CONFIG_DK_LIBRARY=y

# Newlib
CONFIG_NEWLIB_LIBC=y

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

# Memory
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=4096

# Modem library
CONFIG_NRF_MODEM_LIB=y
CONFIG_MODEM_KEY_MGMT=y

# LTE link control
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_NETWORK_MODE_LTE_M_NBIOT=y

# MQTT
CONFIG_MQTT_LIB=y
CONFIG_MQTT_CLEAN_SESSION=y
CONFIG_MQTT_LIB_TLS=y

# Application
CONFIG_MQTT_PUB_TOPIC="alcsystems/publish/topic"
CONFIG_MQTT_SUB_TOPIC="alcsystems/subscribe/topic"
CONFIG_BUTTON_EVENT_PUBLISH_MSG="Hi from nRF91 Series device"
# CONFIG_MQTT_BROKER_HOSTNAME="mqtt.nordicsemi.academy"
CONFIG_MQTT_BROKER_HOSTNAME="033cdd1462f949a2b4812c18f4ca9d13.s1.eu.hivemq.cloud"
# STEP 2.2 - Change the MQTT broker port
CONFIG_MQTT_BROKER_PORT=8883
# CONFIG_MQTT_BROKER_PORT=1883

As you can see, I have increased the memory and also increased the buffer sizes to 512.

Any help would be gratefully received.

Thanks,

Andy

Parents
  • -12 error is due to insufficient memory and not any access with username or password.

    Make sure you have given enough memory for all the contexts that need heap memory in your prj.conf something like below. Use this as template and start to reconfigure things up and down in mem size. You might have to do some trial and error if you do not indend to do proper debugging to see where you get this error. If would be helpful to give us full serial log here so that I can attempt to get a hint of the context of the error.

    CONFIG_MAIN_STACK_SIZE=8192 
    CONFIG_HEAP_MEM_POOL_SIZE=16384 
    CONFIG_MBEDTLS_HEAP=y
    CONFIG_MBEDTLS_HEAP_SIZE=16384
    
    #slim down Zephyr net-buffers if still hitting RAM limits
    CONFIG_NET_BUF_RX_COUNT=5
    CONFIG_NET_BUF_TX_COUNT=5

  • Hi Susheel,

    Thanks for the quick response.

    This is the output with the settings as above.   

    *** Using Zephyr OS v4.0.99-77f865b8f8d0 ***
    [00:00:00.365,753] <inf> Lesson4_Exercise2: Initializing modem library
    [00:00:00.710,418] <inf> Lesson4_Exercise2: match

    [00:00:00.710,449] <inf> Lesson4_Exercise2: Connecting to LTE network
    [00:00:01.919,677] <inf> Lesson4_Exercise2: RRC mode: Connected
    [00:00:06.263,183] <inf> Lesson4_Exercise2: Network registration status: Connected - roaming
    [00:00:06.263,275] <inf> Lesson4_Exercise2: Connected to LTE network
    [00:00:06.486,022] <inf> Lesson4_Exercise2: IPv4 Address found 46.137.47.218
    [00:00:06.486,541] <inf> Lesson4_Exercise2: client_id = nrf-359404230139975
    [00:00:06.486,572] <inf> Lesson4_Exercise2: TLS enabled
    [00:00:08.184,326] <err> Lesson4_Exercise2: Error in mqtt_connect: -12
    [00:00:08.184,356] <inf> Lesson4_Exercise2: Reconnecting in 60 seconds...
    [00:00:13.665,435] <inf> Lesson4_Exercise2: RRC mode: Idle
    [00:01:08.292,297] <inf> Lesson4_Exercise2: RRC mode: Connected
    [00:01:10.078,338] <err> Lesson4_Exercise2: Error in mqtt_connect: -12
    [00:01:10.078,369] <inf> Lesson4_Exercise2: Reconnecting in 60 seconds...
    [00:01:15.674,865] <inf> Lesson4_Exercise2: RRC mode: Idle

    The two lines regarding the MBEDTLS_HEAP caused build errors due to dependencies.  I haven't come across these settings before, can you give me a link to the relevant documentation.

    As I'm new to working with cellular (hence why I'm doing the course), can you give me some tips on how best to debug the TLS handshake.  Presumably, this is the source of the problem(?)  

  • I started to debug mqtt_connect this morning and have found that the -12 seems to be coming from connect_request_encode line 832 which is pack_utf8_str(client->username, buf). 

    The few other posts I have read seem also to be related to problems with the username / password structures.

    Any thoughts? 

Reply Children
No Data
Related