#AzureIoT – Using a Raspberry Pi Grove Sensor in an ☁ Azure IoT Edge Module (5/N)

Buy Me A Coffee

Hi !

So, as a final post, here are some notes related to the Container Create option and also how to send telemetry from the device.

Container Create Option

This is the Container Create Option configuration that I define for the module. This specific configuration allows the access to the i2c components (raspberry pi pins) and also opens a port 8066 that will be used in the module when it’s exposed as a webserver.

# Container Create Options

{
    "HostConfig": {
        "Binds": [
            "/dev/i2c-1:/dev/i2c-1"
        ],
        "Privileged": true,
        "ExposedPorts": {
            "8066/tcp": {}
        },
        "PortBindings": {
            "8066/tcp": [
                {
                    "HostPort": "8066"
                }
            ]
        }
    }
}


Sending Telemetry

Most of the code samples I founded online on how to send telemetry / messages are for IoT apps, not for Custom IoT Edge modules. So here is the lines you need to send a message / telemetry from the custom iot edge module.

In this sample, the message uses 3 properties action, door state and door state description. And the message create a JSON message with this properties and also add them as custom properties in the sent message.

# Azure IoT imports
from azure.iot.device.aio import IoTHubModuleClient
from azure.iot.device import Message
async def send_iot_message_doorState(action, doorState, doorStateDesc):
# send iot messages
# send message reference https://docs.microsoft.com/en-us/python/api/azure-iot-device/azure.iot.device.aio.iothubmoduleclient?view=azure-python#send-message-message-
global module_client
try:
MSG_TXT = '{{"action": "{action}","doorState": "{doorState}","doorStateDesc": "{doorStateDesc}"}}'
msg_txt_json_formatted = MSG_TXT.format(action=action, doorState=doorState, doorStateDesc=doorStateDesc)
message = Message(msg_txt_json_formatted)
message.custom_properties['action'] = action
message.custom_properties['doorState'] = doorState
message.custom_properties['doorStateDesc'] = doorStateDesc
await module_client.send_message(message)
except Exception as e:
return 'Error sending IoT message', 500

To have more details on the class modules and properties, I checked

https://docs.microsoft.com/en-us/python/api/azure-iot-device/azure.iot.device.aio.iothubmoduleclient?view=azure-python#send-message-message-

Docker Image build

This is the docker file for the raspberry pi

FROM arm32v7/python:3.7-slim-buster

WORKDIR /app

# custom installation for RPI Grove dependencies defined in requirements.txt
RUN apt-get update 
RUN apt-get install -y gcc

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

# Code added to support web app in the Azure IoT Module
RUN pip install flask pillow --index-url 'https://www.piwheels.org/simple'

# Expose the port
EXPOSE 8066

COPY . .

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

My requirement file is super simple

azure-iot-device~=2.7.0
RPi.GPIO
grove.py
seeed-python-dht

And I think this covers the most specific setups for this one !

References

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno



Azure ☁ IoT

Create an Azure IoT Module using Raspberry Pi and Grove Sensors

  1. Raspberry Pi + Grove Sensors, read temperature and humidity values
  2. Raspberry Pi + Grove Sensors, send temperature and humidity values as telemetry to Azure IoT Hub
  3. Raspberry Pi + Grove Sensors, create a Azure IoT Module to send temperature and humidity values as telemetry to Azure IoT Hub
  4. Raspberry Pi + Grove Sensors, publish and use the Azure IoT Module
  5. Raspberry Pi + Grove Sensors, notes on build and configuration
  6. 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


¿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:

Advertisement

Leave a comment

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 )

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.

%d bloggers like this: