mqtt_simple sample adding username and password credentials

Hi,

I am trying to connect to cloud MQTT (cute cat) which needs username and password for authentication. In the sample the password and username is set to NULL in client_init:

client->password = NULL;
client->user_name = NULL;

I have edited prj.conf and Kconfig to add my password and username definitions in this format:

CONFIG_MQTT_BROKER_PASSWORD="password"
CONFIG_MQTT_BROKER_USERNAME="username"

but I can't figure out how to assign them in the code. Have someone successfully done this, if so what must be done?

Kind regards

Samuel

  • Update with how I tried to add username and password and error log.

    Code to try to add user name and password 

    client->password->utf8 = (u8_t *)CONFIG_MQTT_BROKER_PASSWORD;
    client->password->size = strlen(CONFIG_MQTT_BROKER_PASSWORD);
    client->user_name->utf8 = (u8_t *)CONFIG_MQTT_BROKER_USERNAME;
    client->password->size = strlen(CONFIG_MQTT_BROKER_USERNAME);

    This addition to the code builds and can be flashed to the board without problem, however while monitoring the serial bus I got this error:

    Prior to running this I built and flashed spm in secure mode and mqtt_simple in non-secure mode.

    The same log with password and user name set to NULL:

    Connecting to a MQTT broker without authentication works fine, in this case mosquittos test broker.

    Hope this extra information helps pin point the problem.

    /Samuel

  • client->password->utf8 = (u8_t *)CONFIG_MQTT_BROKER_PASSWORD;
    client->password->size = strlen(CONFIG_MQTT_BROKER_PASSWORD);
    client->user_name->utf8 = (u8_t *)CONFIG_MQTT_BROKER_USERNAME;
    client->password->size = strlen(CONFIG_MQTT_BROKER_USERNAME);

    That code is what is causing your bus faults.  "client->password" and "client->user_name" are likely uninitialized and so when that code runs it is de-referencing a struct at a bad location like NULL.  You need to allocate two static struct mqtt_utf8 in your code to put those values into and then point client->password and clien->user_name at those.