async def main(): # The "Authentication Provider" is the object in charge of creating authentication "tokens" for the device client. auth_provider = auth.from_environment() # For now, the SDK only supports MQTT as a protocol. the client object is used to interact with your Azure IoT hub. # It needs an Authentication Provider to secure the communication with the hub, using either tokens or x509 certificates device_client = IoTHubModuleClient.from_authentication_provider( auth_provider, "mqtt") # Connect the client. await device_client.connect() async def send_test_message(i): print("sending message #" + str(i)) msg = Message("test wind speed " + str(i)) msg.message_id = uuid.uuid4() msg.correlation_id = "correlation-1234" msg.custom_properties["tornado-warning"] = "yes" await device_client.send_event(msg) print("done sending message #" + str(i)) # send `messages_to_send` messages in parallel await asyncio.gather( *[send_test_message(i) for i in range(1, messages_to_send + 1)]) # finally, disconnect await device_client.disconnect()
def connect_from_environment(self, transport_type): print("connecting from environment") auth_provider = auth.from_environment() self.client = IoTHubModuleClient.from_authentication_provider( auth_provider, transport_type ) async_helper.run_coroutine_sync(self.client.connect())
async def main(): # The "Authentication Provider" is the object in charge of creating authentication "tokens" for the module client. auth_provider = auth.from_environment() # For now, the SDK only supports MQTT as a protocol. # Inputs/Ouputs are only supported in the context of Azure IoT Edge and module client # The module client object acts as an Azure IoT Edge module and interacts with an Azure IoT Edge hub # It needs an Authentication Provider to secure the communication with the Edge hub. # This authentication provider is created from environment & delegates token generation to iotedged. module_client = IoTHubModuleClient.from_authentication_provider(auth_provider, "mqtt") # Connect the client. await module_client.connect() # Send a filled out Message object async def send_test_message(i): print("sending message #" + str(i)) msg = Message("test wind speed " + str(i)) msg.message_id = uuid.uuid4() msg.correlation_id = "correlation-1234" msg.custom_properties["tornado-warning"] = "yes" await module_client.send_to_output(msg, "twister") print("done sending message #" + str(i)) await asyncio.gather(*[send_test_message(i) for i in range(1, messages_to_send)]) # finally, disconnect module_client.disconnect()
def connect(self, transport_type, connection_string, cert): print("connecting using " + transport_type) auth_provider = auth.from_connection_string(connection_string) if "GatewayHostName" in connection_string: auth_provider.ca_cert = cert self.client = IoTHubModuleClient.from_authentication_provider( auth_provider, transport_type ) async_helper.run_coroutine_sync(self.client.connect())
async def main(): # The "Authentication Provider" is the object in charge of creating authentication "tokens" for the device client. auth_provider = auth.from_environment() # For now, the SDK only supports MQTT as a protocol. the client object is used to interact with your Azure IoT hub. # It needs an Authentication Provider to secure the communication with the hub, using either tokens or x509 certificates module_client = IoTHubModuleClient.from_authentication_provider( auth_provider, "mqtt") # connect the client. await module_client.connect() # define behavior for receiving an input message on input1 async def input1_listener(module_client): while True: input_message = await module_client.receive_input_message( "input1") # blocking call print("the data in the message received on input1 was ") print(input_message.data) print("custom properties are") print(input_message.custom_properties) # define behavior for receiving an input message on input2 async def input2_listener(module_client): while True: input_message = await module_client.receive_input_message( "input2") # blocking call print("the data in the message received on input2 was ") print(input_message.data) print("custom properties are") print(input_message.custom_properties) # define behavior for halting the application def stdin_listener(): while True: selection = input("Press Q to quit\n") if selection == "Q" or selection == "q": print("Quitting...") break # Schedule task for C2D Listener listeners = asyncio.gather(input1_listener(module_client), input2_listener(module_client)) # Run the stdin listener in the event loop loop = asyncio.get_running_loop() user_finished = loop.run_in_executor(None, stdin_listener) # Wait for user to indicate they are done listening for messages await user_finished # Cancel listening listeners.cancel() # Finally, disconnect await module_client.disconnect()