Skip to main content

Example: Using the Azure IoT Hub Client SL Library

Product: CODESYS IIOT Libraries SL

Description

The Azure IoT Hub Client SL Example.project sample project shows how to use of the library POUs of the Azure IoT Hub Client SL library.

It contains the AzureHTTPDemo and AzureMQTTDemo applications.

Procedure. Getting Started
  1. Set up the "Azure IoT Hub" and create devices in the "Azure IoT Hub".

  2. Generate "Shared Access Signatures" (SaS tokens).

    A device is authenticated via "Shared Access Signatures" (SaS tokens).

    For access via MQTT, an SaS token based on the "device" policy is required.

    Access via HTTPS requires an SaS token based on the "device" policy and an SaS token based on the "iothubowner" policy.

    The SaS token can be generated via the Azure Cloud Shell (Azure Portal), via the "Azure IoT Explorer" tool, or directly via the GenerateSasToken library function.

    The GenerateSasToken function takes 4 input parameters (URL address, Primary Key, Policy Name, Expiry) and returns an SaS token as WSTRING(255).

    1. Start the Cloud Shell from the Azure Portal.

      _example_img_azure_iot_hub_start_portal.png
    2. Generate a device SaS token:

      az iot hub generate-sas-token -d YOUR_DEVICE_ID -n YOUR_IOT_HUB --du DURATION_IN_SECONDS --policy device

    3. Generate the iothubowner SaS token:

      az iot hub generate-sas-token -n YOUR_IOT_HUB --du DURATION_IN_SECONDS

      Figure 10. Generated SaS tokens
      Generated SaS tokens



  3. In the sample project, set the names of the IoT Hub, DeviceId, and SaS tokens:

    sSubDomainName: Name of the "Azure IoT Hub" without "azure-devices.net" (see Step 1)

    sDeviceId: ID of the device

    wsDeviceSaS: SaS token of the device (MQTT, HTTP) (see Step 2)

    wsIoTHubOwnerSaS: SaS token of the IoT Hub Owner (HTTP) (see Step 2)

  4. Download the project to the controller and start the applications.

    1. Start the AzureHTTPDemo application.

      You can execute the following functions via the visualization:

      Send D2C Message

      Get C2D Message

      Get Device Twin

      Update Device Twin

      _example_img_azure_iot_hub_app_http_demo_visu.png
    2. Start the AzureMQTTDemo application.

      You can execute the following functions via the visualization:

      • Establishing an MQTT connection

      • Send D2C Message

      • Get C2D Message

      • Get Device Twin

      • Update Device Twin

      • Method call: Direct method call

      • Subscribe Device Twin (desired)

      _example_img_azure_iot_hub_app_mqtt_demo_visu.png
  5. You send and receive messages using the Azure IoT Explorer tool.

    The Azure IoT Explorer tool can be used to view and send messages. You can find the download link for it below.

    Figure 11. Azure IoT Explorer: Monitoring of D2C messages
    Azure IoT Explorer: Monitoring of D2C messages


    Figure 12. Azure IoT Explorer: Sending of D2C messages
    Azure IoT Explorer: Sending of D2C messages


    Figure 13. Azure IoT Explorer: Remote method call
    Azure IoT Explorer: Remote method call


Additional information

"Azure IoT Explorer" tool:

For more information about "Cloud Shell" and "Azure CLI", see: az iot hub

Tip

In the Azure environment, messages are usually sent in JSON format. The CODESYS library JSON Utilities SL can be used for parsing and generating JSON files.

System requirements and restrictions

Programming system

CODESYS Development System (version 3.5.18.0 or higher)

Runtime system

CODESYS Control Win (version 3.5.18.0 or higher)

Note: Use the free Device Reader project (available in the CODESYS Store International) to find out the functions supported by the controller.

Add-on components

-

Additional requirements

Microsoft Azure account with Azure IoT Hub service, device explorer

Note

_example_icon.png DOWNLOAD projects