Marcio Cunha

How to monitor server racks with ESP32 and MQTT sensors

Learn how to build an environmental telemetry system for server racks using ESP32 microcontrollers, DHT22 sensors, and the MQTT protocol.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Choosing between DHT22 and DS18B20 sensors depends directly on whether you need relative humidity or just spot temperature inside the racks.
  • The MQTT protocol operates lightly and efficiently on local networks, making it ideal for sending frequent temperature readings without clogging infrastructure.
  • ESP32 microcontrollers offer built-in Wi-Fi connectivity and low cost to centralize environmental data from multiple server enclosures.
  • Integrating telemetry with supervision systems enables automated alerts before overheating damages network equipment.
  • Proper power supply management and physical isolation prevent false readings and ensure continuous monitoring stability.

Why monitor IT rack temperature

Keeping servers within the ideal temperature range is one of the most critical tasks in infrastructure administration. When air conditioning fails or rack airflow is blocked, internal components suffer severe thermal stress, drastically reducing the lifespan of hard drives and processors. In practice, this means a minor oversight in cooling can result in unplanned outages and loss of valuable data. To avoid unpleasant surprises, having an early warning system has become indispensable, whether in corporate data centers or small residential homelabs.

The good news is that you do not need to spend a fortune on proprietary, closed solutions to solve this problem. With accessible electronic components, it is entirely feasible to create a custom network of environmental sensors. This decentralized approach allows you to scatter measurement points exactly where heat accumulates, such as the upper rear of enclosures, where hot air exhausted by servers tends to get trapped.

Choosing sensors and network architecture

The heart of our physical monitoring system is the ESP32 microcontroller, a compact chip featuring built-in Wi-Fi and Bluetooth connectivity at an extremely low price. It handles reading physical environmental values and transmitting them across the local network. To collect temperature data, we have two main options on the market: the DHT22 and the DS18B20. The DHT22 measures both temperature and relative air humidity, while the DS18B20 focuses exclusively on temperature but offers the major advantage of being available in waterproof probes with long cables.

For communication between microcontrollers and the central server, we adopt the MQTT protocol. This is an extremely lightweight messaging system designed specifically for IoT (Internet of Things) devices communicating via a publish-subscribe model. In practice, the ESP32 acts as a publisher sending periodic readings to an intermediary called an MQTT broker, while supervision systems and graphical dashboards listen to these topics to render real-time charts. If you want to understand more about this message-exchange dynamic, check out the guide on how to use MQTT to integrate industrial sensors locally, which delves into topic logic and packet retention.

Assembling the circuit and programming the ESP32

Physical assembly of the prototype requires few components and basic soldering. If you notice signal instabilities or constant wireless drops, it is recommended to review the advice in the article on how to handle an ESP32 losing Wi-Fi and reconnecting with secure MQTT to ensure resilience against connection failures. The code below demonstrates the essential logic to connect the ESP32 to Wi-Fi, read the DHT22 sensor, and publish the resulting JSON to the local MQTT broker.

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

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

#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

WiFiClient espClient;
PubSubClient client(espClient);

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

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

float h = dht.readHumidity();
float t = dht.readTemperature();

if (!isnan(h) && !isnan(t)) {
String payload = "{\"temperature\": " + String(t) + ", \"humidity\": " + String(h) + "}";
client.publish("telemetry/rack1/ambient", payload.c_str());
}
delay(10000);
}

Integrating data with supervision systems

With data flowing via MQTT on the local network, the next step is storing and displaying it in a friendly visual dashboard. Platforms like Home Assistant, Node-RED, or Grafana natively communicate with the MQTT protocol, allowing you to turn raw numbers into colorful panels and sound or visual alerts. When the sensor detects that temperature has exceeded a safe threshold, for instance, the supervision system can trigger a Telegram message or an audible alarm for the on-call team.

Additionally, if you are operating high-power equipment or power supplies that require extra bench care and precise analog measurements, consult the recommendations outlined in the article on how to test bench components and diagnose hardware failures. Maintaining the physical integrity of the power source driving the ESP32 prevents unwanted reboots and ensures rack monitoring runs 24/7 without interruption.

Best practices for installation and continuous operation

Installing sensors in an environment full of power cables and metal enclosures requires extra attention to electromagnetic interference. Try to keep sensor wiring away from high-current electrical lines to prevent noise that could corrupt temperature readings. If you use long cables for the DS18B20 sensor, utilizing proper pull-up resistors and shielded cables makes all the difference in maintaining digital signal stability.

Another critical point is monitoring infrastructure redundancy. Since the MQTT broker and visualization dashboard run on local servers, ensure these services are configured to restart automatically in case of power loss. With a well-planned architecture, the autonomous ESP32 system transforms into a reliable, low-cost sentinel protecting your servers against catastrophic overheating.