다음을 통해 공유

파이썬을 통해 azure iot hub에서 메시지 수신 오류

solution-lab 0 평판 포인트
2024-05-10T02:06:10.41+00:00
from azure.iot.device import IoTHubDeviceClient, Message
import threading
import time

CONNECTION_STRING = "HostName=ID.azure-devices.net;DeviceId=sample;SharedAccessKey=???"

def message_handler(message):
    print("Received message from Azure IoT Hub:")
    print(message.data.decode("utf-8"))
    print("")

def main():
    client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
    client.connect()

    # 메시지 핸들러 설정
    client.on_message_received = message_handler

    try:
        # 메인 스레드가 종료되지 않도록 유지
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("IoTHubDeviceClient sample stopped")

if __name__ == "__main__":
    main()

오류 메시지는 다음과 같습니다 .

Exception caught in background thread. Unable to handle.

['azure.iot.device.common.transport_exceptions.ConnectionDroppedError: Unexpected disconnection\n']

코드가 잘못되었거나, 추가적인 방법이 있나요?

Exchange | Exchange Server | 기타
비즈니스용 Windows | Windows Server | 사용자 환경 | 기타
비즈니스용 Windows | IT 전문가용 Windows 클라이언트 | 사용자 환경 | 기타
커뮤니티 센터 | Q&A 사이트 논의 | Q&A 시작
댓글 0개 설명 없음
투표 {count}개

답변

질문 작성자가 수락한 답변이라고 답변에 표시할 수 있으며, 이를 통해 작성자의 문제를 해결한 답변을 사용자가 알 수 있도록 도와줍니다.