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

nrf9160 MQTT sample problem

Hello. I have problem with MQTT sample. I use NB-IoT network.

If I use public MQTT server without username and password, then MQTT sample ist works. But if I use MQTT server with username and password, then I have this response

Error 61 is for Connection refused, right ? I use this configure

And user name + password

It's good ? 

User name, password, port and IP, MQTT version for the server is good. I use another MQTT client with same configuration, and this is works.

This is output from the server, during connection from nrf9160

What am I doing wrong ? Thank you

Parents
  • Hi,

     

     

    Error 61 is for Connection refused, right ?

    For the inbuilt minimal libc in zephyr, that is correct (if you choose to use newlib-nano, errno.h will be different). Here from errno.h:

    #define ECONNREFUSED 61 /* Connection refused */

     

    For the .password and .user_name, these are both of struct mqtt_utf8, so you'll have to populate both the string and the size of the string:

    client->password.utf8 = (u8_t*)MY_PWD;
    client->password.size = strlen(MY_PWD);
    client->user_name.utf8 = (u8_t*)USER;
    client->user_name.size = strlen(USER);

     

    Could you check if this helps?

     

    Kind regards,

    Håkon

  • Yes this morning I found out. Well thank you.

    But password, and user_name is pointer to struct. I used this solution

    struct mqtt_utf8 pass,name;
    
    pass.size = strlen("PASS");
    pass.utf8 = (u8_t*)"PASS"
    name.size = strlen("NAME");
    name.utf8 = (u8_t *)"NAME";
    
    client->password = &pass;
    client->user_name = &name;

    Thank you.

  • Great to hear that you found the solution Slight smile

    Let me know if any other issues/questions should pop up.

     

    Cheers,

    Håkon

  • Yes, I have question. He stopped working for me mqtt_siple example. I do not know why, but I've tried everything.

    I only changed 

    #define CONFIG_MQTT_BROKER_HOSTNAME "18.194.228.78"
    #define CONFIG_MQTT_BROKER_PORT 1883
    //#define CONFIG_LTE_NETWORK_MODE_LTE_M 1
    #define CONFIG_LTE_NETWORK_MODE_NBIOT 1

    And after rebuild and flashing, I have this output on the console

    I do not know why. I reverted from git all source code, and change only this 4 line on the file autoconf.h. AT_client example is working. But mqtt_simple have this output.

    Please what is bad ? 

  • Bug is maybe on the line with HOSTNAME.

    If I use hostname

    #define CONFIG_MQTT_BROKER_HOSTNAME "iot.eclipse.org"

    Then on the console is output

    ERROR: getaddrinfo failed 22
    ERROR: mqtt_connect -47
    

    And error 22 is for Invaid argument ? 

    But if I use IP address on the this format 

    #define CONFIG_MQTT_BROKER_HOSTNAME "198.41.30.241"

    Then I have on the console this 

    ***** Booting Zephyr OS v1.14.99-ncs1 *****
    The MQTT simple sample started
    LTE Link Connecting ...
    LTE Link Connected!
    IPv4 Address found 0xf11e29c6
    Exception occurred in Secure State
    ***** HARD FAULT *****
      Fault escalation (see below)
    ***** BUS FAULT *****
      Precise data bus error
      BFAR Address: 0x50008120
    ***** Hardware exception *****
    Current thread ID = 0x20020284
    Faulting instruction address = 0x1b8c0
    Fatal fault in ISR! Spinning...

    But why ? What I'm doing wrong ? 

  • It looks like an code optimization error. I must to add a print NAME and PASS  to the console, and then it works. Thank you. Please close ticket.

    static void client_init(struct mqtt_client *client)
    {
    	mqtt_client_init(client);
    
    	broker_init();
    
    	/* MQTT client configuration */
    	client->broker = &broker;
    	client->evt_cb = mqtt_evt_handler;
    	client->client_id.utf8 = (u8_t *)CONFIG_MQTT_CLIENT_ID;
    	client->client_id.size = strlen(CONFIG_MQTT_CLIENT_ID);
    	//client->password = NULL;
    	//client->user_name = NULL;
    
            struct mqtt_utf8 pass,name;
            pass.size = (u32_t) strlen("PASS");
            pass.utf8 =(u8_t*)"PASS";
            printk("pass: %d,%s\n",pass.size,pass.utf8);
            name.size = (u32_t) strlen("NAME");
            name.utf8 = (u8_t*)"NAME";
            printk("name: %d,%s\n",name.size,name.utf8);
    
            client->password = &pass;
            client->user_name = &name;
    	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 */
    	client->transport.type = MQTT_TRANSPORT_NON_SECURE;
    }

Reply
  • It looks like an code optimization error. I must to add a print NAME and PASS  to the console, and then it works. Thank you. Please close ticket.

    static void client_init(struct mqtt_client *client)
    {
    	mqtt_client_init(client);
    
    	broker_init();
    
    	/* MQTT client configuration */
    	client->broker = &broker;
    	client->evt_cb = mqtt_evt_handler;
    	client->client_id.utf8 = (u8_t *)CONFIG_MQTT_CLIENT_ID;
    	client->client_id.size = strlen(CONFIG_MQTT_CLIENT_ID);
    	//client->password = NULL;
    	//client->user_name = NULL;
    
            struct mqtt_utf8 pass,name;
            pass.size = (u32_t) strlen("PASS");
            pass.utf8 =(u8_t*)"PASS";
            printk("pass: %d,%s\n",pass.size,pass.utf8);
            name.size = (u32_t) strlen("NAME");
            name.utf8 = (u8_t*)"NAME";
            printk("name: %d,%s\n",name.size,name.utf8);
    
            client->password = &pass;
            client->user_name = &name;
    	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 */
    	client->transport.type = MQTT_TRANSPORT_NON_SECURE;
    }

Children
No Data
Related