MQTT communication between NodeMCU and Raspberry Pi
In this tutorial, we will learn how to make communicate between NodeMCU and Raspberry Pi using MQTT. We will use Mosquitto MQTT broker to establish a connection between NodeMCU and Raspberry Pi.
What is MQTT?
MQTT is a lightweight messaging protocol. MQTT stands for Message Queuing Telemetry Transport. Due to its lightweight nature, MQTT is widely used in IoT applications. Besides, MQTT supports bi-directional communication.
MQTT Architecture
MQTT is a publish/subscribe protocol. In this protocol, there are two types of entities: publishers and subscribers. Publishers publish messages to a topic. Subscribers subscribe to a topic and receive messages published to that topic. A topic is like a channel. A message published to a topic is received by all the subscribers subscribed to that topic.
To establish a connection between a publisher and a subscriber, a broker is required. The broker is a central entity that manages the communication between publishers and subscribers. The broker is responsible for routing messages to the correct subscribers.
Workflow
Send temperature data from NodeMCU to Raspberry Pi.
Control LED from Raspberry Pi using MQTT.
Prerequisites
Before we start, make sure you have the following:
- Raspberry Pi
- Any Linux distribution installed on your Raspberry Pi
- Python 3 installed on your Raspberry Pi
- NodeMCU
- DHT Sensor
Setup MQTT Broker on Raspberry Pi
In your Raspberry Pi, open the terminal and run the following commands to install Mosquitto MQTT broker:
Now, install Mosquitto MQTT broker and the Mosquitto clients:
To check if Mosquitto is installed successfully, run the following command:
If Mosquitto is installed successfully, you will see the following output:
The above output shows that Mosquitto is installed successfully.
Now with this configuration, you can publish and subscribe to the MQTT broker from your Raspberry Pi. But, if you want to publish and subscribe to the MQTT broker from your NodeMCU, you need to configure the MQTT broker to accept connections from other devices.
To do this, edit the Mosquitto configuration file /etc/mosquitto/mosquitto.conf
:
Add the following lines to the configuration file:
The full configuration file will look like this:
Save the file and restart the Mosquitto service:
Now, you can publish and subscribe to the MQTT broker from your NodeMCU.
Setup NodeMCU to Publish and Subscribe to MQTT Broker
We will write the program to publish and subscribe to the MQTT broker in C++
. You can use Arduino IDE or any other IDE to write the program. Before we start, install the following libraries:
Now, connect the DHT11 sensor to your NodeMCU. The DHT11 sensor has three pins: VCC
, GND
, and DATA
. Connect the VCC pin to the 3.3V pin of your NodeMCU. Connect the GND pin to the GND pin of your NodeMCU. Connect the DATA pin to the D6
pin of your NodeMCU.
Now, write the following program to publish the temperature data to the MQTT broker and subscribe to the MQTT broker to receive the LED control data:
Now upload the program to your NodeMCU. You can use the Arduino IDE to upload the program. After uploading the program, open the serial monitor and check if the program is working correctly. You should see the temperature data being published to the MQTT broker.
Publish and Subscribe to MQTT Broker from Raspberry Pi
In your Raspberry Pi terminal, write the following commands to subscribe to test/data
topic to receive the temperature data from the NodeMCU:
This will subscribe to the test/data
topic and print the temperature data received from the NodeMCU in the terminal in every 5 seconds.
Now, write the following command to publish the LED control data to the test/ctrl
topic:
This will send the on
message to the test/ctrl
topic. Now, the NodeMCU will turn on the LED. You can also send the off
message to turn off the LED.
Note: with the above program, the LED will turn on when test/ctrl
message is "off" and turn off when test/ctrl
message is "on". This is because the LED is build-in to the NodeMCU and it is active low.
References
- MQTT - The Standard for IoT Messaging, https://mqtt.org/.
- Eclipse Mosquitto, https://mosquitto.org/.
- Ndungu, Francis. “Install Mosquitto MQTT Broker On Ubuntu 20.04 Server." Vultr, 15 October 2021, https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/.
- O'Leary, Nick. “knolleary/pubsubclient: A client library for the Arduino Ethernet Shield that provides support for MQTT.” GitHub, https://github.com/knolleary/pubsubclient.
- Santosh, Rui. “Install Mosquitto Broker Raspberry Pi.” Random Nerd Tutorials, https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/.