|
|
@@ -0,0 +1,29 @@ |
|
|
|
import paho.mqtt.client as mqtt |
|
|
|
|
|
|
|
|
|
|
|
def on_connect(_client, userdata, flags, rc): |
|
|
|
print("Connected with result code " + str(rc)) |
|
|
|
client.subscribe("test/topic") |
|
|
|
|
|
|
|
|
|
|
|
def on_message(_client, userdata, msg): |
|
|
|
print(msg.topic + " " + str(msg.payload)) |
|
|
|
|
|
|
|
|
|
|
|
client = mqtt.Client() |
|
|
|
client.on_connect = on_connect |
|
|
|
client.on_message = on_message |
|
|
|
|
|
|
|
client.connect("localhost", 1883, 60) |
|
|
|
|
|
|
|
client.loop_forever() |
|
|
|
|
|
|
|
"""In this example, we create a new instance of the mqtt.Client class and set up the on_connect and on_message |
|
|
|
callback functions. The on_connect function is called when the client successfully connects to the broker, |
|
|
|
and the on_message function is called when a message is received on a subscribed topic. |
|
|
|
|
|
|
|
Then, we call the connect method to connect to the MQTT broker running on the local machine on port 1883, and set the |
|
|
|
keepalive value to 60 seconds. |
|
|
|
|
|
|
|
Finally, we call the loop_forever method to start the client and begin processing incoming messages. This method |
|
|
|
blocks the program execution and runs the client loop in a loop until the client is disconnected. (ChatGPT)""" |