How to Install MQTT on Raspberry Pi

In this article, we learn how to transmit MQTT messages from ESP8266 / NodeMCU to a Raspberry Pi running an MQTT broker. Our NodeMCU will read the light levels through a light-dependent resistor (LDR) and then transmit this information to the Raspberry Pi over Wi-Fi using MQTT.

NodeMCU is an open-source IoT platform. It is programmed using Lua scripting language. The platform is based on the eLua open-source project and uses the ESP8266 SDK version 0.9.5 at its core. The platform utilizes many open-source projects, such as lua-cjson and spiffs. NodeMCU includes firmware that can run on the ESP8266 Wi-Fi SoC chip and hardware based on the ESP-12 module.

Circuit Diagram

How to Install MQTT on Raspberry Pi

1.What is MQTT?

Message Queue Telemetry Transport (MQTT) is a messaging protocol that can run on any device with an Internet connection, making it one of the most commonly used protocols in IoT projects. It is designed for systems with low bandwidth constraints, making it an ideal choice for home automation or any IoT application. The advantages of MQTT include its low bandwidth, simple implementation, and suitability for unreliable connections.

How to Install MQTT on Raspberry Pi

MQTT requires an “agent” to receive and transmit MQTT messages. In this case, our agent is Mosquitto, which will be installed on the Raspberry Pi. The second component of the MQTT system is the “client,” which connects to the central broker and can publish or subscribe to MQTT messages. MQTT messages are identified by topics, which are part of the message. Below, we will try to publish and subscribe to the topic “makerpro/mqtt_tutorial/light”.

2. Install Mosquitto Broker on Raspberry Pi

Use the following command in the terminal to install the MQTT broker Mosquitto:

sudo apt install -y mosquitto mosquitto-clients

Test Mosquitto by creating two new instances of the terminal. In one terminal, enter the following:

mosquitto_sub  -v -t makerpro/mqtt_tutorial/light

In another new terminal, enter:

mosquitto_pub -t makerpro/mqtt_tutorial/light -m 'helloWorld'

After pressing Enter on the second terminal, you should see the message “makerpro/mqtt_tutorial/light helloWorld” on the first terminal, as shown in the image:

How to Install MQTT on Raspberry Pi

This indicates that Mosquitto was successfully tested on the Raspberry Pi, and you just need to find its IP address by typing the command hostname -I.

hostname -I

The terminal outputs 192.168.0.47, which is its IP address. The NodeMCU needs this IP to connect to our Mosquitto server.

How to Install MQTT on Raspberry Pi

3.Build the Circuit

Here we need a breadboard, an LDR, jumper wires, a 10K resistor, and our NodeMCU development board. The wiring is as shown in the figure:

How to Install MQTT on Raspberry Pi

The LDR and resistor form a voltage divider, and the NodeMCU’s analog input reads the voltage between them. As the resistance of the LDR changes with lighting, the voltage level between the LDR and the resistor also changes. This way, data can be sent via MQTT to the Raspberry Pi for analysis, health checks, monitoring, and other operations.

Stay tuned for more exciting content!

👇👇👇

How to Install MQTT on Raspberry Pi

Leave a Comment

×