import paho.mqtt.client as mqtt
def on_message(client, userdata, message):
# userdata is the structure we choose to provide, here it's a list()
print('message is :',message.payload.decode())
def on_connect(client, userdata, flags, reason_code, properties):
if reason_code.is_failure:
print(f"Failed to connect: {reason_code}. loop_forever() will retry connection")
else:
# we should always subscribe from on_connect callback to be sure
# our subscribed is persisted across reconnections.
client.subscribe("TestingTopic")
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,client_id='py/001')
mqttc.username_pw_set('python','111')
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.connect("yours serverip")
mqttc.publish(topic="TestingTopic", payload="TestingPayload", qos=1, retain=False)
mqttc.loop_forever()
WIN10使用pycharm接入MQTT服务器
最新推荐文章于 2024-08-28 08:53:02 发布