0
votes

As the title states, I'm having difficulties connecting my Mosquitto MQTT client (written in C) to my Azure IoT-hub. I've managed to connect to many different platforms before (e.g. Amazon EC2, ThingsBoard, TheThings.io, SierraWireless, ...), so I know my client is pretty solid.

The difficulty here is the fact that I need some sort of certificate to be allowed to connect, and I'm not sure what I need to do this.

I have added the following configuration in order to get this working:

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

In the code above, "hubname", "deviceName" and "sigValue" are of course replaced with real values in my code.

Can any of you point me to what I'm doing wrong, or what other configuration steps I need to take?

2

2 Answers

0
votes

I install mosquitto on Windows and send a message with the command successfully:

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

Based on information you provided, maybe the issue caused by the certificate.

Azure IoT Hub use DigiCert Baltimore Root certificate to secure device connection. (Note that China Azure didn’t use CyberTrust Root CA. Instead it still uses 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.

Here I save it as IoTHubTest.cer.

Hope this is helpful to you.

0
votes

Finally managed to get things working. It turned out I had cross-compiled my Mosquitto client without SSL support. So after compiling and installing again, these functions now all return 1 and I can connect successfully.

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