Azure IoT SDK Python API
Azure IoT
Azure IoT is Microsoftās end-to-end IoT platform. Microsoft offers products like Azure IoT Hub to easily and securely connect your IoT devices to Microsoft Azure.
SDK for Python
You can connect devices to Azure IoT using open-source device SDKs offered by Microsoft. These SDKs support multiple operating systems and programming languages, including Python. One of them ā Azure IoT Hub Device SDK for Python ā was implemented as a standalone Router App for Advantech routers called Azure IoT SDK Python.

Tips
Two versions of this Router App are available: Azure IoT SDK Python and Azure IoT SDK Python3 API Version 2. The original version is still available for compatibility reasons and can still be used for existing implementations. Azure IoT SDK Python version 2 was completely reworked to Python. The original version and version 2 are not compatible.
For more information, including features of the device SDK, see:
https://github.com/Azure/azure-iot-sdk-python
Note that only the "device SDK" part of the Python SDK was implemented.A more complex README file for the Python SDK is available here:
https://github.com/Azure/azure-iot-sdk-pythonThe SDK for the deprecated version 1 is still available here:
https://github.com/Azure/azure-iot-sdk-python/tree/v1-deprecated
Caution
- This Router App has been tested on a router with firmware version 6.3.10. After updating the router firmware to a higher version, check whether a newer version of the Router App has also been released and update it accordingly for compatibility.
- The Azure IoT SDK Python Router App is not installed on Advantech routers by default. It can be downloaded from https://icr.advantech.com/user-modules. There is a dependency for the Azure IoT SDK Python Router App to be installed in the router ā follow the instructions in the Dependency Chapter. See the Configuration Manual, chapter Customization ā Router Apps, for a description of how to upload a Router App.
Azure IoT SDK Python Dependency
Tips
The Python3 Router App must be installed alongside Azure IoT SDK Python. Python3 is required for Azure IoT SDK Python to work ā it is a separate module and can be used as a standalone Python3 for other purposes.

Available Python Modules
Installing Python3 and Azure IoT SDK Python offers a set of standard and common Python modules, including these:
- os
- sys
- logging
- time
- datetime
- multiprocessing
- threading
- json
- uuid
- sqlite3
- textutils
- importlib
- shell
- compression
- subprocess
- tblib
- uuid
To list all available Python modules, type the following command in the routerās command-line interface (available via SSH):
python3The prompt will switch to Python mode starting with >>>. Enter Python help mode by typing:
help()Now you are in the Python help mode starting with help>, and you can type the following command for the full list of installed Python modules:
modulesSee the example output in the figure below:

Azure Installation
Tips
Detailed information and examples are available at: https://github.com/Azure/azure-iot-sdk-python
Install Python3 with PIP into the router.
Install Python requirements ā Setuptools, azure-iot-device (via the routerās CLI):
pip3 install setuptools pip3 install azure-iot-deviceCreate a link for the routerās certificate:
ln -s /etc/ssl/certs/ca-certificates.crt /usr/ssl/cert.pemThis link is permanent. Alternatively, add the following line to every Python script under
async def main():os.environ["SSL_CERT_FILE"] = "/etc/ssl/certs/ca-certificates.crt"
Example code inclusion Create an Azure IoT environment (Azure account, Azure IoT Hub, Device Provisioning Center).
Create a device in Azure IoT Hub and copy its Primary Connection String to the clipboard.

Primary connection string Set a variable in the Python environment for the device in Azure via the router CLI:
export IOTHUB_DEVICE_CONNECTION_STRING="PASTE_THE_CONNECTION_STRING_HERE"
Exporting the connection string Start the Azure IoT Python script:
Starting the script View communication details in the Azure Shell:

Communication in Azure shell