1

I setup azure, created iot hub, device, topic and subscription. Now I want to send a message from a python script via mqtt and see it appear somehow on the azure page

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")

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.username_pw_set(username=iot_hub_name+".azure-devices.net/" + device_id, password=sas_token)

client.tls_set(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.publish("devices/" + device_id + "/messages/events/", "{id=123}", qos=1)
client.loop_forever()

The problem is that I don't know how to specify the topic. When I tried this

client.publish("devices/" + device_id + "/mytopicname/messages", "{id=123}", qos=1)

it disconnects with code 1.

2 Answers 2

2

IoT Hub is not a general-purpose pub-sub messaging broker, it only supports the documented topic names and topic filters. Please refer to this document(Communicate with your IoT hub using the MQTT protocol).

So you can not publish a custom topic when you want to connect to IoT Hub with MQTT directly.

Sign up to request clarification or add additional context in comments.

2 Comments

How should I do that then? According to the documentation it seemed to be possible to me Sending device-to-cloud messages After making a successful connection, a device can send messages to IoT Hub using devices/{device_id}/messages/events/ or devices/{device_id}/messages/events/{property_bag} as a Topic Name. The {property_bag} element enables the device to send messages with additional properties in a url-encoded format.
I think you can choose Azure Service Bus instead of IoT Hub.You can also use topics to send and receive messages.Topics can have multiple, independent subscriptions.You can refer to this document about How to use Service Bus queues with Python.
0

Code 1 means that you are using different protocol version. Newest version sometime has different header (for example added an additional field).

Exact meaning of code 1: " Connection Refused, unacceptable protocol version"

4 Comments

Ok, how about sending to topic?
Code 1 you are mentioned in question regards to connection, not to publishing ("4.3 Payload The PUBACK Packet has no payload."). It is not possible to publish when client is not connected. The other possibility is that you are exceeding keep alive time
If I do not change the code, it connects with code 0. And a message seems to be send. But how to specify a topic?
Have you tried with simpler topic name? For example "client.publish("devices/test")? Can you should body of "publish" function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.