MQTT Publish Client

Medium Python Protocols
Solve this problem

Implement a Python MQTT client that connects to a broker and publishes sensor data periodically. The client should maintain connection, handle disconnects, and support QoS 1.

Constraints

  • Use paho-mqtt library.
  • Broker: test.mosquitto.org.
  • Topic: device/sensor.
  • Publish every 2 seconds.
  • Reconnect on disconnect.
  • Use QoS level 1.
Starter codePython
1import paho.mqtt.client as mqtt2# TODO