How to Publish Data From an Arduino Microcontroller Via MQTT?
by kavishchoudhary1935 in Circuits > Arduino
231 Views, 2 Favorites, 0 Comments
How to Publish Data From an Arduino Microcontroller Via MQTT?

This guide will walk you through the process of setting up MQTT on an Arduino (using an Arduino Nano 33 IoT in this example) and testing it with a local MQTT client tool like MQTTX. MQTT is a lightweight messaging protocol perfect for IoT applications, making it a popular choice for communication between devices.
Supplies
What You'll Need
- Arduino Nano 33 IoT (or similar with WiFi support)
- WiFi network credentials
- MQTT broker (we'll use a public broker, but you can also set up a local broker)
- MQTTX - a desktop MQTT client for testing (available for Windows, MacOS, and Linux)
Install Required Libraries

For this setup, we'll use two libraries: WiFiNINA for WiFi connectivity and PubSubClient for MQTT communication.
- Open the Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries.
- Search for and install WiFiNINA and PubSubClient.
Connect to WiFi and MQTT Broker
First, configure the Arduino to connect to WiFi and the MQTT broker.
Here's a basic sketch:
Explanation of the Code
- setupWiFi(): Connects to WiFi using the provided SSID and password.
- reconnectMQTT(): Reconnects to the MQTT broker if the connection is lost.
- callback(): Handles incoming MQTT messages, printing the message content.
- loop(): Publishes a "Hello from Arduino!" message every 5 seconds to the MQTT topic.
Upload this code to your Arduino.
Download and Set Up MQTTX
MQTTX is a powerful MQTT client that can be used to test MQTT connections.
- Download MQTTX from MQTTX GitHub releases.
- Install MQTTX on your computer.
Test the MQTT Connection Using MQTTX
Now, you’ll use MQTTX to connect to the same broker and topic as your Arduino.
- Open MQTTX.
- Create a New Connection:
- Click on New Connection.
- Set the Client ID (e.g., "MQTTXClient").
- Set the Host to broker.hivemq.com (or use your local broker’s IP if applicable).
- Set the Port to 1883.
- Click Connect.
- Subscribe to the Topic:
- Once connected, click on Subscribe and enter the topic name: test/arduino.
- Click Confirm to start subscribing to messages on this topic.
- View Published Messages:
- MQTTX should now show any messages published to test/arduino.
- If everything is set up correctly, you should see "Hello from Arduino!" messages every 5 seconds.
- Test Sending Messages to Arduino:
- Go to the Publish tab in MQTTX.
- Set the Topic to test/arduino and type a test message in the payload (e.g., "Message from MQTTX").
- Click Publish.
- Your Arduino should print this message in the Serial Monitor, confirming it received the MQTT message.
Troubleshooting
If you encounter issues:
- Check WiFi Connection: Verify your SSID and password are correct.
- Verify Broker Connection: Ensure the broker (like HiveMQ or your local one) is reachable.
- Check MQTT Settings: Ensure MQTT server, port, and topic match between Arduino and MQTTX.
- Monitor Serial Output: Use the Serial Monitor to view debug messages from your Arduino.
Customizing for a Local Broker
To use a local broker instead of a public one, you can install Mosquitto, a popular MQTT broker.
- Install Mosquitto on your computer. For installation instructions, refer to the Mosquitto website.
- Start Mosquitto:
- Run Mosquitto by opening a terminal and typing mosquitto -v (on Linux/macOS) or starting the Mosquitto service on Windows.
- Mosquitto will run on localhost (IP: 127.0.0.1) and port 1883 by default.
- Update Arduino Code:
- Change the mqtt_server variable in the Arduino code to mqtt_server = "your_local_ip_address".
- Connect MQTTX:
- In MQTTX, change the Host field to localhost or your computer’s IP address.
- Connect and test as described earlier.