Reputation: 63
I have a little python script which connects, subscribes, publish and disconnects to a mqtt broker. Everything is fine, but I only can use the received data in the on_message function.
first the actual code:
import time
import sys
sys.path.append('C:\\Users\\user\\Python\\pyproj\\project1\\Lib\\site-packages\\paho_mqtt-1.6.1-py3.10.egg')
import paho.mqtt.client as mqtt
userdata = "Leer"
client_name = "Pythonscript"
broker_address="000.000.000.000"
port = 1234
topic = "topic to publish"
subtopic = "topic to recieve"
mqtt1 = ''
def on_log(client, userdata, level, buf):
print(" client: ",client)
print(" userdata: ",userdata)
print(" level: ",level)
print(" buf: ",buf)
def on_connect(client, userdata, flags, rc):
if rc==0:
#print("connected OK ")
#print("Subscribing to topic ",subtopic)
client.subscribe(subtopic)
else:
print("Bad Connection Returned code=",rc)
def on_message(client,userdata,msg):
topic=msg.topic
m_decode=str(msg.payload.decode("utf-8","ignore"))
global mqtt1
mqtt1 = m_decode
print("message recieved:",m_decode)
print("message in mqtt1:",mqtt1)
return m_decode
def on_disconnect(client, userdata, flags, rc=0):
print("Disconnected result code "+str(rc))
print("creating new instance ",client_name)
client = mqtt.Client(client_name)
client.on_connect=on_connect
client.on_disconnect=on_disconnect
#client.on_log=on_log
client.on_message=on_message
print("connecting to broker ",broker_address+" :"+str(port))
client.connect(broker_address,port,60)
client.loop_start()
print("Publishing message to topic ",topic)
client.publish(topic, "python mqqt message")
result = mqtt1 + "changes"
print("recieved Message in Variable:",result)
time.sleep(4)
client.loop_stop()
client.disconnect()
i would like to get the whole message object and work with that data in my script.
I see my msg.payload in the console with print in on_message. but i cant use it, for instance, in the variable result.
I tried with a global variable, with a return from the on_message function. but nothing works.
I think I don't understand which parameters I should give the on_message function to get my returned value (later the whole msg object)
perhaps somebody can help me to understand.
here is my console output:
But I cant get the value in an variable out of the on_message function...
Upvotes: 0
Views: 2241
Reputation: 59826
You need to remember that the on_message()
function is not called by any of your code, it is called by the MQTT client's network loop as it processes incoming packets from the network.
The return
at the end of on_message()
will not do anything useful as there is nowhere to return anything to.
Your next problem is that you have to remember that MQTT is asynchronous, so trying to read the value of mqtt1
immediate after the call to client.publish()
will just not work. Your code will try to read the value immediately, but you have no way of knowing how long it will take for a message to be delivered as a possible response from any client that is subscribed.
Remember that you should not think of MQTT as something like HTTP, it is not a request response synchronous model. While MQTTv5 added the concept of response topics in the headers, it did not change the fact that a response message is totally separate the request message and may arrive, arrive late, never arrive or arrive multiple times from multiple other clients.
You need to spend some time learning about asynchronous systems and look at how you maintain state under those circumstances (e.g. a state machine model)
Upvotes: 0