Azure-iot-sdk-c: Faile to connect the Azure Iot Hub

Created on 16 Jan 2018  ·  7Comments  ·  Source: Azure/azure-iot-sdk-c

HI:

OS and version used: MBED

SDK version used: 1.1.23

Description of the issue:

Faile to connect the Azure Iot Hub.

My equipment connectionString = "HostName=AzureIoTHub4Beta.azure-devices.cn;DeviceId=011299d4e6fd50f80_011299d4e6fd50f81;SharedAccessKey=Qz2RHaEPhX4z74mWJDKru0PVbd7/3niBMG2mJQU9dCg="

I generate the mqtt equipment connectionstring by SDK for C:

00 04 4d 51 54 54 04 00 00 f0 (Fixed header ,10 bytes) 
+
011299d4e6fd50f80_011299d4e6fd50f81 AzureIoTHub4Beta.azure-devices.cn/011299d4e6fd50f80_011299d4e6fd50f81/api-version=2016-11-14&DeviceClientType=iothubclient%2f1.1.23%20ARDUINO_ARCH_ESP8266 SharedAccessSignature sr=AzureIoTHub4Beta.azure-devices.cn/devices/011299d4e6fd50f80_011299d4e6fd50f81&sig=qyc3S9BVFQwb1M4%2fVjbb81TunGssJP0okk8JpiVq74s%3d&se=3032142972&skn=
(Payload ,365 bytes)

Altogether 375 bytes

My SDK source :

https://github.com/Azure/azure-iot-sdk-c/blob/master/doc/mbed_get_started.md   ——————   https://os.mbed.com/ create

But now it's faile to connect the Azure Iot Hub,the TCP closed by Azure Iot Hub.

Please help analysis:
1、The structure of my mqtt equipment connectionstring (375 bytes)is correct or not.
2、What is the reason that causes my equipment faile to connect to Azure Iot Hub.

Thanks!

question

Most helpful comment

A couple of things:
Are you completing the SSL handshake? If the connection has not been completed then the content of your CONNECTION packet is irrelevant. The packet itself looks good though.
Please be aware that the China Azure data center uses different root certificates to those in the SDK file certs.c. Run the command:

openssl s_client -connect AzureIoTHub4Beta.azure-devices.cn:8883

This will dump the certificate chain for you. The openssl command is available on most desktop Linux distributions.

_Mark Radbourne MSFT_

All 7 comments

Latest situation:
10 FD 02 (Fixed_Header,3 bytes)
+
00 04 4d 51 54 54 04 C0 00 f0 (Varible_Header,10 bytes)
+
011299d4e6fd50f80_011299d4e6fd50f81 AzureIoTHub4Beta.azure-devices.cn/011299d4e6fd50f80_011299d4e6fd50f81/api-version=2016-11-14&DeviceClientType=iothubclient%2f1.1.23%20ARDUINO_ARCH_ESP8266 SharedAccessSignature sr=AzureIoTHub4Beta.azure-devices.cn/devices/011299d4e6fd50f80_011299d4e6fd50f81&sig=CaTo2q%2bo0WF0Cr5nPYUAkraRUgr4ngrA%2fxSO0Kfwzd4%3d&se=3032247752&skn=
(Payload ,371 bytes)

Altogether 384 bytes

Still faile to connect the Azure IoT Hub

Hi @caobaokun ,
are you any specific sample we have?
Could you try running one of our samples in your device?
https://github.com/Azure/azure-iot-sdk-c/tree/master/iothub_client/samples/iothub_ll_telemetry_sample/mbed

Make sure you are setting the trusted certs on your application.

Your connection string is good.
You can validate it yourself by running one of our samples in your Windows or Linux box.
I ran that same sample suggested above using your connection string on a Linux environment. It works.

