Main Content

Running a MQTT Broker on Raspberry Pi

This project gives you an overview on MQTT and helps you setup a mosquitto MQTT server on a Raspberry Pi. We will send some Dummy data too.

In the fast-changing world of IoT, there’s no such thing as a universal standard. But over the last few years, MQTT has started to establish itself as the de facto messaging protocol. In this tutorial you’ll learn how to set up an MQTT message server in just a few minutes using a Raspberry Pi.

Before starting with the tutorial, let us know a little more about MQTT.

What is MQTT?

MQTT stands for Message Queuing Telemetry Transport, which is quite a complex name. In simple words, it is a very simple protocol which enables us to send and receive messages across a network to any of the devices.

It is a publish/subscribe communication protocol and is quite flexible to use. The next section deals with MQTT Brokers, publishers and subscribers.

MQTT Brokers, Publishers and Subscribers

Even though these terms look scary, it is a very simple explanation. In terms of a client-server terminology, here is how to explain them:

The MQTT Broker is the Server.
The MQTT Subscribers and Publishers are the Clients.
The MQTT Broker in this tutorial will be our raspberry pi. The role of the broker is to receive all the messages from the publishers (i.e. the clients that publish on this server) and to send particular messages to the subscribers (i.e. the devices which have opted to view the published sensor data). The image below explains the concept we discussed just now.

But one more question arises now. How does the Broker know where and what message to publish or send to which device. This is done using the concept of Topics and Messages. The next section will explain them in detail.

MQTT Topics and Messages

There are no client device addresses or identifiers in MQTT, which makes it very easy to build an expansible, ad hoc network. The only thing all clients must know is the address of the server.

So, how do messages get routed between the clients? The solution for this is Topics and Messages.

Publishers
The Publishers sends messages to a particular topic, which are equivalent to channels. A topic can have sub-topics too. For example, in an application where you send the temperature data from a sensor connected to your fridge, the topic will look something like this:

Kitchen/Fridge/

The main topic is the Kitchen, and the appliance is the subtopic. The message will be “Temperature:14” on the given topic.

Subscribers
The subscribers listen to the topic. So, if the subscriber is listening to the topic Kitchen then it will have access to all the sub topics which are a part of this topic.

Broker
The Broker handles all these topics and acts as an intermediate connector. It helps the publisher to publish its messages on a topic and the subscriber to listen to the given topic. The only requirement is they should be on the same network if the Broker is running locally.

This is all you need to know about MQTT to get on with this tutorial. Now, we will learn the following:

1. Host a MQTT Broker on a Raspberry Pi

2. Publish to a particular topic locally and remotely using a Windows PC.

3. Send DHT11 sensor data from a node MCU to the MQTT Server.

So, let’s get started.”

Link to article