Azure IoT Central Connection String Generator is not working- Kindly provide a solution

Pinakster 25 Reputation points
2025-01-05T14:32:55.82+00:00

The web site : https://gloveboxes.github.io/Turn-a-Command-Line-tool-into-a-REST-API-with-Azure-Functions/dps-cstr.html

does not return for a Device id, scope & device key. Screen is hanging..

Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
367 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sander van de Velde | MVP 34,766 Reputation points MVP
    2025-01-05T21:57:40.35+00:00

    Hello @Pinakster,

    welcome to this moderated Azure community forum.

    I recommend getting in touch with the creator https://github.com/gloveboxes if you have question about this repo.

    It is recommended to use the regular way to connect a IoT central devices based on the proper secrets instead of this shortcut.

    Behind IoT Central technology like Azure IoT Hub and Azure Device Provisioning Service are used.

    You could try it first with an example like this Python example.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Pinakster 25 Reputation points
    2025-01-06T14:10:02.6133333+00:00

    I tried this py code as below to get the connection string which worked.

    import os
    import asyncio
    
    from dotenv import load_dotenv
    
    from azure.iot.device.aio import IoTHubDeviceClient, ProvisioningDeviceClient
    
    # Load the environment variables
    ##load_dotenv()
    
    # Get the connection details from the .env file for Azure IoT Central
    ##ID_SCOPE = os.environ['ID_SCOPE']
    ##DEVICE_ID = os.environ['DEVICE_ID']
    ##PRIMARY_KEY = os.environ['PRIMARY_KEY']
    ID_SCOPE = "???????????"
    DEVICE_ID = "raspberrypi-1"
    PRIMARY_KEY = "????????????????????????????"
    
    async def connect_device() -> IoTHubDeviceClient:
        """Connects this device to IoT Central
        """
        # Connect to the device provisioning service and request the connection details
        # for the device
        provisioning_device_client = ProvisioningDeviceClient.create_from_symmetric_key(
            provisioning_host='global.azure-devices-provisioning.net',
            registration_id=DEVICE_ID,
            id_scope=ID_SCOPE,
            symmetric_key=PRIMARY_KEY)
        registration_result = await provisioning_device_client.register()
    
        # Build the connection string - this is used to connect to IoT Central
        conn_str = 'HostName=' + registration_result.registration_state.assigned_hub + \
                    ';DeviceId=' + DEVICE_ID + \
                    ';SharedAccessKey=' + PRIMARY_KEY
        print ("Connection string:", conn_str)
        # The device client object is used to interact with Azure IoT Central.
        device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
    
        # Connect to the IoT Central hub
        device_client.connect()
    
        # Return the device client
        return device_client
    
    def main():
        print("IoT Hub - connection string")
    
    
        # Instantiate the client. Use the same instance of the client for the duration of
        # your application
        client = asyncio.run(connect_device())
    
    if __name__ == '__main__':
        main()
    

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.