Skip to content

krishtekpl/AzureIoTSamples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AzureIoTSamples

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

Prerequisites

To have it working the following requirements should be met:

  1. Installed Azure Iot SDK for Python (https://github.com/Azure/azure-iot-sdk-python)
  2. Active Acure subscription Azure Portal

Azure IoT resources

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]:

  1. IoT Hub [1,2]
  2. IoT Device [1]
  3. 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

1. Sending message to IoT hub asynchronously (sim_device_async.py)

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.

2. Provisioning IoTDevice using DPS and X509 authentication (dps_prov_x509.py)

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):

  1. creation root and device certificates here you can find some help
  2. adding the root certificate to the DPS
  3. 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.

About

Azure IoT SDK Python examples

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages