0
votes

i was able to open a listener on iothub messages (messages from cloud to deivce) but i am unable to subscribe to direct method . I am trying to use mqtt support , not the iothub library

I have followed https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-direct-methods#mqtt but there must be a small detail that i haven't managed to do correctly yet

this is my code (python)

from paho.mqtt import client as mqtt
import ssl
import token_generator #a script that exports the token , it is working fine for device messages

path_to_root_cert = "cert.cer"
device_id = "mydevice_id "
endpoint ="myiot_hub_name.azure-devices.net/devices/mydevice_id "
policyKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
sas_token =  token_generator.generate_sas_token(endpoint ,policyKey ,None)
iot_hub_name = "myiot_hub_name"

def on_connect(client, userdata, flags, rc):
  print ("Device connected with result code: " + str(rc))
def on_disconnect(client, userdata, rc):
  print ("Device disconnected with result code: " + str(rc))
def on_publish(client, userdata, mid):
  print ("Device sent message")
def on_message(client, userdata, msg):
    print("Message received at: " + msg.topic+" with payload: "+str(msg.payload))

client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv311)

client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
client.on_message = on_message

client.username_pw_set(username=iot_hub_name+".azure-devices.net/" + device_id, password=sas_token)

client.tls_set(ca_certs=path_to_root_cert, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None)
client.tls_insecure_set(False)

client.connect(iot_hub_name+".azure-devices.net", port=8883 )

client.subscribe("iothub/methods/POST/")

client.loop_forever()
2

2 Answers

1
votes

the client should be subscribed on the topic:

$iothub/methods/POST/#

the following are others topics for subscribing:

$iothub/twin/res/#
$iothub/twin/PATCH/properties/desired/#
devices/{myDeviceId}/messages/devicebound/#
devices/{myDeviceId}/modules/{myModuleId}/messages/devicebound/#

and for device streams (currently in the preview)

$iothub/streams/POST/#
1
votes

I faced also such a problem. After spending some time I found out that the actual problem is in the python example.

According to the documentation the username should be {iothubhostname}/{device_id}/?api-version=2018-06-30 but in the example the version is missing. As result if you try to subscribe to any topic starting from $iothub it doesn't work.

After fixing the username everything works fine for me.