Here is an example of how to send messages to Azure IoT Hub (MQTT).
Preparations
Prepare a Linux or Mac to create a certificate.
Downloading the tools
To use a certificate in Azure IoT Hub, please download the command line tools to your Linux or Mac from here.
For Windows, execute the bash command from PowerShell and use bash.
Enter the following commands to create a certificate that can be pasted into the field.
Unzip the downloaded zip file and move to the tools directory.
cd <path>/azure-iot-sdk-c-main/tools/CACertificates
Give the tool execution permissions.
chmod +x ./certGen.sh
Creating a CA certificate
Run the tool to create a CA certificate.
./certGen.sh create_root_and_intermediate
Creating a device certificate
Run the tool to create a certificate for the device.
./certGen.sh create_device_certificate <device id>
※ is where you specify the device ID.
Create a public key for the device.
openssl pkcs12 -in certs/new-device.cert.pfx -clcerts -nokeys -out <device id>.crt
※If you are asked to enter a password, enter the password that is displayed on the screen.
Create a private key for the device.
openssl pkcs12 -in certs/new-device.cert.pfx -nocerts -nodes -out <device id>.key
※If you are asked to enter a password, enter the password that is displayed on the screen.
.crt and .key will be used in Gravio Studio.
If you are using multiple devices, please create one for each device.
Creating an IoT Hub in Azure
Log into Azure and display and create the IoT Hub:
Create a new resource group. In the sample, we named it “iothub”.
Enter the IoT Hub name. In the sample, we named it “gravio-mqtt-iot”.
Specify the network.
Specify the scaling level and units in Management. In the sample, we selected default.
Confirm and create with the content of the settings.
When the deployment is complete, “gravio-mqtt-iot” will be created.
Select “gravio-mqtt”, open and create the certificate.
In Add Certificate, enter the certificate name and specify the CA certificate created in the pre-preparation. Set “Confirm certificate status at upload time” to On.
When you save the certificate, it will be displayed in the list.
Open and create the device.
The device ID is the ID specified in the device certificate. Select “X.509CA Signed” for the type of authentication.
When you save the device, it will be displayed in the list.
Open the overview and record the hostname. The hostname is the entry point for the Azure IoT Hub.
Setting up the MQTTPublisher in Gravio Studio
Set the properties of the MQTTPublisher in Gravio Studio as follows.
Property name | Description |
---|---|
Topic | Specify “devices/” + device ID + “/messages/events/”. For example, “devices/d002/messages/events/” |
Client ID | Specify the device ID. For example, “d001” |
QoS | (Leave as default) |
Retain | (Leave as default) |
Timeout | (Leave as default) |
URL | Specify the hostname following “ssl://”. For example, “ssl://gravio-mqtt.azure-devices.net” |
Port | Specify 8883 |
Username | Specify the hostname + “/” + device ID. For example, “gravio-mqtt.azure-devices.net/d001” |
Password | (Don’t specify) |
Certificate | Specify the public key of the device created in the pre-preparation |
Secret Key | Specify the private key of the device created in the pre-preparation |
Secret Key Passphrase | (Don’t specify) |
Root Certificate | (Don’t specify) |
With this, the settings for sending data from MQTTPublisher to Azure IoT Hub (MQTT) are complete.
Need more help with this?
Join our slack community for help