Connecting to Private MQTT broker

Dear Nordic Team, 

I am currently using nrf9160 for MQTT transmission. I have successfully connected to a public MQTT broker which is HiveMQ and transmitted data to HiveMQ. The next step I am trying to achieve is to transfer data to a private MQTT broker which will require a username and password. May I know how I configure these parameters in the prj.conf file? The image below is my configuration for HiveMQ. 

Parents
  • Hello, 

    You will need to know if the MQTT broker uses TLS for private connection. If so, you can build your project using nrf\samples\nrf9160\mqtt_simple\overlay-tls.conf.

    If you are using the simple_mqtt sample, you can add the user_name and password in client_init(). From zephyr\include\zephyr\net\mqtt.h

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    /** User name (if any) to be used for the connection. NULL indicates
    * no user name.
    */
    struct mqtt_utf8 *user_name;
    /** Password (if any) to be used for the connection. Note that if
    * password is provided, user name shall also be provided. NULL
    * indicates no password.
    */
    struct mqtt_utf8 *password;
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    More information can be found in the MQTT library documentation

    Kind regards,
    Øyvind.

  • Hi, sorry for the late reply. I have modified my code in client_init(), prj.conf, and Kconfig. However, I still couldn't send the data data to private MQTT broker. I have attached the code below. 8741.MQTT_simple.zip

  • 8816.mqtt_simple_sensor (2).zipHi, Please refer to this file instead

  • Hi, is there anyone to answer my question?

  • Hello, 

    Looking at the last project that you have attached, there is no username or password added to client_init().

    Both are configured to NULL.

    Have a look at the sample zephyr\samples\net\cloud\mqtt_azure. I.e. this shows how to configure the username and password. The sample uses KConfig to define the Kconfig symbols which you can add value to in prj.conf. In client_init() the following is added:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    //add to top off client_init()
    static struct mqtt_utf8 password;
    static struct mqtt_utf8 username;
    client->client_id.utf8 = (uint8_t *)MQTT_CLIENTID;
    client->client_id.size = strlen(MQTT_CLIENTID);
    password.utf8 = (uint8_t *)CONFIG_SAMPLE_CLOUD_AZURE_PASSWORD;
    password.size = strlen(CONFIG_SAMPLE_CLOUD_AZURE_PASSWORD);
    client->password = &password;
    username.utf8 = (uint8_t *)CONFIG_SAMPLE_CLOUD_AZURE_USERNAME;
    username.size = strlen(CONFIG_SAMPLE_CLOUD_AZURE_USERNAME);
    client->user_name = &username;
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    This can be used as an example to get your project up and running.

  • MQTT_simple (3).zip

    This is my modified code. However, it still doesn't send messages to the private MQTT broker. Can you please help me to have a look?

    best regards,

    Yeo Rhi Khin

Reply Children