Marcio Cunha

How to monitor industrial sensors via ESP32 and MQTT

Learn how to collect real-time telemetry with ESP32 and MQTT in industrial environments, ensuring low power consumption and reliable data delivery.

Marcio Cunha•3 min
Also available in:PortuguêsEspañol
Summary
  • The ESP32 offers excellent cost-effectiveness along with native Wi-Fi and Bluetooth connectivity for the factory floor.
  • The MQTT protocol consumes minimal bandwidth and battery, making it ideal for unstable networks with many devices.
  • Raw industrial signals require isolation circuits and voltage conversion before reaching the microcontroller pins.
  • Publishing data at regular intervals prevents network congestion and protects the message broker from failure.
  • Ensuring automatic reconnection to Wi-Fi and the MQTT broker prevents telemetry loss in critical environments.

Why use the ESP32 in modern industrial automation?

Bringing intelligence to the factory floor does not necessarily require expensive industrial computers or high-end PLCs. In practice, this means affordable microcontrollers like the ESP32 can read temperatures, vibrations, and pressures with high precision. The secret lies in understanding how to handle real-world electrical signals, which are usually noisy, and turn them into safe digital data.

In industrial settings, physical robustness and immunity to electromagnetic interference are top priorities. The ESP32 meets this demand by offering dual-core processing and low power consumption, allowing local readings to be processed before sending them out. When you combine this hardware with a modular software architecture, you build a resilient and easily scalable sensor network.

Understanding the MQTT protocol and its lightweight design

MQTT is a lightweight messaging protocol designed specifically for unstable network connections with limited bandwidth. It operates on a publish-subscribe model, where devices send data to a central broker without needing to know who will read it. In practice, this dramatically reduces network traffic compared to traditional HTTP requests.

For industrial sensors sending readings every second, this packet saving prevents communication channel congestion. Furthermore, the protocol allows configuring quality of service levels known as QoS, ensuring critical alarm messages reach their destination even if temporary radio signal drops occur.

Preparing the hardware and isolating signals

The input pins of the ESP32 operate at 3.3V logic levels and cannot tolerate the 24V levels commonly found in standard industrial sensors. Therefore, the first practical step involves using voltage dividers or optocouplers to protect the microcontroller. In practice, this physical barrier prevents power surges from frying the main circuit.

Beyond electrical protection, using shielded cables and keeping data wiring away from large electric motors is essential. If your application demands high reliability and robust data processing on the server side, it is worth optimizing data flow just like we do when we speed up REST endpoints with memory caching to avoid bottlenecks at the receiving end.

Writing the reading and publishing code

The code running on the ESP32 must be simple, straightforward, and focused on preventing memory overflow crashes. Below is a functional example using the PubSubClient library to connect the device to Wi-Fi and publish telemetry from an analog sensor.

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

const char* ssid = "YourWiFiNetwork";
const char* password = "YourPassword";
const char* mqtt_server = "192.168.1.100";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
client.setServer(mqtt_server, 1883);
}

void loop() {
if (!client.connected()) {
while (!client.connected()) {
if (client.connect("ESP32IndustrialClient")) {
break;
} else {
delay(5000);
}
}
}
client.loop();

int sensorValue = analogRead(34);
char buffer[10];
sprintf(buffer, "%d", sensorValue);
client.publish("factory/sensor/temperature", buffer);
delay(2000);
}

Ensuring stability and automatic reconnection

Wi-Fi signal losses or momentary drops in the MQTT broker are common in industrial warehouses due to physical barriers and interference. The ESP32 code must anticipate these failures and attempt reconnection loops without freezing execution. In practice, implementing a robust error-handling routine prevents unnecessary technician visits to the production line.

Another critical point is managing the internal watchdog timer of the ESP32, which reboots the microcontroller if code gets stuck in an infinite loop. Keeping firmware updated and monitoring network behavior guarantees continuous operation without unpleasant surprises.

Final thoughts on industrial telemetry

Implementing monitoring with ESP32 and MQTT turns industrial process control into an agile and low-cost task. By handling electrical signals correctly, writing resilient code, and structuring communication efficiently, we achieve a highly reliable telemetry network. This approach paves the way for predictive diagnostics and faster decision-making on the factory floor.