#AzureIoT – How to create an Azure IoT module from an Azure #CustomVision project 👀 3/N

Buy Me A Coffee

Hi !

In my previous post I wrote about how to create an Azure Custom Vision project. And, once created how to export the project as a Docker image to be used in different platforms. I also analyzed the content of the exported Linux image.

Today I´ll perform a similar analysis with an empty Azure IoT module. I created an Azure IoT Solution named [AzureIoTSolution], and I added a Python module named [ImageDrawingRecognitionModule]. The main solution content is described in the image below. We can find

  • Folder modules app. This folder contains the Azure Iot modules for the solution.
  • Folder ImageDrawingRecognitionModule. Main IoT module, includes several docker platform configuration files, python files for app [main.py] and requirements [requirements.txt]; and the module configuration [module.json].
  • Root files. Deploy template files for release and debug modules.
azure iot solution contents

As I mentioned, there are several platforms. For this post I’ll focus on the Raspberry Pi world, so let’s take a look at the content of the [Dockerfile.arm32v7].

  • It’s based on a Python 3.7 slim image for arm32v7
  • Install the following prerequisites: azure-iot-device~=2.0.0
  • Run the python app in the file [main.py]
FROM arm32v7/python:3.7-slim-buster


COPY requirements.txt ./
RUN pip install -r requirements.txt

COPY . .

CMD [ "python3", "-u", "./main.py" ]

And finally, let’s take a look at the [main.py]. I cropped some of the code for a better readability.

  • Run the main() function in an async loop
  • Connect the module to Azure IoT based on the environment context information
  • Start a listener for input messages
  • Stop the module in local mode, when the letter Q is pressed
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for
# full license information.

import time
import os
import sys
import asyncio
from six.moves import input
import threading
from azure.iot.device.aio import IoTHubModuleClient

async def main():
        if not sys.version >= "3.5.3":
            raise Exception( "The sample requires python 3.5.3+. Current version of Python: %s" % sys.version )
        print ( "IoT Hub Client for Python" )

        # The client object is used to interact with your Azure IoT hub.
        module_client = IoTHubModuleClient.create_from_edge_environment()

        # connect the client.
        await module_client.connect()

        # define behavior for receiving an input message on input1
        async def input1_listener(module_client):

        # define behavior for halting the application
        def stdin_listener():

        # Schedule task for C2D Listener
        listeners = asyncio.gather(input1_listener(module_client))

        print ( "The sample is now waiting for messages. ")

        # Run the stdin listener in the event loop
        loop = asyncio.get_event_loop()
        user_finished = loop.run_in_executor(None, stdin_listener)

        # Wait for user to indicate they are done listening for messages
        await user_finished

        # Cancel listening

        # Finally, disconnect
        await module_client.disconnect()

    except Exception as e:
        print ( "Unexpected error %s " % e )

if __name__ == "__main__":
    loop = asyncio.get_event_loop()

    # If using Python 3.7 or above, you can use following code instead:
    # asyncio.run(main())

This is a good start to merge the docker custom drawing files with the zure IoT project. I’ll start this on the next post.


Happy coding!


El Bruno

¿Con ganas de ponerte al día?

En Lemoncode te ofrecemos formación online impartida por profesionales que se baten el cobre en consultoría:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.