skip to Main Content

I’m having an issue trying to execute a very simple python script that works perfectly in Windows but not on my Le Potato (Debian):

import uuid
from azure.iot.hub import IoTHubRegistryManager
import MqttClient as mqttClient


CONNECTION_STRING = "HostName=(...)"
DEVICE_ID = "(...)"
registry_manager = IoTHubRegistryManager(CONNECTION_STRING)

def send_message(msg):
    props = {}
    props.update(messageId = str(uuid.uuid4()))
    props.update(contentType = "application/json")
    props.update()
    registry_manager.send_c2d_message(DEVICE_ID, msg, properties=props)


def on_message(mqttc, obj, msg):
    print("[" + msg.topic+"]: "+str(msg.payload))
    try:
        send_message(str(msg.payload))
    except Exception as e:
        print("An exception occurred:", str(e))

mqttClient.init(on_message=on_message)

When running python myscript.py I get the error ModuleNotFoundError: No module named 'azure.iot.hub'

I installed everything several times:

  • pip install azure-iot-hub

  • pip install azure-iot-device

And I even created my own virtual environment with a fresh install and tried the same exact versions in Windows.

2

Answers


  1. Using this reference MSDOC able to Send cloud-to-device messages.

    enter image description here

    • Install pip install azure-iot-hub , pip3 install azure-iot-hub andpip3 install uuid
    • With pip list we can check the respective module is installed .

    enter image description here

    enter image description here

    Code:

    import  random
    import  uuid
    from  azure.iot.hub  import  IoTHubRegistryManager
    CONNECTION_STRING = "HostName=sampath23sa454.azure-devices.net;SharedAccessKeyName=iot"
    DEVICE_ID = "sampath23"
    registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
    def  send_message(msg):
    props = {
    "messageId":  str(uuid.uuid4()),
    "contentType":  "application/json"
    }
    registry_manager.send_c2d_message(DEVICE_ID,  msg,  properties=props)
    print("Message sent successfully!")
    print("Message content:",  msg)
    def  on_message(mqttc,  obj,  msg):
    print("[" + msg.topic + "]: " + str(msg.payload))
    try:
    send_message(str(msg.payload))
    except  Exception  as  e:
    print("An exception occurred:",  str(e))
    class  MqttClient:
    @staticmethod
    def  init(on_message):
    pass
    mqttClient = MqttClient()
    mqttClient.init(on_message=on_message)
    if  __name__ == '__main__':
    print("Starting the IoT Hub C2D Messaging sample...")
    message_to_send = "Hello from the cloud!"
    send_message(message_to_send)
    input("Press Enter to stop...n")
    print("IoT Hub C2D Messaging sample stopped")
    

    Output:

    enter image description here

    Code:

    mqttClient = MqttClient()
    mqttClient.init(on_message=on_message)
    if  __name__ == '__main__':
    print("Starting the IoT Hub C2D Messaging sample...")
    random_messages = ["Temperature: 25°C",  "Humidity: 50%",  "Pressure: 1013 hPa"]
    for  _  in  range(5):  # Sending 5 random messages
    random_message = random.choice(random_messages)
    send_message(random_message)
    input("Press Enter to stop...n")
    print("IoT Hub C2D Messaging sample stopped")
    

    Output:

    enter image description here

    Login or Signup to reply.
  2. linux see root and non-root as different users, are you running your python code at the same user you installed these dependencies?
    for example if you run it by sudo, you need to reinstall those libraries on root too.

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