将Mosquitto MQTT C客户端连接到Azure IoT-hub

问题描述 投票:0回答:2

正如标题所述,我很难将我的Mosquitto MQTT客户端(用C编写)连接到我的Azure IoT-hub。我之前已经设法连接到许多不同的平台(例如Amazon EC2,ThingsBoard,TheThings.io,SierraWireless ......),所以我知道我的客户端非常稳固。

这里的困难是我需要某种证书才能连接,而且我不确定我需要做什么。

我添加了以下配置以使其正常工作:

mosquitto_opts_set(client, MOSQ_OPT_PROTOCOL_VERSION, "MQTT_PROTOCOL_V311");
mosquitto_tls_set(client, "/home/ca-certificates.crt", NULL, NULL, NULL, NULL);
mosquitto_tls_insecure_set(client, 1);
mosquitto_tls_opts_set(client, 0, "tlsv1", NULL);
mosquitto_username_pw_set(client, "hubname.azure-devices.net/deviceName", "SharedAccessSignature=SharedAccessSignature sr=hubname.azure-devices.net%2Fdevices%2FdeviceName&sig=sigValue&se=1553087157");

在上面的代码中,“hubname”,“deviceName”和“sigValue”当然被我的代码中的实际值替换。

你们中的任何人都可以指出我做错了什么,或者我需要采取哪些其他配置步骤?

ssl mqtt tls1.2 mosquitto azure-iot-hub
2个回答
0
投票

我在Windows上安装mosquitto并成功发送命令:

mosquitto_pub -d -h hubname.azure-devices.net -i "device1" -u "hubname.azure-devices.net/device1" -P "SharedAccessSignature sr=hubname.azure-devices.net%2Fdevices%2Fdevice1&sig=sig&se=1553325061" -m "hi from mosquitto client" -t "devices/device1/messages/events/" -p 8883 --cafile \path-to-cert-file\IoTHubTest.cer -V mqttv311

根据您提供的信息,可能是证书引起的问题。

Azure IoT Hub使用DigiCert Baltimore Root证书来保护设备连接。 (请注意,China Azure没有使用Cyber​​Trust Root CA.相反,它仍然使用WoSign Root CA.)

您可以通过从Azure IoT SDK for C中的certs.c复制证书信息来创建此文件。包括行----- BEGIN CERTIFICATE -----和----- END CERTIFICATE -----,删除“每行开头和结尾的标记,并删除每行末尾的\ r \ n字符。

在这里,我将其保存为IoTHubTest.cer。

希望这对你有所帮助。


0
投票

终于设法使事情有效。原来我在没有SSL支持的情况下交叉编译了我的Mosquitto客户端。所以在重新编译和安装之后,这些函数现在都返回1并且我可以成功连接。

mosquitto_opts_set(client, MOSQ_OPT_PROTOCOL_VERSION, "MQTT_PROTOCOL_V311");
mosquitto_tls_set(client, NULL, /etc/ssl/certs, NULL, NULL, NULL);
mosquitto_tls_insecure_set(client, 1);
mosquitto_tls_opts_set(client, 0, "tlsv1", NULL);
mosquitto_username_pw_set(client, "hubname.azure-devices.net/deviceName", "SharedAccessSignature=SharedAccessSignature sr=hubname.azure-devices.net%2Fdevices%2FdeviceName&sig=sigValue&se=1553087157");
© www.soinside.com 2019 - 2024. All rights reserved.