skip to Main Content

I have an application on a device that connects to Azure IoT Hub using the C SDK. This application will send messages to the cloud and interact via the device twin.

I also need to have the device receive updates. It seems like the Azure Device Update agent application is the way to do this, certainly for their support of things like swupdate.

My question is can two applications connect to Azure IoT Hub? If so, is there something needed to be done so they don’t step on each others toes? If not, then is the device update agent the way to go, and if it is how do you also interact with IoT Hub for something like device twins?

I have two applications connecting to Azure IoT Hub. What I’m seeing when I run both my application and the device update agent is they are interfering with each others connection. When they normally print out they’re sending messages every few minutes when running alone, they alternate printing that they are resending a KEEPALIVE message every few seconds when both are running.

2

Answers


  1. When working with Azure IoT Hub, each device connection is identified and authenticated using a device ID and its associated security material (like a shared access key, certificate, etc.). A single device (identified by its device ID) should only have one active connection to the IoT Hub at any given time. If two applications try to connect using the same device ID simultaneously, they will interfere with each other’s connections, leading to issues like the ones you described.

    Given this, here is what I will recommend.

    1. Single Application for All Functions:
      Merge your two applications into a single application that handles both regular device functionality (sending messages, device twin interactions) and device updates. This is the most straightforward approach, ensuring only one connection to the IoT Hub.

    2. Use Module Identities:
      Azure IoT Edge devices support module identities, where a single device can have multiple modules, each with its own identity and connection to IoT Hub. If your device can run IoT Edge runtime, you can make each application a module, allowing them to connect to IoT Hub simultaneously without interference. However, setting up IoT Edge might be an overkill if you don’t need other Edge functionalities.

    Login or Signup to reply.
  2. I suppose you are using the same device connection string from two application to push the telemetry as well as reading device twin updates. The Azure IoT device connection string is meant to be used from a single device at a given time. If you use from two devices simultaneously, you will run into connection stability into issues and one of the devices will get disconnected with some errors.

    To overcome this, you can use the IoT Hub Service Connection string from the Shared Access Policies and authenticate the application using this string through IoTHubRegistryManager to access digital twin.

    Here is a sample implementation in Pyhton demonstrating this approach

    from azure.iot.hub import IoTHubRegistryManager
    
    # Connection string for the IoT Hub service shared access policy
    CONNECTION_STRING = "<ReplaceWithIoTHubServiceConnectionString>"
    DEVICE_ID = "<ReplaceWithDeviceID>"
    
    
    def get_device_twin():
        # Create an instance of the IoTHubRegistryManager
        registry_manager = IoTHubRegistryManager.from_connection_string(
            CONNECTION_STRING)
    
        try:
            # Get the device twin
            twin = registry_manager.get_twin(DEVICE_ID)
            print("Device Twin:")
            print(twin)
    
            desired_properties = twin.properties.desired
    
        # Process desired properties
            if desired_properties:
                print("Desired properties:")
                print(desired_properties)
            else:
                print("No desired properties found.")
    
        except Exception as e:
            print("Error getting device twin:", str(e))
    
    
    # Call the function to get the device twin
    get_device_twin()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search