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

  • I forgot to mention that I am using the isrgrootx1.pem and the certificate.h is:

    #define CA_CERTIFICATE \
    "-----BEGIN CERTIFICATE-----\n"\
    "MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n"\
    "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n"\
    "cmNoIE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HRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkq\n"\
    "hkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZL\n"\
    "ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ\n"\
    "3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK\n"\
    "NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5\n"\
    "ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur\n"\
    "TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n"\
    "jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n"\
    "oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n"\
    "4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n"\
    "mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n"\
    "emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n"\
    "-----END CERTIFICATE-----"

  • -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

Related