This project help me to be with preparartion to AZ-220 exam. You can also use all it if you wish ;)
I will continiusly add next features. Let's get started ;)
To have it working the following requirements should be met:
- Installed Azure Iot SDK for Python (https://github.com/Azure/azure-iot-sdk-python)
- Active Acure subscription Azure Portal
When you have alredy active the Azure subcriprion, you need to create at least below Azure IoT resources [in square brackets I will give the numbers of programs that will require the given object]:
- IoT Hub
[1,2]
- IoT Device
[1]
- IoT Hub Device Provisioning Service
[2]
TIP: If you are not yet familiar with Azure IoT I would recommend to the exercises from Microsoft Learning GitHub
This program sends messages with the following payload structure to the IoTHub:
"payload": {
"temperature": 28.03118331388056,
"humidity": 70.70824441850742,
"pressure": 1019.6749466511045,
"location": {
"latitude": 52.49183891046268,
"longitude": 21.256189610462688
}
The connection string to the IoT device is required. You can find it in Azure portal or you can use below PowerShell command:
az iot hub device-identity connection-string show --hub-name {IoTHubName} --device-id {IoTDeviceName} --output table
For security reasons it is not recommended to write the connection string directly to the program file. You can export it to the environment variable using following command:
export IOTHUB_DEVICE_CONNECTION_STRING={YourConnectiionString}
If you don't do it, you will be prompted by the program to provide it. Additionally the program will prompt you to provide number of messages to send and time interval between messages. If you provide number of messages as 0 or blank, the program will send it continiusly until press Ctrl+C. The default message interval is 1s.
To see if messages are flowing to the IoTHub successfully, you can use below PowerShell command:
az iot hub monitor-events --hub-name {IoTHubName} --device-id {IoTDeviceName}
NOTE: If you have configured message routing to send all messages to the endpoint, the above command will not show any messages.
This program automatically provision the IoT Device to IoT Hub through IoT Hub Device Provisioning Service (DPS) and then send messages to the UoT Hub with the same payload structure as in above example.
Before starting the program, perform the following steps (apart from creating Azure objects mentioned above):
- creation root and device certificates here you can find some help
- adding the root certificate to the DPS
- creation the enrolment group in the DPS
For security reasons it is not recommended to write the connection details directly to the program file. You can export it to the environment variable using following command:
export DPS_SCOPE_ID="{your DPS Scope ID}"
export X509_CERT_PATH="{path_to_the_folder with {Device_ID}.cert.pem file"
export X509_KEY_PATH="{path_to_the_folder with {Device_ID}.key.pem file}"
export X509_PASS_PHRASE="{certificate pass phrase}"
NOTE: You will be prompted to provide Device ID to be provisioned and files with certificate and key should be named as {Device_ID}.cert.pem
and {Device_ID}.key.pem
. Please don't use *.cert.pfx
files.