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

Buy Me A Coffee

Hi !

In my previous post I shared some details on how to configure the Azure IoT Module to interact with the sensors / servos / LEDs connected to the Raspberry Pi. I also shared the base code to send telemetry. Let’s add some more details related to the Send Telemetry part.

Send Telemetry

In the code sample below:

  • 2 imports are required,
    • azure.iot.device.aio import IoTHubModuleClient, to interact with the Azure IoT Module
    • azure.iot.device import Message, to create the Azure IoT Hub message to be sent
  • A JSON message is created with the 3 door properties, and added as the message body. Lines 13 to 15.
  • The message adds also 3 custom properties [action, door state and door state description]. Lines 15 to 18.

# 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-

Sample Messages

This is a sample of 2 messages received and processed in Azure IoT. First message to open the door, second message to close the door.

Tue Aug 17 2021 09:18:00 GMT-0400 (Eastern Daylight Time):
{
  "body": {
    "action": "Door closed",
    "doorState": "0",
    "doorStateDesc": "closed"
  },
  "enqueuedTime": "Tue Aug 17 2021 09:18:00 GMT-0400 (Eastern Daylight Time)",
  "properties": {
    "action": "Door closed",
    "doorState": "0",
    "doorStateDesc": "closed"
  },
  "systemProperties": {
    "iothub-connection-device-id": "rpiIoTDoor2",
    "iothub-connection-module-id": "DoorController",
    "iothub-connection-auth-method": "{\"scope\":\"module\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
    "iothub-connection-auth-generation-id": "637637771398106938",
    "iothub-enqueuedtime": 1629206280400,
    "iothub-message-source": "Telemetry",
    "x-opt-sequence-number": 71,
    "x-opt-offset": "4294968816",
    "x-opt-enqueued-time": 1629206280463
  }
}
Tue Aug 17 2021 09:17:51 GMT-0400 (Eastern Daylight Time):
{
  "body": {
    "action": "Door opened",
    "doorState": "1",
    "doorStateDesc": "open"
  },
  "enqueuedTime": "Tue Aug 17 2021 09:17:51 GMT-0400 (Eastern Daylight Time)",
  "properties": {
    "action": "Door opened",
    "doorState": "1",
    "doorStateDesc": "open"
  },
  "systemProperties": {
    "iothub-connection-device-id": "rpiIoTDoor2",
    "iothub-connection-module-id": "DoorController",
    "iothub-connection-auth-method": "{\"scope\":\"module\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
    "iothub-connection-auth-generation-id": "637637771398106938",
    "iothub-enqueuedtime": 1629206271635,
    "iothub-message-source": "Telemetry",
    "x-opt-sequence-number": 70,
    "x-opt-offset": "4294968312",
    "x-opt-enqueued-time": 1629206271744
  }
}

References

Happy coding!

Greetings

El Bruno



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:

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 )

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.

%d bloggers like this: