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

Connecting a Mosquitto MQTT C client to Azure IoT-hub

如标​​题所述,我在将我的 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"当然在我的代码中被替换为实际值。

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

我在 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 中心使用 DigiCert 巴尔的摩根证书来保护设备连接。 (请注意,China Azure 没有使用 Cyber​​Trust Root CA,而是仍然使用 WoSign Root CA。

You can create this file by copying the certificate information from certs.c in the Azure IoT SDK for C. Include the lines -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----, remove the " marks at the beginning and end of every line, and remove the \r\n characters at the end of every line.

这里我保存为IoTHubTest.cer。

希望对您有所帮助。

终于成功了。结果我 cross-compiled 我的 Mosquitto 客户端没有 SSL 支持。所以重新编译安装后,这些功能现在都return1,我可以连接成功了。

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");