Key Takeaway: Learn how to implement an MQTT-based remote monitoring system for industrial machinery using ESP32 edge gateways, industrial sensors, and cloud-based dashboards. This complete guide covers the end-to-end IoT architecture from sensor selection to alerting.
Table of Contents
1. Why MQTT for Industrial Monitoring
MQTT (Message Queuing Telemetry Transport) is the de-facto standard protocol for Industrial IoT applications. Unlike HTTP, MQTT uses a publish/subscribe model that is ideally suited for machine-to-machine communication. The protocol is lightweight, uses minimal bandwidth, supports unreliable networks with QoS levels, and enables real-time bidirectional communication between sensors, gateways, and cloud platforms.
For Industrial IoT MQTT monitoring, the protocol offers several key advantages:
- Low bandwidth: MQTT headers are just 2 bytes, making it ideal for cellular or satellite-connected remote equipment
- QoS levels: Choose between fire-and-forget (QoS 0), at-least-once delivery (QoS 1), and exactly-once delivery (QoS 2)
- Last Will: Detect machine disconnection immediately with LWT messages
- Topic hierarchy: Organize data with structured topics like
factory/line1/motor1/temperature - Retained messages: New subscribers instantly receive the last known value
2. System Architecture
The complete Industrial IoT MQTT monitoring system consists of four layers:
- Sensor Layer: Temperature (PT100/DS18B20), vibration (MPU6050/ADXL345), current (ACS712), and RPM sensors connected to microcontrollers via analog, I2C, or SPI interfaces.
- Edge Gateway Layer: ESP32 or Raspberry Pi modules running MQTT client firmware that read sensors and publish data to the broker at configurable intervals.
- Transport/Broker Layer: Mosquitto or EMQX MQTT broker handling message routing, security (TLS/SSL), and client authentication.
- Application Layer: Node-RED dashboards, InfluxDB databases, Grafana visualizations, and alerting systems (email, SMS, Telegram).
3. Industrial Sensor Selection
| Parameter | Sensor | Interface | Range | Accuracy |
|---|---|---|---|---|
| Temperature | PT100 RTD | MAX31865 / I2C | -200 to 850C | ±0.1C |
| Temperature | DS18B20 | 1-Wire | -55 to 125C | ±0.5C |
| Vibration | MPU6050 | I2C | ±16g, ±2000/s | High |
| Current | ACS712 | Analog | 5A / 20A / 30A | ±1.5% |
| RPM | Hall effect | Digital pulse | 0-10000 RPM | ±1 RPM |
4. ESP32 Edge Gateway
The ESP32 is the ideal choice for an industrial IoT edge gateway due to its built-in WiFi, Bluetooth, dual-core processor, and rich peripheral set. Here is a complete firmware outline for publishing sensor data via MQTT:
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <MPU6050.h>
const char* ssid = "Factory_WiFi";
const char* password = "your_password";
const char* mqtt_server = "192.168.1.100";
const int mqtt_port = 1883;
const char* device_id = "motor_drive_01";
WiFiClient espClient;
PubSubClient client(espClient);
// Sensor pins
#define ONE_WIRE_BUS 4
#define CURRENT_SENSOR 34
#define RPM_SENSOR 27
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
sensors.begin();
pinMode(RPM_SENSOR, INPUT_PULLUP);
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
// Read temperature
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
// Read current (ACS712: 185mV/A sensitivity)
int raw = analogRead(CURRENT_SENSOR);
float current = ((raw * 3.3 / 4095) - 1.65) / 0.185;
// Publish via MQTT
char msg[50];
snprintf(msg, 50, "%.2f", tempC);
client.publish(("factory/" + String(device_id) + "/temperature").c_str(), msg);
snprintf(msg, 50, "%.2f", current);
client.publish(("factory/" + String(device_id) + "/current").c_str(), msg);
delay(5000); // Publish every 5 seconds
}
MQTT Topic Structure
Organize your topics hierarchically for easy subscription and routing:
factory/{machine_id}/temperature
factory/{machine_id}/vibration
factory/{machine_id}/current
factory/{machine_id}/rpm
factory/{machine_id}/status (online/offline via LWT)
factory/alerts/{machine_id} (alert messages)
5. MQTT Broker Setup
Mosquitto is the most widely used open-source MQTT broker for industrial applications. Install it on a Raspberry Pi or cloud VM:
# Install Mosquitto on Ubuntu/Debian
sudo apt update
sudo apt install mosquitto mosquitto-clients
# Enable and start the service
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
# Test the broker
mosquitto_sub -h localhost -t "factory/#"
mosquitto_pub -h localhost -t "test" -m "hello"
Securing the Broker
For production deployments, you must secure the broker:
- Create a password file:
mosquitto_passwd -c /etc/mosquitto/passwd user1 - Enable TLS by placing certificates in
/etc/mosquitto/certs/ - Add to mosquitto.conf:
listener 8883,cafile,certfile,keyfile - Restart the broker
6. Node-RED Dashboard
Node-RED provides a visual flow-based programming environment that connects directly to MQTT topics. Build a live dashboard with gauges, charts, and status indicators in minutes:
- Install Node-RED on your server:
npm install -g node-red - Add the MQTT input node configured to your broker
- Subscribe to
factory/#topics - Connect to dashboard UI nodes (gauge, chart, text)
- Deploy and access the dashboard at
http://server:1880/ui
7. Analytics and Alerting
Store time-series data in InfluxDB and visualize with Grafana for long-term trend analysis and anomaly detection:
Threshold Alerts
Configure Node-RED function nodes to trigger alerts when values exceed thresholds:
// Node-RED alert function
if (msg.payload > 85.0) {
msg.topic = "factory/alerts/motor_drive_01";
msg.payload = "ALERT: Motor temperature exceeded 85C!";
return msg;
}
return null;
Predictive Maintenance
Monitor vibration trends over time. A gradual increase in vibration amplitude typically indicates bearing wear 2-4 weeks before failure. Set alerts when the 7-day moving average exceeds baseline by 20%.
Frequently Asked Questions
What is the difference between MQTT and Modbus for industrial monitoring?
Modbus is a request-response protocol designed for PLC-to-device communication within a factory floor. MQTT is a publish-subscribe protocol designed for IoT and cloud connectivity. In practice, they complement each other: use Modbus for real-time machine control and MQTT for aggregating data from multiple Modbus networks to a central monitoring system.
How secure is MQTT for industrial use?
MQTT supports TLS/SSL encryption, username/password authentication, certificate-based client authentication, and ACL-based topic authorization. For critical industrial monitoring, always enable TLS on port 8883 and use client certificates.
Can I use cellular connectivity for remote sites?
Yes. MQTT’s small packet size makes it ideal for cellular IoT. Use an ESP32 with a SIM7000G LTE module for remote sites without WiFi. The protocol’s QoS 1 or 2 ensures data delivery even with intermittent cellular coverage.
How many devices can one MQTT broker handle?
Mosquitto can handle 10,000+ concurrent clients on modest hardware. EMQX, a clustered MQTT broker, can scale to millions of devices. For a typical factory with 50-200 machines, a single Mosquitto instance on a Raspberry Pi 4 is sufficient.
What is MQTT Last Will and Testament (LWT)?
LWT is a feature that automatically publishes a message when a client disconnects unexpectedly. Subscribe to factory/+/status to get immediate notifications when equipment goes offline — essential for production monitoring and maintenance dispatch.
Related Reading
- STM32 Temperature Monitoring with DS18B20
- Stepper Motor Control with DRV8825
- STM32 Blue Pill Pinout Guide
Sources
- MQTT Standard Protocol (OASIS)
- Eclipse Mosquitto MQTT Broker
- Node-RED Flow-Based Programming
- ESP-IDF Programming Guide
- InfluxDB Time Series Database
Disclosure: This post contains affiliate links. We may earn a small commission when you purchase through these links at no extra cost to you.

