- Print
- DarkLight
- PDF
Dual Data Path - MQTT data directly to Azure Iot Hub from Gateway
Introduction
In this tutorial, we'll demonstrate how to send MQTT data directly to Azure IoT Hub from a Neuron gateway. MQTT is a popular communication protocol for IoT devices and Azure IoT Hub provides secure and scalable communication between IoT devices and the cloud. Follow along to learn how to set up this integration.
Security
IoT Hub uses Shared Access Signature (SAS) tokens to authenticate devices and services to avoid sending keys on the wire. You use SAS tokens to grant time-bounded access to devices and services to specific functionality in IoT Hub. To get authorization to connect to IoT Hub, devices and services must send SAS tokens signed with either a shared access or symmetric key. Symmetric keys are stored with a device identity in the identity registry. A token signed with a shared access key grants access to all the functionality associated with the shared access policy permissions. A token signed with a device identity's symmetric key only grants the DeviceConnect permission for the associated device identity.
Our gateway uses the MQTT protocol to send data to the Iot Hub. When using MQTT, the CONNECT packet has the deviceId as the ClientId, {iothubhostname}/{deviceId} in the Username field, and a SAS token in the Password field. {iothubhostname} should be the full CName of the IoT hub (for example, contoso.azure-devices.net).
SharedAccessSignature sig={signature-string}&se={expiry}&skn={policyName}&sr={URL-encoded-resourceURI}
Add gateway to your Azure Iot Hub
Go to your Azure Iot Hub and select Devices. Choose + Add device and use the gateway serial number as the Device ID. Leave the rest at the default values and Save the device.
MQTT Settings
See MQTT data directly for general config
Server URI:
{iothub-hostname}
neuronhub.azure-devices.net
Port:
8883
Azure has some special requirements for username/password
Username: {iotHub-hostname}/{device-id}/?api-version=2021-04-12.
It's recommended to include api-version in the field. Otherwise it could cause unexpected behaviors.
neuronhub.azure-devices.net/VP919100141310/?api-version=2021-04-12
Password / auth token: Insert the SAS token for the selected gateway.
SharedAccessSignature sr=neuronhub.azure-devices.net%2Fdevices%2FVP919100141310&sig=w7LmMg4kynkqC7eFZeJ0%2By****XxjXCJNYi1hK%2F%2BYNM%3D&se=2323076739
Topic
devices/%s/messages/events/
The topic has to be right or the client will disconnect and Secondary Broker Status will show CONNECTING
You can use Visual studio Code and the Azure IoT Tools for Visual Studio Code
Or you can generate a SAS token with the CLI extension command in your Azure terminal
az iot hub generate-sas-token