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

MQTT - Cannot connect to broker when using username and PW

Hi, I use the following.

  • Raspberry Pi 3 (RPi3 with Raspbian Jessie 4.4) as a router,

RPi3 is connected to a Wi-Fi router to access the Internet by using the built-in Wi-Fi BLE BCM43438 chip.

  • IoT SDK 0.9 (nrf5_iot_sdk_3288530)

  • MQTT Publisher Example, configured as the non-secure port

  • PCA10040 v1.1.0 (2016.23)

  • Keil MDK-ARM uVision 5.20 (in Windows 10 64bit)

J-Link

  • Amazon EC2 (which uses Ubuntu 14.0) for Mosquitto Broker (v 1.4.9)

  • Amazon RDS which uses MySQL 5.6 for ACL (Access Control Lists)

Thanks to Stian's answer,I tested the example and published the LED state.

Now I added Access Control Lists (ACL) by referring this blog.

I inserted username "user1" with password "1234" for testing. This user had read/write control.

I tested with Android Paho MQTT client Example; the client who has the username and password above can only publish or subscribe.

Other users who had different username was disconnected when they tried to publish something.

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/

I entered the username and password. I modified the code like this;

#define APP_MQTT_BROKER_PORT                1883 /*8883*/    
static void button_event_handler(uint8_t pin_no, uint8_t button_action){ // omitted some parts
   case BSP_BUTTON_0:{ if (m_connection_state == false){
   mqtt_client_init(&m_app_mqtt_client);

   mqtt_utf8_t pw = {"1234", 0};
   mqtt_utf8_t user = {"user1", 0};
   pw.utf_strlen = strlen(pw.p_utf_str);
   user.utf_strlen = strlen(user.p_utf_str);

   memcpy(m_app_mqtt_client.broker_addr.u8, m_broker_addr.u8, IPV6_ADDR_SIZE);
   m_app_mqtt_client.broker_port          = APP_MQTT_BROKER_PORT;
   m_app_mqtt_client.evt_cb               = app_mqtt_evt_handler;
   m_app_mqtt_client.client_id.p_utf_str  = (uint8_t *)m_client_id;
   m_app_mqtt_client.client_id.utf_strlen = strlen(m_client_id);
   m_app_mqtt_client.p_password           = &pw /*NULL*/;
   m_app_mqtt_client.p_user_name          = &user /*NULL*/;
   m_app_mqtt_client.transport_type       = MQTT_TRANSPORT_NON_SECURE /* MQTT_TRANSPORT_SECURE*/;
   m_app_mqtt_client.p_security_settings  = NULL /*&m_tls_keys*/;

   uint32_t err_code = mqtt_connect(&m_app_mqtt_client);
   if(err_code || &(m_app_mqtt_client.p_password) == NULL) // breakpoint location
   APP_ERROR_CHECK(err_code);

cmd

I placed a breakpoint at if(err_code) statement.

debug

Although it returns no error, the PCA10040 cannot connect to the broker; the LED doesn't change to LED 2 on mode.

with PW

I sniffed the bt0 interface. You can see the packets in detail; wire-wPWCap.pcapng

Don't have a clue why destination port 0 is captured.

Did anybody test the MQTT example with ACL?

What seems to be the reason for failing connection after using ACL?

-Best Regards, Mango

  • Hi, the problem is how you declare and define the mqtt_utf8_t pw and user variables, and also the strings within them. Remember that you pass the pointers to the m_app_mqtt_client varable, so when the function returns they will no longer be valid. You should declare them as global variables at the top of main.c. Something like this should work:

    static uint8_t username[] = "test";
    static uint8_t password[] = "test";
    static mqtt_utf8_t pw;
    static mqtt_utf8_t user;
    

    And then in button_event_handler() you can assign the values:

    pw.p_utf_str = password;
    pw.utf_strlen = strlen(password);
    user.p_utf_str = username;
    user.utf_strlen = strlen(username);
    

    And pass the pointers m_app_mqtt_client:

    m_app_mqtt_client.p_password           = &pw;
    m_app_mqtt_client.p_user_name          = &user;
    

    PS. I also tried with Mosquitto and the Auth plugin, and I got it working by following the guide you linked to. Just remember to get the apostrophes right ('%s') if you are copy/pasting the /etc/mosquitto/mosquitto.conf example directly. I also had to do the fix described here: github.com/.../33

  • My mistake. Thanks for pointing it out and it works as you mentioned.

Related