robust.py 726 B

12345678910111213141516171819202122232425
  1. """Fake mqtt interface - this simulates the api provided
  2. by micropython. We use paho.mqtt to talk to the broker.
  3. """
  4. import paho.mqtt.client
  5. class MQTTClient:
  6. def __init__(self, name, host, port):
  7. self.client = paho.mqtt.client.Client(name)
  8. self.host = host
  9. self.port = port
  10. def connect(self):
  11. self.client.connect(self.host, self.port)
  12. self.client.loop_start()
  13. def disconnect(self):
  14. self.client.disconnect()
  15. self.client.loop_stop(force=False)
  16. def publish(self, topic, data):
  17. topic = str(topic, encoding='utf-8') # paho wants a string
  18. print("publishing %s on %s" % (repr(data), repr(topic)))
  19. self.client.publish(topic, data)