import paho.mqtt.client as mqtt
mqtt_username = "USER"
mqtt_password = "PASSWORD"
mqtt_topic = "TOPIC"
mqtt_broker_ip = "MQTT SERVER"
client = mqtt.Client()
client.username_pw_set(mqtt_username, mqtt_password)
def on_connect(client, userdata, flags, rc):
print ("Connected!", str(rc))
client.subscribe(mqtt_topic)
def on_message(client, userdata, msg):
print ("Topic: ", msg.topic + "\nMessage: " + str(msg.payload))
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqtt_broker_ip, 1883)
client.publish(mqtt_topic, payload="test", retain=True)
# Once we have told the client to connect, let the client object run itself
client.loop_forever()
client.disconnect()
ไม่มีความคิดเห็น:
แสดงความคิดเห็น