Search code examples
pythonmqttpublisher

Is it possible to use the raspberry pi on which the MQTT broker is running, also to publish on the MQTT broker? So as a broker and publisher


I am currently running a raspberry pi with a MQTT broker. But my problem is that I also want to use the raspberry pi to publish on the broker, for some microcontrollers to receive the message. I do not know how to utilise this. There's however a few ideas:

  1. Make Thonny open the terminal and make it paste this message: mosquitto_pub -d -t testTopic -m "Hello world!" Though I dont know whether that is possible, especially due to the "" that are required for it to work.
  2. Or a more direct method by using a mqtt library for Thonny. However there is only one library "mqtt 0.0.1", and the developer doesnt even have a github page for it. Also installing this library externally "micropython-umqtt.simple 1.3.4" didn't work either.

Solution

  • Yes, you can run both a MQTT Broker and a MQTT Client on the same device, you can even run 1000s of clients on the same device if you so choose to.

    As for Python MQTT clients, the most popular is probably the Paho Python client available here: https://www.eclipse.org/paho/clients/python/

    Or for devices the MicroPython client https://mpython.readthedocs.io/en/master/library/mPython/umqtt.simple.html (with it's github page here: https://github.com/micropython/micropython-lib/tree/master/micropython/umqtt.simple)

    If you have problems with any of the libraries, ask a new question and include all the details about the problem, so that we have something to work with.