Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created August 10, 2021 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/791b21157aef717fa16008d0cd7a9c30 to your computer and use it in GitHub Desktop.
Save elbruno/791b21157aef717fa16008d0cd7a9c30 to your computer and use it in GitHub Desktop.
azureiotgrovemodulesendmessage.py
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment