Azure IoT Hub Python device constantly crashing

CogniTech 20 Reputation points
2024-02-06T16:11:15.7733333+00:00

Hi all. We have an existing program that uses Intel Neural Compute stick to classify the stream from a USB camera and send messages to a broker. Previously we used our own MQTT broker and this device would run for months with no issues. We have swapped out the MQTT broker for IoT hub and it constantly crashes rendering the device unresponsive and the only solution is to restart the device, run the program again and it all repeats. This is quite concerning considering we have previously run this exact same software prior to the addition of Azure with absolutely no issues. The main program will check the provisioning state and then connect to the IoT hub, two threads are created, one for the reading of the camera stream and one for the streaming of the frames to a local web server where the stream can be monitored.

When it doesn't crash the speed of the stream is massively reduced, where as with our own MQTT broker the feed was near real-time, now we have replaced that with IoT Hub the stream is about 20 seconds behind, very jittery, and constantly freezes. We only send a message every 2 minutes so only introducing Azure is causing this. Errors like this constantly happen: {'_id': '27af74bb-5d6b-4933-b67a-0e42be9afc70', 'Data': [{'Sensor': 'Camera', 'Data': {'Dected': '0'}}], 'State': {'CPU': '24.1', 'Memory': '15.3', 'Diskspace': '41.9', 'CPUTemp': '59.887', 'IP': '192.168.1.97', 'MAC': 'dc:a6:32:75:89:a1'}, 'Location': {'Longitude': '-3.0833', 'Latitude': '53.1667'}} WARNING:azure.iot.device.common.pipeline.pipeline_stages_base:ConnectionStateStage: DisconnectEvent received while in unexpected state - ConnectionState.DISCONNECTED WARNING:azure.iot.device.common.handle_exceptions:Exception caught in background thread. Unable to handle. WARNING:azure.iot.device.common.handle_exceptions:['azure.iot.device.common.transport_exceptions.ConnectionDroppedError: Unexpected disconnection\n'] WARNING:azure.iot.device.common.handle_exceptions:Exception caught in background thread. Unable to handle. WARNING:azure.iot.device.common.handle_exceptions:['azure.iot.device.common.transport_exceptions.ConnectionDroppedError: Unexpected disconnection\n'] WARNING:azure.iot.device.common.handle_exceptions:Exception caught in background thread. Unable to handle. WARNING:azure.iot.device.common.handle_exceptions:['azure.iot.device.common.transport_exceptions.ConnectionFailedError\n'] WARNING:azure.iot.device.common.handle_exceptions:Exception caught in background thread. Unable to handle. WARNING:azure.iot.device.common.handle_exceptions:['azure.iot.device.common.transport_exceptions.ConnectionFailedError\n'] WARNING:azure.iot.device.common.handle_exceptions:Exception caught in background thread. Unable to handle. WARNING:azure.iot.device.common.handle_exceptions:['azure.iot.device.common.transport_exceptions.ConnectionFailedError\n'] WARNING:azure.iot.device.common.handle_exceptions:Exception caught in background thread. Unable to handle. WARNING:azure.iot.device.common.handle_exceptions:['azure.iot.device.common.transport_exceptions.ConnectionFailedError\n'] Message sent to IntelliConnect IoT We would like to find a solution for this issue if possible.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,272 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 36,761 Reputation points MVP Volunteer Moderator
    2024-02-06T23:05:34.6933333+00:00

    Hello @CogniTech ,

    welcome to this moderated Azure community forum.

    From the messages you share, many errors are related to connecting and disconnecting.

    Azure IoT Hub communicates over MQTT with a limited set of predefined topics due the tight relationship with the DeviceTwin.

    I recommend testing the connection first without the camera part to be sure the connection itself works as expected. If needed, this blog post explains how to connect with python using the SDK.

    Are you sure the DeviceClient tries to keep the connection open or is it connecting and disconnecting for each message? Have you tried MQTT over websockets too (less depending on MQTT related firewall rules)?

    As an alternative, you could try the new Azure MQTT broker based on Azure EventGrid. A video about how to use it can be found here.


    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.


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.