Arduino remote monitoring with MQTT

Photo of author

By Jackson Taylor

Home » Arduino » Arduino remote monitoring with MQTT

As an Amazon Associate we earn from qualifying purchases.

Arduino remote monitoring is a powerful way to keep track of various systems and devices in real-time, especially when paired with MQTT (Message Queuing Telemetry Transport), a lightweight messaging protocol. This combination allows for seamless communication between an Arduino and remote devices, providing efficient monitoring solutions for applications ranging from smart homes to industrial automation. In this guide, we’ll explore how Arduino can be used for remote monitoring with MQTT, offering a clear step-by-step process to set up and implement this system.

What is MQTT and Why Use It with Arduino?

Introduction to MQTT

MQTT is a simple and efficient messaging protocol designed for small sensors and mobile devices in remote locations. It operates on a publisher-subscriber model, where devices (clients) send and receive messages through a broker. The lightweight nature of MQTT makes it ideal for low-bandwidth, high-latency, or unreliable networks.

Why Pair Arduino with MQTT?

Arduino is a popular open-source electronics platform, widely known for its flexibility and ease of use. When combined with MQTT, Arduino can act as a sensor node that collects data and sends it to remote monitoring systems via MQTT, enabling real-time tracking and control. This combination is perfect for projects like smart homes, weather stations, and industrial monitoring systems.

Setting Up the Arduino for Remote Monitoring

Required Components for Arduino MQTT Setup

Before diving into the setup process, you’ll need some essential components:

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

Installing Libraries for MQTT Communication

To communicate with an MQTT broker, you need to install the necessary libraries. The most commonly used library for Arduino is the PubSubClient library. You can install this library directly through the Arduino IDE by going to Sketch > Include Library > Manage Libraries, then searching for “PubSubClient” and clicking install.

See also
Arduino remote monitoring with Blynk

Connecting Arduino to the Internet

Using ESP8266 or ESP32 for Wi‑Fi Connectivity

To enable remote monitoring, your Arduino must be connected to the internet. The ESP8266 and ESP32 are popular Wi‑Fi modules compatible with Arduino that allow easy access to the internet.

  1. Connect the ESP8266 or ESP32 to the Arduino.
  2. In the Arduino IDE, select the correct board and port from Tools.
  3. Use the WiFi.h library to connect to a Wi‑Fi network. Example code:
#include <WiFi.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

void setup() {
 Serial.begin(115200);
 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.println("Connecting to WiFi...");
 }
 Serial.println("Connected to WiFi");
}

void loop() {
}

Setting Up the MQTT Broker

Choosing an MQTT Broker

An MQTT broker is the central hub for message exchange. You can either use a public MQTT broker or set up your own using software like Mosquitto. For beginners, using a free cloud broker like CloudMQTT or Adafruit.io is a great option.

Connecting Arduino to the MQTT Broker

Once the Arduino is connected to the internet, it can begin publishing and subscribing to MQTT topics. Here’s a basic example using the PubSubClient library to connect to an MQTT broker and publish a message:

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "broker_address";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
 Serial.begin(115200);
 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.println("Connecting to WiFi...");
 }
 client.setServer(mqtt_server, 1883);
}

void loop() {
 if (!client.connected()) {
  reconnect();
 }
 client.loop();
 client.publish("topic/arduino", "Hello from Arduino");
}

void reconnect() {
 while (!client.connected()) {
  if (client.connect("ArduinoClient")) {
   client.subscribe("topic/arduino");
  } else {
   delay(5000);
  }
 }
}

Reading Sensor Data for Remote Monitoring

Integrating Sensors with Arduino

To make your monitoring system more functional, you’ll need to integrate sensors with the Arduino. Here’s how you can set up a temperature and humidity sensor (e.g., DHT11) to send data to the MQTT broker.

  1. Wire the DHT11 sensor to the Arduino.
  2. Use the DHT.h library to read sensor data.
#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
 Serial.begin(115200);
 dht.begin();
}

void loop() {
 float temp = dht.readTemperature();
 float humidity = dht.readHumidity();

 if (!isnan(temp) && !isnan(humidity)) {
  String message = "Temperature: " + String(temp) + "°C, Humidity: " + String(humidity) + "%";
  client.publish("topic/arduino", message.c_str());
 }
 delay(5000);
}

Real-Time Data Monitoring

By sending data from the sensor to the MQTT broker, you can now monitor it remotely. The data can be displayed on a dashboard or logged into a database for further analysis.

See also
How to read a temperature sensor with Arduino

Securing Your Arduino MQTT System

Enabling SSL/TLS Encryption

While MQTT works well for many applications, security is crucial. Enabling SSL/TLS encryption ensures that your data is transmitted securely. Many public brokers support SSL encryption. You’ll need to adjust the PubSubClient library to use secure connections by modifying the port and using certificates for authentication.

client.setServer(mqtt_server, 8883); // SSL port
client.setCallback(callback); // Set a callback function to handle incoming messages

Authenticating with MQTT Broker

To prevent unauthorized access to your MQTT system, use authentication methods like username and password. This can be configured on both the Arduino and the broker.

client.connect("ArduinoClient", "username", "password");

Visualizing Data with MQTT Dashboards

Using MQTT Dashboards for Visualization

To make remote monitoring even more useful, you can visualize your data using MQTT dashboards. Platforms like ThingSpeak, Blynk, and Node-RED allow you to create real-time visualizations of the data sent from your Arduino. These platforms support MQTT, making it easy to integrate and display your Arduino sensor readings.

Frequently Asked Questions About Arduino and MQTT

Can an Arduino Uno use MQTT on its own?

Not without a network connection. A standard Arduino Uno has no Wi-Fi or Ethernet built in, so it needs an Ethernet shield or a Wi-Fi module. Boards such as the ESP8266 and ESP32 have Wi-Fi built in and can run the PubSubClient library directly, so many people use them in place of an Uno for MQTT projects.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

How do I check that my messages are reaching the broker?

Subscribe to the same topic from another device. If you run Mosquitto, the mosquitto_sub command-line tool prints every message that arrives on a topic. A desktop MQTT client with a graphical interface works too. If nothing shows up, check the broker address, the port, and whether the broker requires a username and password.

What are MQTT topics and how should I name them?

A topic is a text label, written as levels separated by slashes, that tells the broker where a message belongs. A clear structure makes a system easier to grow, for example home/livingroom/temperature and home/livingroom/humidity. Publish each reading to its own topic instead of packing everything into one text string. Separate topics are easier to chart and to use in automations.

See also
Arduino security system with motion sensors

Why does my Arduino publish too many messages?

If client.publish() sits in loop() with no delay, the board sends messages as fast as it can. This can overload the broker or get you disconnected. Publish at a sensible interval, such as every few seconds or minutes depending on how fast the value changes. A simple timer based on millis() works better than delay() because the client can keep handling incoming messages in the meantime.

Which MQTT quality of service levels does PubSubClient support?

PubSubClient publishes messages at QoS 0, which means “at most once” with no delivery confirmation. It can subscribe at QoS 0 or QoS 1. This is fine for regular sensor readings, since the next reading arrives shortly afterward. If you need guaranteed delivery for critical alerts, consider a different MQTT library.

Conclusion

In conclusion, combining Arduino with MQTT provides a powerful solution for remote monitoring. The simplicity of Arduino, paired with the efficiency of MQTT, allows for real-time data collection and transmission over the internet. Whether you’re monitoring a home automation system or tracking industrial processes, this setup ensures you stay connected with the data that matters most. By following the steps outlined in this guide, you’ll be well on your way to creating a seamless remote monitoring solution with Arduino and MQTT.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works