skip to Main Content

I have a few questions regarding best practices when using Azure IoT Edge devices.

I have a module, deployed through a deployment manifest on an edge device. This module handle messages and if needed, make a call to an API. This is working correctly. I want to send messages to that edge device and have that module handle them. I followed this tutorial and it seems to send messages without any issue.

However, I’m unsure about a few things:

My questions:

  1. How can I see messages sent? How can I route them to my module input?
    If I have a running module listening to messages it works alright (as it’s the case in the tutorial above, but what if it’s not running, or failed due to a network issue?

I tried playing a little bit by defining routes but I don’t think they’re intended for messages sent from the cloud.

  1. Should my module always be running ? I based it on this Azure docs tutorial The module is a python scripts and when it’s done, it simply exits. This is fine when setting the desired state to stopped and the restart policy to something other than always. However, the defaults are running and always

  2. If they don’t need to be always running, I thought about having a module responsible for handling messages and starting appropriate modules depending on these. Is that something common ? I’m wondering if I’m missing something

Thanks a lot for taking the time to read me!

2

Answers


  1. For the First question

    I have referred this MSDOC routes and endpoints ,for sending message and monitor-events.

    • For best practice use try and catch in sending a message IoT device. it tracks messages delivered to a module that isn’t running or fails because of a network problem and will be held in a queue until the module is back up. Messaging delivery is guaranteed with confidence by Azure IoT Edge.
      It’s wise to plan your strategy to deal with sporadic module availability and network outages. Utilise Azure IoT Edge’s message retry methods, buffering capabilities, and offline support.

    Sending a Message to the Module :

    
    import  logging
    from azure.iot.device  import  IoTHubDeviceClient
    logging.basicConfig(level=logging.INFO)
    connection_string = "connection string of the module"
    device_client = IoTHubDeviceClient.create_from_connection_string(connection_string)
    device_client.connect()
    message = "Hello, IoT Hub!"
    device_client.send_message(message)
    logging.info("Message sent: %s", message)
    device_client.disconnect()
    
    

    enter image description here

    az iot hub monitor-events -n {iothub_name} -d {device_id}
    
    

    enter image description here

    To send a message IoT device with try and except:

    
    from azure.iot.device  import  IoTHubDeviceClient, exceptions
    connection_string = "Device connection string"
    device_client = IoTHubDeviceClient.create_from_connection_string(connection_string)
    try:
    message = "Hello, IoT Hub!"
    device_client.send_message(message)
    print("Message sent successfully.")
    print("The message is : "+message)
    except  exceptions.ConnectionDroppedError:
    print("Failed to send message. Connection to IoT Hub dropped.")
    except  Exception  as  e:
    print("An error occurred while sending the message:", str(e))
    
    

    enter image description here

    enter image description here

    Note:

    While routing is enabled the message will send to the route point.

    Routing:

    enter image description here

    We can route the above endpoints. I Routed to Event Hub from the above doc and was able to capture the message of the IoT device.

    enter image description here

    Login or Signup to reply.
  2. In terms of wanting to see your messages from the IOT HUB side of things, try the Azure IOT Explorer. It will display the telemetry of any device, as well as have a few other bits of functionality (NOTE: Its in Preview!)

    https://learn.microsoft.com/en-us/azure/iot/howto-use-iot-explorer

    you need an IOT Hub connection string (not Device, not Module)
    [enter image description here]

    For development, you can grab the IOTHubOwner policy, but be careful where that goes!

    When you start the app, you will need this connection string, and when enabled, you can retrieve all the devices on the hub. when you select one, you can start subscribing to the telemetry:
    enter image description here
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search