Arduino remote monitoring with MQTT

Photo of author

By Jackson Taylor

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:
  • Arduino board (e.g., Arduino Uno or Arduino Nano)
  • Wi-Fi or Ethernet Shield (e.g., ESP8266 or ESP32 for Wi-Fi)
  • MQTT Broker (like Mosquitto or CloudMQTT)
  • Sensors (e.g., temperature, humidity, motion sensors)
  • Jumper wires and breadboard

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 ThingSpeak

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:
cpp
#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:
cpp
#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.
cpp
#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
Arduino self-driving car with remote control

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.
cpp
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.
cpp
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.

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.