Shows two ways of connecting to IOT Hub directly over MQTT.
A practical adaptation of the guidelines here: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support
Pre-requisites:
- Install Azure CLI https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
- Install IOT Extensions for Azure CLI
az extension add --name azure-cli-iot-ext
- Create a device identity with a self-signed cert that is valid for 10 days using
az iot hub device-identity create -n <hubname> -d <deviceid> --am x509_thumbprint --valid-days 10 --od .
This will generate two files<deviceid>-cert.pemand<deviceid>-key.pemin the current folder - Modify iothubcert.py by setting:
device_id=<deviceid>
iot_hub_name=<hubname> - Run iothubcert.py. It should connect and send one message to IOT Hub over MQTT.
- Create a device identity with a SAS Token:
az iot hub generate-sas-token -d <deviceid> -n <hubname>
The token is the entire returned value, i.e.SharedAccessSignature sig={signature-string}&se={expiry}&sr={URL-encoded-resourceURI} - Modify iothubsas.py by setting:
device_id=<deviceid>
iot_hub_name=<hubname>
sas_token=<value of sas token generated in step 1> - Run iothubsas.py. It should connect and send one message to IOT Hub over MQTT.