repository to manage all files related to the makeathon farm bot project (Software + Documentation).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mqtt_client_basic.py 1.1KB

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