Hi friends,
I obtained recently a Raspberry Pi 4 and started working on small exercices from this wonderful book : Learn Robotics with Raspberry Pi.
I discoverd the power of Python and its simplicity and I am in love with this new language.
On the other hand, Azure IoT Hub is fascinating me and I decided to connect my Raspberry Pi to Azure making it an Azure IoT Hub Edge device. This guide is well done in order to achieve my objective.
And I can see my Raspberry Pi connetced to Aure IoT Hub:

Make sure that the runtime response is OK!
Dokcer is here! Yes, everything running inside your edge device is a module in a container. After joining your device to Azure IoT hub, you have by default 2 modules: the agent and the runtime.

Both of them are running in containers:


Now, let’s get a small project to play with. We want to implement an IoT edge device with a temperature module. Another module will be also developed in order to receive messages from the temperature module and manages two leds. The red led is on when the temperatre is above a threshold. Otherwise, the green led is on. The latest module will also send messages to Azure IoT hub for analysis. (This is another topic that would be covered in a next post incha Ellah).
In my case, I used the simulated temperature module and developed a custom module that intercepts messages from the first module. To manage the leds, I have used this circuit based on a breadboard, 2 leds and 2 resistors:


Let’s develop our custom module following the steps described in this article. Some changes are necessary:
- In the main.py file, we have to add these imports and define the leds variables
import timeimport gpiozeroimport osimport sysimport asynciofrom six.moves import inputimport threadingfrom azure.iot.device.aio import IoTHubModuleClientimport jsonled1 = gpiozero.LED(8)led2 = gpiozero.LED(21)
- In the main.py file, I changed the code as follows to manage my leds:
# define behavior for receiving an input message on input1async def input1_listener(module_client):global RECEIVED_MESSAGESglobal TEMPERATURE_THRESHOLDwhile True:try:time.sleep(100)RECEIVED_MESSAGES=0input_message = await module_client.receive_message_on_input(“input1”) # blocking callmessage = input_message.datasize = len(message)message_text = message.decode(‘utf-8’)print ( ” Data: <<<%s>>> & Size=%d” % (message_text, size) )custom_properties = input_message.custom_properties#print ( ” Properties: %s” % custom_properties )RECEIVED_MESSAGES += 1print ( ” Total messages received: %d” % RECEIVED_MESSAGES )data = json.loads(message_text)await module_client.send_message_to_output(input_message, “output1”)if “ambient” in data and “temperature” in data[“ambient”] and data[“ambient”][“temperature”] > TEMPERATURE_THRESHOLD:custom_properties[“MessageType”] = “Alert”print ( “ambient temperature %s exceeds threshold %s” % (data[“ambient”][“temperature”], TEMPERATURE_THRESHOLD))led1.on()led2.off()await module_client.send_message_to_output(“Red LED is ON!”, “output1”)elif “ambient” in data and “temperature” in data[“ambient”] and data[“ambient”][“temperature”] <= TEMPERATURE_THRESHOLD:led2.on()led1.off()await module_client.send_message_to_output(“Green LED is ON!”, “output1”)else :led2.on()led1.on()await module_client.send_message_to_output(“Green & Red LEDs are ON!”, “output1”)except Exception as ex:print ( “Unexpected error in input1_listener: %s” % ex )
- Two packages are added to the requirements.txt file:
azure-iot-device~=2.0.0gpiozero==1.5.1RPi.GPIO==0.7.0
- The DockerFile file has to be changed as follows (the gcc is used to compile rpi.gpio python library) :
FROM arm32v7/python:3.7-slim-buster
WORKDIR /app
COPY requirements.txt ./
RUN apt-get update
RUN apt-get -y install gcc
RUN pip install -r requirements.txtCOPY . .
CMD [ “python3”, “-u”, “./main.py” ]
- Finally, under the custom module, we hve to cofigure the privileged mode:
“modules”: {“SampleModule”: {“version”: “1.0”,“type”: “docker”,“status”: “running”,“restartPolicy”: “always”,“settings”: {“image”: “${MODULES.SampleModule}”,“createOptions”: {“HostConfig”: {“Privileged”: true}}}},
























































