How To Set Up A Local MQTT Broker

If Mosquitto is not yet installed, refer to https://mosquitto.org/download/ for installation instructions.

With the Mosquitto PPA added to the repository lists, install the Mosquitto broker and helper clients with:

Before starting a client program, ensure that the Mosquitto Broker is installed and running with:

If the Mosquitto Broker is active, the output will be the PID number associated with the Mosquitto daemon. For example:

If the pgrep command yields no output, start Mosquitto with:

The -v flag enables Verbose mode. Making it easier to troubleshoot connections to the broker.

If Verbose mode is not desired, the Mosquitto broker can be started in Daemon mode by passing parameter -d. Daemon mode runs the Mosquitto broker in the background, freeing up a terminal for other purposes.

Testing The Connection

The data being published to the Mosquitto Broker can be viewed with:

To publish a message, in another terminal:

That Is It

If a connection was successfully established with the local Mosquitto broker, you are ready to continue by programming clients to communicate through the broker.

Check out the following guides on programming MQTT clients:

How To MQTT In C++

How To MQTT In Python