So, I created IoT hub, device, topic and subscription post setting up my Azure. Now I want to send a message via mqtt using a python script. What should I do to see it on the azure page?
This is my code.
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 issue being, I don't know how to specify the topic.
client.publish("devices/" + device_id + "/mytopicname/messages", "{id=123}", qos=1)
This thing I tried disconnects with code 1.