azureiotgrovemodulesendmessage.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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