Unable to subscrive MQTT topic on IoT Hub - Connection Refused.

Andrea Previtali 106 Reputation points
2022-05-24T13:59:58.453+00:00

Hi all,

I'm trying to move my first steps in IoT Hub.
I created a test device and I'm trying to run the samples found here: https://learn.microsoft.com/it-it/azure/iot-hub/iot-hub-mqtt-support

In particular I'm trying the C/C++ samples without Azure SDK support.

I haven't any trouble using the application that send message to IoT Hub, infact , using Azure Iot Explorer, I can see the MQTT message arriving.

I would try to use the subscribe sample to see how it works but after launching it I get the following error:

Disconnect callback 7 : The connection was lost.  

After that I also tried with mosquitto_sub method in linux shell but i got the following result.

Client EM_MXC3166 sending CONNECT  
Client EM_MXC3166 received CONNACK (5)  
Connection error: Connection Refused: not authorised.  
Client EM_MXC3166 sending DISCONNECT  

It seems to me that all parameters are set in the right manner but obvioulsy I'm doing something wrong.
The publish method seems to work well.

Can someone help me with this issue pls?

Andrea

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,124 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vidya Narasimhan 2,201 Reputation points Microsoft Employee
    2022-05-24T17:30:17.72+00:00

    Hi @Andrea Previtali I believe you are talking about this sample here https://github.com/Azure-Samples/IoTMQTTSample/blob/master/src/Windows/SubscribeMQTTWin32/SubscribeMQTTWin32.cpp

    In addition to provinding IoTHub, device names and SAS Token, please ensure to download the Baltimore certificate file. Please also ensure that the token has not expired.

    define IOTHUBNAME <MyIoTHub>

    define DEVICEID <DeviceId>

    define CERTIFICATEFILE "<pathTo>\IoTHubRootCA_Baltimore.pem"

    define PWD "SharedAccessSignature sr=[yourIoTHub].azure-devices.net%2Fdevices%2F[DeviceId]&sig=[tokengeneratedforyourdevice]"

    The setup and pre-req are well described here https://learn.microsoft.com/en-us/samples/azure-samples/iotmqttsample/iotmqttsample/ .


  2. risolis 8,701 Reputation points
    2022-05-27T19:19:20.357+00:00

    Hi @Andrea Previtali

    Thanks for your answer.

    Which MQTT version is being used?
    which is the packet size used ?
    Is this for WS or Linux broker?
    Are you using user/password for authentication?
    Are you using MQTT SSL/TLS or PSK(Preshared KEY)?
    Are you using the default listening port number?
    Did you set up connection restriction custom value or is the default one?
    Have you tried to disable the TCP timestamp value? in order to try to avoid closing the connection if this takes more time than the expect.

    Looking forward to your feedback,

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.