I am developing a python app that should receive mqtt messages. I'm using paho-mqtt library.
After setting the username/password and the callback function on_message
, I subscribe to the topic and make the connection to the broker. Then I start the loop_forever()
.
Here is my code:
import paho.mqtt.client as mqtt
MQTT_TOPIC = "my_topic"
BROKER_ENDPOINT = "my_broker_url"
BROKER_PORT = my_port
BROKER_USERNAME = "my_username"
BROKER_PASSWORD = "my_password"
mqtt_client = mqtt.Client()
def on_message(client, userdata, message):
print("Message Recieved from broker: " + message.payload)
def main():
mqtt_client.username_pw_set(username=BROKER_USERNAME, password=BROKER_PASSWORD)
mqtt_client.on_message = on_message
mqtt_client.connect(BROKER_ENDPOINT, BROKER_PORT)
mqtt_client.subscribe(MQTT_TOPIC)
mqtt_client.loop_forever()
if __name__ == '__main__':
main()
I'm testing this code with the mosquitto_pub
command:
mosquitto_pub -h my_broker_url -p my_port -u my_username -P my_password -t 'my_topic' -m "Hello World"
But I cannot see any messages received by my application.
What am i doing wrong ? Thanks for helping