I have a sample code for getting a message from Mqtt broker
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("123")
def on_message(client, userdata, msg):
x = int(msg.payload)*10
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.connect("broker.hivemq.com", 1883)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
How can I get the variable of msg.payload
in on_message
?
I add the x
variable in function of on_connect
:
def on_message(client, userdata, msg):
x = int(msg.payload)*10
print(msg.topic+" "+str(msg.payload))
return x
Question: How do I get the variable x
?