
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.

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
WORKDIR /app
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():
try:
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
listeners.cancel()
# Finally, disconnect
await module_client.disconnect()
except Exception as e:
print ( "Unexpected error %s " % e )
raise
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
# 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.
Resources
- Azure Custom Vision
- Azure Custom Vision Documentation
- Azure IoT Tools
- Deploy your first IoT Edge module to a virtual Linux device
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno
Azure ☁ IoT
- Install ☁ Azure IoT on Raspberry Pi
- Deploy ☁ Azure Blob Storage on IoT Edge, lessons learned
- Connect to ☁ Azure Blob Storage on IoT Edge using Microsoft Azure Storage Explorer
- Lesson learned and tips on how to install Azure IoT Edge on Ubuntu on a Raspberry Pi
- Azure IoT Explorer, in preview and awesome
- Mapping a local ☁ Azure IoT Edge folder module with an Edge device folder 📁
- Creating a folder 📂 in the docker definition in an ☁ Azure IoT Edge
- Granting access to Raspberry Pi GPIO from an ☁ Azure IoT Edge Module
Create an Azure IoT Module using Raspberry Pi and Grove Sensors
- Raspberry Pi + Grove Sensors, read temperature and humidity values
- Raspberry Pi + Grove Sensors, send temperature and humidity values as telemetry to Azure IoT Hub
- Raspberry Pi + Grove Sensors, create a Azure IoT Module to send temperature and humidity values as telemetry to Azure IoT Hub
- Raspberry Pi + Grove Sensors, publish and use the Azure IoT Module
- Raspberry Pi + Grove Sensors, notes on build and configuration
- Raspberry Pi + Grove Sensors, details on how to send a telemetry message and sample messages
Create an Azure IoT Module from Azure Custom Vision project
- Create and export a Custom Vision Project as Docker image
- Analyze the content of the CV Docker image
- Create and analyze an Azure IoT Module
- Merge the CV project as an Azure IoT Module
- Deploy to an Azure IoT device and test the CV module
- Send telemetry for each analyzed image
- Add digital twin configuration to the Azure IoT module (coming soon)
¿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:
- Si tienes ganas de ponerte al día con Front End (ES6, Typescript, React, Angular, Vuejs…) te recomendamos nuestros Máster Front End: https://lemoncode.net/master-frontend#inicio-banner
- Si te quieres poner al día en Backend (stacks .net y nodejs), te aconsejamos nuestro Bootcamp Backend: https://lemoncode.net/bootcamp-backend#bootcamp-backend/banner
- Y si tienes ganas de meterte con Docker, Kubernetes, CI/CD…, tenemos nuestro Bootcamp Devops: https://lemoncode.net/bootcamp-devops#bootcamp-devops/inicio
1 comment