How to resolve MQTT CONNACK error 5 - Connection Refused, not authorized

Brad Johnson 25 Reputation points
2023-09-28T16:47:24.4233333+00:00

I am using the Azure IoT SDK on an IoT device and trying to connect to the IoT Hub. the embedtls connection setup is successful, the tsl/ssl handshake is successful, but the MQTT Connect packet returns a CONNACK with error 5 - Connection Refused, not authorized. Please assist.

Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
219 questions
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 15,241 Reputation points Microsoft Employee
    2023-10-11T13:35:19.7266667+00:00

    Hi @Brad Johnson I'm glad that you were able to resolve your issue and thank you for letting us know your solution so that others experiencing the same thing can easily reference this! I'll repost your solution in case you'd like to accept the answer .

    Error Message:

    MQTT Connect packet returns a CONNACK with error 5 - Connection Refused, not authorized

    Issue:

    Using Azure RTOS middleware to connect to Azure IoT Hub through MQTT. The embedtls connection setup is successful, the tsl/ssl handshake is successful, but the MQTT Connect packet returns a CONNACK with error 5 - Connection Refused, not authorized

    Solution:

    There is a type-casting error in the code which led to truncated connection data within the Azure IOT freeRTOS middleware.

    If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information on the specific section of the code which you have updated to resolve this.

    I hope this helps!

    If you have any other questions, please let me know. Thank you again for your time and patience throughout this issue.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 32,556 Reputation points MVP
    2023-09-28T20:39:29.5+00:00

    Hello @Brad Johnson ,

    the Azure IoT Hub supports devices to connect using several SDKs and using 'plain' MQTT.

    Using the SDKs is recommended due to extra logic being implemented already like throttling and reconnecting.

    The MQTT connection is very particular regarding the topics being used.

    Please compare your MQTT calls with this reference solution written in C#.

    Remember to supply the correct certificate (the Baltimore certificate is deprecated. You need to supply a DigiCert certificate now).


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.