如标题所述,我在将我的 Mosquitto MQTT 客户端(用 C 语言编写)连接到我的 Azure 物联网中心时遇到了困难。我之前已经成功连接到许多不同的平台(例如 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”当然在我的代码中被替换为实际值。
你们中的任何人都可以指出我做错了什么,或者我需要采取哪些其他配置步骤吗?
最佳答案
我安装 mosquitto在 Windows 上并使用命令成功发送消息:
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 没有使用 CyberTrust 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。
希望对您有所帮助。
关于ssl - 将 Mosquitto MQTT C 客户端连接到 Azure 物联网中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49403601/