async def main():
    hostname = os.getenv("HOSTNAME")

    # The device having a certain module that has been created on the portal
    # using X509 CA signing or Self signing capabilities

    device_id = os.getenv("DEVICE_ID")
    module_id = os.getenv("MODULE_ID")

    x509 = X509(
        cert_file=os.getenv("X509_CERT_FILE"),
        key_file=os.getenv("X509_KEY_FILE"),
        pass_phrase=os.getenv("PASS_PHRASE"),
    )

    module_client = IoTHubModuleClient.create_from_x509_certificate(
        hostname=hostname, x509=x509, device_id=device_id, module_id=module_id
    )

    # Connect the client.
    await module_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 module_client.send_d2c_message(msg)
        print("done sending message #" + str(i))

    await asyncio.gather(*[send_test_message(i) for i in range(1, messages_to_send + 1)])

    # finally, disconnect
    await module_client.disconnect()
async def result_from_register(registration_id, device_cert_file, device_key_file):
    x509 = X509(cert_file=device_cert_file, key_file=device_key_file, pass_phrase=device_password)

    provisioning_device_client = ProvisioningDeviceClient.create_from_x509_certificate(
        provisioning_host=PROVISIONING_HOST,
        registration_id=registration_id,
        id_scope=ID_SCOPE,
        x509=x509,
    )

    return await provisioning_device_client.register()
示例#3
0
async def result_from_register(registration_id, device_cert_file, device_key_file, protocol):
    x509 = X509(cert_file=device_cert_file, key_file=device_key_file, pass_phrase=device_password)
    protocol_boolean_mapping = {"mqtt": False, "mqttws": True}
    provisioning_device_client = ProvisioningDeviceClient.create_from_x509_certificate(
        provisioning_host=PROVISIONING_HOST,
        registration_id=registration_id,
        id_scope=ID_SCOPE,
        x509=x509,
        websockets=protocol_boolean_mapping[protocol],
    )

    return await provisioning_device_client.register()
示例#4
0
    async def register_device():
        x509 = X509(
            cert_file=os.getenv("X509_CERT_FILE"),
            key_file=os.getenv("X509_KEY_FILE"),
            pass_phrase=os.getenv("PASS_PHRASE"),
        )
        provisioning_device_client = ProvisioningDeviceClient.create_from_x509_certificate(
            provisioning_host=provisioning_host,
            registration_id=registration_id,
            id_scope=id_scope,
            x509=x509,
        )

        await provisioning_device_client.register()
async def main():
    hostname = os.getenv("HOSTNAME")
    # The device that has been created on the portal using X509 CA signing or Self signing capabilities
    device_id = os.getenv("DEVICE_ID")

    x509 = X509(
        cert_file=os.getenv("X509_CERT_FILE"),
        key_file=os.getenv("X509_KEY_FILE"),
        pass_phrase=os.getenv("PASS_PHRASE"),
    )

    # The client object is used to interact with your Azure IoT hub.
    device_client = IoTHubDeviceClient.create_from_x509_certificate(
        hostname=hostname, device_id=device_id, x509=x509)

    # define behavior for receiving a C2D message
    async def c2d_listener(device_client):
        while True:
            c2d_message = await device_client.receive_c2d_message(
            )  # blocking call
            print("the data in the message received was ")
            print(c2d_message.data)
            print("custom properties are")
            print(c2d_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
    asyncio.create_task(c2d_listener(device_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

    # Finally, disconnect
    await device_client.disconnect()
import logging

logging.basicConfig(level=logging.ERROR)

hostname = os.getenv("HOSTNAME")

# The device having a certain module that has been created on the portal
# using X509 CA signing or Self signing capabilities
# The <device_id>\<module_id> should be the common name of the certificate

device_id = os.getenv("DEVICE_ID")
module_id = os.getenv("MODULE_ID")

x509 = X509(
    cert_file=os.getenv("X509_CERT_FILE"),
    key_file=os.getenv("X509_KEY_FILE"),
    pass_phrase=os.getenv("PASS_PHRASE"),
)

module_client = IoTHubModuleClient.create_from_x509_certificate(
    hostname=hostname, x509=x509, device_id=device_id, module_id=module_id)

module_client.connect()

# send 5 messages with a 1 second pause between each message
for i in range(1, 6):
    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"