AndreaPrevitali-6172 avatar image
0 Votes"
AndreaPrevitali-6172 asked AndreaPrevitali-6172 commented

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

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:

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?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

VidyaNarasimhan-2409 avatar image
0 Votes"
VidyaNarasimhan-2409 answered AndreaPrevitali-6172 commented

Hi @AndreaPrevitali-6172 I believe you are talking about this sample here

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][DeviceId]&sig=[tokengeneratedforyourdevice]"

The setup and pre-req are well described here .

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @VidyaNarasimhan-2409 , thanks for your answer.

I don't know why but... .pem file were not in the same directory of compiled file...
Now the program run but I still have some problem.

In this moment I am running MQTT_Mosquitto (the application which send messages) without any problems, I'm monitoring messages via Azure IoT Explorer and all the messages are ok.
As yesterday I would read theese messages via "SubscribeMQTTWin32" but now I'm encountering a different issue.

I'm following theese steps:

  1. Run SubscribeMQTTWin32

  2. On the screen I see this message, so I think the connection is established:
    Connect callback returned result: No error.
    Waiting for C2D messages...

  3. I run MQTT_Mosquitto in order to send a message. Every time I send a message from this application, the subscriber print this error message:
    Disconnect callback 7 : The connection was lost.Connect callback returned result: No error.
    Waiting for C2D messages...

I guess the connection is lost and few milliseconds later the connection is ok again.

I thinkg my sas token is well created since is the same I am using in the sender application and it works.

I guess there is something stupid I am doing wrong but I can't figure where exactly in the error.


0 Votes 0 ·

@AndreaPrevitali-6172 great to know that the connection error got resolved.
For the current issue of not receiving messages via subscriber, can you please check if you are using the default consumer group to consume both from IoT explorer and subscriber at the same time. This can cause issues. Shut the IoT explorer and try consuming only from Subscriber.

Please mark as Answer if the responses have helped you so that it can help the community.

0 Votes 0 ·

Hi @VidyaNarasimhan-2409

Unfortunately I continue to facing up this issue.
I shutted down IoT explorer but nothing changed.

The Disconnect callback 7 continue to appears.


0 Votes 0 ·
Show more comments
ricardosolisvillegas-4678 avatar image
0 Votes"
ricardosolisvillegas-4678 answered AndreaPrevitali-6172 commented

Hi @AndreaPrevitali-6172

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AndreaPrevitali-6172

I just wanted to ask if you were able to read previous feedback on this thread.


Have a good one!

0 Votes 0 ·

Hi @ricardosolisvillegas-4678 !

Sorry for replying you after two days, I was out of the office these days.
I'll try to answer your question, I'm completely new to this world so, if I write something silly, please don't laugh :D

According to the code I downloaded from your sample, I am using MQTT version 3.11 (in the code I see following lines:

 // specify the mqtt version to use
     int* option = new int(MQTT_PROTOCOL_V311);

I guess the package is very small since I'm trying sample code so the message is the following (As I told I can see the message arrives to IoT Hub using IoT Explorer):

 char msg[] = "Bonjour MQTT from WSL";

I followed the instruction from this link ( to set a working authentication.
In particoular I set as PWD a SharedAccessSignature obtained from IoT Explorer (Device -> Connection string with SaS token).
I used this PWD for both application, "sender" and "subscriber", is it correct?

The port number is set for both application to 8883 (I did not edit that, It was already setted to this value).

How can I disable TCP timestamp value?



0 Votes 0 ·