user@user-debian921:~/azure-iot-sdk-c/cmake/iothub_client/samples/iothub_ll_telemetry_sample$ ./iothub_ll_telemetry_sample
./iothub_ll_telemetry_sample: /usr/local/lib/libcurl.so.4: no version information available (required by ./iothub_ll_telemetry_sample)
Creating IoTHub handle
Sending message 1 to IoTHub
Sending message 2 to IoTHub
Sending message 3 to IoTHub
Sending message 4 to IoTHub
Sending message 5 to IoTHub
-> 14:57:32 CONNECT | VER: 4 | KEEPALIVE: 240 | FLAGS: 192 | USERNAME: AzureIoTHub4Beta.azure-devices.cn/011299d4e6fd50f80_011299d4e6fd50f81/api-version=2016-11-14&DeviceClientType=iothubclient%2f1.1.30%20(native%3b%20Linux%3b%20x86_64) | PWD: XXXX | CLEAN: 0
<- 14:57:32 CONNACK | SESSION_PRESENT: false | RETURN_CODE: 0x0
-> 14:57:32 PUBLISH | IS_DUP: false | RETAIN: 0 | QOS: DELIVER_AT_LEAST_ONCE | TOPIC_NAME: devices/011299d4e6fd50f80_011299d4e6fd50f81/messages/events/property_key=property_value&%24.cid=CORE_ID&%24.mid=MSG_ID&%24.ct=application%2Fjson&%24.ce=utf-8 | PACKET_ID: 2 | PAYLOAD_LEN: 12
-> 14:57:32 PUBLISH | IS_DUP: false | RETAIN: 0 | QOS: DELIVER_AT_LEAST_ONCE | TOPIC_NAME: devices/011299d4e6fd50f80_011299d4e6fd50f81/messages/events/property_key=property_value&%24.cid=CORE_ID&%24.mid=MSG_ID&%24.ct=application%2Fjson&%24.ce=utf-8 | PACKET_ID: 3 | PAYLOAD_LEN: 12
-> 14:57:32 PUBLISH | IS_DUP: false | RETAIN: 0 | QOS: DELIVER_AT_LEAST_ONCE | TOPIC_NAME: devices/011299d4e6fd50f80_011299d4e6fd50f81/messages/events/property_key=property_value&%24.cid=CORE_ID&%24.mid=MSG_ID&%24.ct=application%2Fjson&%24.ce=utf-8 | PACKET_ID: 4 | PAYLOAD_LEN: 12
-> 14:57:32 PUBLISH | IS_DUP: false | RETAIN: 0 | QOS: DELIVER_AT_LEAST_ONCE | TOPIC_NAME: devices/011299d4e6fd50f80_011299d4e6fd50f81/messages/events/property_key=property_value&%24.cid=CORE_ID&%24.mid=MSG_ID&%24.ct=application%2Fjson&%24.ce=utf-8 | PACKET_ID: 5 | PAYLOAD_LEN: 12
-> 14:57:32 PUBLISH | IS_DUP: false | RETAIN: 0 | QOS: DELIVER_AT_LEAST_ONCE | TOPIC_NAME: devices/011299d4e6fd50f80_011299d4e6fd50f81/messages/events/property_key=property_value&%24.cid=CORE_ID&%24.mid=MSG_ID&%24.ct=application%2Fjson&%24.ce=utf-8 | PACKET_ID: 6 | PAYLOAD_LEN: 12
<- 14:57:32 PUBACK | PACKET_ID: 2
Confirmation callback received for message 1 with result IOTHUB_CLIENT_CONFIRMATION_OK
<- 14:57:32 PUBACK | PACKET_ID: 3
Confirmation callback received for message 2 with result IOTHUB_CLIENT_CONFIRMATION_OK
<- 14:57:32 PUBACK | PACKET_ID: 4
Confirmation callback received for message 3 with result IOTHUB_CLIENT_CONFIRMATION_OK
<- 14:57:32 PUBACK | PACKET_ID: 5
Confirmation callback received for message 4 with result IOTHUB_CLIENT_CONFIRMATION_OK
<- 14:57:32 PUBACK | PACKET_ID: 6
Confirmation callback received for message 5 with result IOTHUB_CLIENT_CONFIRMATION_OK
-> 14:57:32 DISCONNECT
Press any key to continue
user@user-debian921:~/azure-iot-sdk-c/cmake/iothub_client/samples/iothub_ll_telemetry_sample$

Btw make sure you delete your device id, since you made your connection string public here.

A couple of things:
Are you completing the SSL handshake? If the connection has not been completed then the content of your CONNECTION packet is irrelevant. The packet itself looks good though.
Please be aware that the China Azure data center uses different root certificates to those in the SDK file certs.c. Run the command:

openssl s_client -connect AzureIoTHub4Beta.azure-devices.cn:8883

This will dump the certificate chain for you. The openssl command is available on most desktop Linux distributions.

_Mark Radbourne MSFT_

Hi @ewertons @markrad
First of all, thank you for your help!
I have now successfully connected to the Azure IoT Hub.This connection string is correct as you said.
I transplanted mbed platform to the freertos platform and I used an 4G module replace the Ethernet because my device has no Ethernet.
My current device software process is freertos+mqtt+wolfssl(tls/ssl)+4G module(socket+tcp/ip),it is can connect to the Azure IoT Hub.Before this, I did not connect to success because of the lack of tls/ssl
Thank you again for the help!

Thank you very much @caobaokun.
We are glad it is now working.
Let us know if we can help in any other way.

-Azure IoT Team

Was this page helpful?
0 / 5 - 0 ratings