I am Not able to connect my .crt and .key file to Azure IoT Hub, it give connection error while my file path and host is correct

Indy 5 Reputation points
2024-05-01T11:43:22.2266667+00:00

User's image

import os
import asyncio
from azure.iot.device.aio import IoTHubDeviceClient
from azure.iot.device import X509

x509_cert = X509(
    cert_file="E:\\Repo\\RRR_1.crt",
    key_file="E:\\Repo\\RRR_1.key"
    # cert_file="20003C_1.crt",
    # key_file="20003C_1.key",
)

async def main():
    # Fetch the connection string from an environment variable
    # conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")

    # Create instance of the device client using the authentication provider
    device_client = IoTHubDeviceClient.create_from_x509_certificate(device_id='RRR_1',hostname='monoiothub.azure-devices.net',x509=x509_cert)

    # Connect the device client.
    
    await device_client.connect()

    # Send a single message
    print("Sending message...")
    await device_client.send_message("This is a message sent")
    print("Message successfully sent!")

    # finally, shut down the client
    await device_client.shutdown()


if __name__ == "__main__":
    asyncio.run(main())
Azure IoT
Azure IoT
A category of Azure services for internet of things devices.
401 questions
Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
364 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,205 questions
0 comments No comments
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.