Using Paho's message_callback_add() function, but no results
mqtt, paho, python
Solution
I called `loop_start` in the wrong place.
I moved the call to right after the connect statement and it now works.
Here is the snippet:
client_uniq = "pubclient_"+str(mypid)
mqttclient = paho.Client(client_uniq, False) #nocleanstart
mqttclient.connect(broker, port, 60)
mqttclient.loop_start()
mqttclient.subscribe("FM_WaterPump/Commands/#")
In the documentation on loop_start it alludes to calling `loop_start()` after or before connect though it should say immediately before or after to clarify.
Snippet of the documentation:
These functions implement a threaded interface to the network loop. Calling loop_start() once, before or after connect*(), runs a thread in the background to call loop() automatically. This frees up the main thread for other work that may be blocking. This call also handles reconnecting to the broker. Call loop_stop() to stop the background thread.
Problem
I am attempting to catch messages by topic by using the message_callback_add() function in this library. Below is my entire module that I am using to deal with my mqtt subscribe and publishing needs. I have been able to test that the publish works, but I can't seem to catch any incoming messages. There are no warnings/errors of any kind and the `print("position")` statements are working for 1 and 2 only. ``` import sys import os import time import Things import paho.mqtt.client as paho global mqttclient; global broker; global port; broker = "10.64.16.199"; port = 1883; mypid = os.getpid() client_uniq = "pubclient_"+str(mypid) mqttclient = paho.Client(client_uniq, False) #nocleanstart mqttclient.connect(broker, port, 60) mqttclient.subscribe("Commands/#") def Pump_callback(client, userdata, message): #print("Received message '" + str(message.payload) + "' on topic '" # + message.topic + "' with QoS " + str(message.qos)) print("position 3") Things.set_waterPumpSpeed(int(message.payload)) def Valve_callback(client, userdata, message): #print("Received message '" + str(message.payload) + "' on topic '" # + message.topic + "' with QoS " + str(message.qos)) print("position 4") Things.set_valvePosition(int(message.payload)) mqttclient.message_callback_add("Commands/PumpSpeed", Pump_callback) mqttclient.message_callback_add("Commands/ValvePosition", Valve_callback) print("position 1") mqttclient.loop_start() print("position 2") def pub(topic, value): mqttclient.publish(topic, value, 0, True) ```