Arduino Nano ESP32 - MQTT
In this guide, we'll delve into utilizing Arduino Nano ESP32 for sending and receiving data to/from an MQTT broker through the MQTT protocol. Specifically, we'll cover:
- Establishing a connection between Arduino Nano ESP32 and an MQTT broker.
- Programming Arduino Nano ESP32 to transmit data to the MQTT broker by publishing it to a specific MQTT topic.
- Programming Arduino Nano ESP32 to receive data by subscribing to a designated MQTT topic.
We'll examine two distinct scenarios:
- Utilizing Arduino Nano ESP32 with an online MQTT broker.
- Utilizing Arduino Nano ESP32 with an MQTT broker installed on your PC.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Arduino Nano ESP32 and MQTT
If you're already familiar with the MQTT protocol, that's great! If not, you can search for it online. This tutorial is focused on using Arduino Nano ESP32 for sending and receiving data using MQTT.
Here are different ways Arduino Nano ESP32 can be used with MQTT:
- You can connect Arduino Nano ESP32 to an online MQTT broker like Mosquitto or AWS IoT.
- You can connect Arduino Nano ESP32 to an MQTT broker installed on your computer, such as Mosquitto or HiveMQ.
- You can connect Arduino Nano ESP32 to an MQTT broker running on your Raspberry Pi, for example, Mosquitto.
- You can also connect Arduino Nano ESP32 to a cloud-based MQTT broker, like Mosquitto or HiveMQ on AWS EC2.
In this tutorial, we'll begin by checking if Arduino Nano ESP32 can connect to an online Mosquitto broker. We'll have Arduino Nano ESP32 exchange data through this broker over the internet.
After that, we'll set up the Mosquitto broker on our computer. Then, we'll connect Arduino Nano ESP32 to this local broker and continue sending and receiving data.
Once you've completed this tutorial, you can explore further by looking into these additional tutorials:
These tutorials will provide more insights into related topics.
Connect Arduino Nano ESP32 to an online MQTT broker
In this part, we will learn how to connect Arduino Nano ESP32 to test.mosquitto.org, an online MQTT broker created by Mosquitto. Please note that this broker should be used for the testing purpose only.
Arduino Nano ESP32 Code
The below Arduino Nano ESP32 code does:
- Connect to the MQTT broker
- Subscribe to a topic
- Periodically publish messages to the same topic that it subscribes
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM port.
- Open Arduino IDE on your PC
- Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of Arduino IDE
- Type MQTT on the search box, then look for the MQTT library by Joel Gaehwiler.
- Click Install button to install MQTT library.
- Type Arduino Nano ESP32Json on the search box, then look for the Arduino Nano ESP32Json library by Benoit Blanchon.
- Click Install button to install Arduino Nano ESP32Json library.
- Copy the above code and open with Arduino IDE
- Replace the WiFi information (SSID and password) in the code with your own.
- In the code, you will see the word 'YOUR-NAME' three times. Replace this word with your name or random characters (alphabet characters only, no spaces). This is necessary because if you do not make the change, there may be multiple people running this code at the same time, which could lead to conflicts because the MQTT client IDs and topics are the same for everyone.
- Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
- Open the Serial Monitor
- See the result on Serial Monitor.
As you can see, Arduino Nano ESP32 publishes messages to the MQTT broker, then receives back the same message. That is because the above code subscribes to the same topic that it publishes data to. If you do not want Arduino Nano ESP32 to receive the message that it publishes, simply make the SUBSCRIBE topic different from the PUBLISH topic.
Connect Arduino Nano ESP32 to the MQTT broker installed on your PC
Installing Mosquitto MQTT Broker
- Download the Mosquitto MQTT Broker
- Install it on the D: drive instead of the C: drive. Avoid installing the Mosquitto broker on the C: drive to prevent potential issues.
Run Mosquitto MQTT broker
Now, let's check if the MQTT broker is functioning properly by following these steps:
- Go to the directory where Mosquitto was installed. For instance: D:\mosquitto>
- Make a new file named test.conf, copy the content below, and save it in that directory:
- Run a Command Prompt as Administrator on your PC. Let's call it Broker Window. Do not close it until the end of the tutorial.
- Run the below commands one by one:
- You will see:
- Open another Command Prompt as Administrator on your PC.
- Find the IP address of your PC by running the below command:
- Write down the IP address for later use. In the above example: 192.168.0.5
Test if the Mosquitto Broker works
- Open another Command Prompt as Administrator on your PC. Let's call it Subscriber Window
- Subscribe to a topic by running the below commands one by one (replace by your IP address):
- Open another Command Prompt as Administrator on your PC. Let's call it Publisher Window
- Publish a message to the same topic by running the below commands one by one (replace by your IP address):
- You will see:
You will see that message is forwarded to the Subscriber Window as follows:
Now, you installed successfully the Mosquitto MQTT broker on your PC. Please do NOT close three windows: Broker Window, Subscriber Window, and Publisher Window. We will use them next.
Arduino Nano ESP32 Code
The below Arduino Nano ESP32 code does:
- Connect to the MQTT broker
- Subscribe to a topic
- Periodically publish messages to another topic
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM port.
- Copy the above code and open with Arduino IDE
- Replace the WiFi information (SSID and password) in the code with your own.
- Replace the MQTT broker address in the code (domain name or IP address).
- Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
Send message from Arduino Nano ESP32 to PC via MQTT
Arduino Nano ESP32 codes publishes data to the MQTT topic esp32-001/send, Subscriber Window on PC subscribe that topic to receive the data.
- Open the Serial Monitor, you will see Arduino Nano ESP32 periodically publish a message to a topic.
- Check the Subscriber Window, you will see that it receives the message published by Arduino Nano ESP32 as below:
Send message from PC to Arduino Nano ESP32 via MQTT
Arduino Nano ESP32 subscribes to the topic esp32-001/receive, Publisher Window on PC publish a message to that topic to send it to the Arduino Nano ESP32.
- Publish a message to the topic that Arduino Nano ESP32 subscribed by running the following command on Publisher Window:
- You will see this message is received by Arduino Nano ESP32 on Serial Monitor as below: