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,

  • 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

  • Dear Øyvind,

    thank you! Indeed, username and password as a struct works! I just had to copy your example and immediately the Thingy could connect to the server.

    Under your last post, I couldn't find neither a reply button nor a "Verify Answer"-button. I can confirm that the structs do work.

    Thank you for your quick help! Smile

    Have a good day,

    Chris

  • Hi

    I am experiencing the same, however the fix below did not solve the problem for me. Any advice?

Related