How to Build an ESP32 Mesh Network with UART Gateway and MQTT Integration
By K Taylor 05\29\2025

How to Build an ESP32 Mesh Network with UART Gateway and MQTT Integration

Building an ESP32 Mesh Network with UART Gateway and MQTT Integration

Learn how to create a self-healing ESP32 mesh network using the painlessMesh library, with a UART-connected gateway node that relays messages to a WiFi-enabled MQTT broker for real-time data integration

Building an ESP32 Mesh Network with UART Gateway and MQTT Integration

This article explores how to create a self-healing ESP32 mesh network using the painlessMesh library, with a dedicated UART gateway node that communicates with a WiFi-connected MQTT node. This setup allows real-time data sharing across ESP32 devices, while relaying messages to external systems like Raspberry Pi or Node-RED dashboards.

Overview of the Setup

Each ESP32 node joins a mesh network defined by a shared SSID and password. The painlessMesh library handles peer discovery, message routing, and scheduling. One special node?the gateway?acts as a bridge between the mesh and the external network through UART.

  • Regular Nodes: Broadcast messages and relay any received messages across the mesh.
  • Gateway Node: Sends and receives mesh messages via UART to a connected MQTT WiFi node.
  • MQTT Node: Forwards data from the UART interface to a WiFi-based MQTT broker like Node-RED.

Libraries Used

- painlessMesh.h ? for mesh networking
- Scheduler ? for running timed tasks
Documentation: Scheduler Library

1. Regular ESP32 Node Code

#include 
#include "painlessMesh.h"

#define MESH_PREFIX "khometekconn"
#define MESH_PASSWORD "L8*ITX93IFFKS"
#define MESH_PORT 5555

Scheduler userScheduler;
painlessMesh mesh;

void sendMessage();
Task taskSendMessage(TASK_SECOND * 1 , TASK_FOREVER, &sendMessage);

void sendMessage() {
  String msg = "Hi from ESP32_mqtt_connect_1 ";
  mesh.sendBroadcast(msg);
  taskSendMessage.setInterval(TASK_SECOND * 4);
}

void receivedCallback(uint32_t from, String &msg) {
  Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}

void newConnectionCallback(uint32_t nodeId) {
  Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}

void changedConnectionCallback() {
  Serial.printf("Changed connections\n");
}

void nodeTimeAdjustedCallback(int32_t offset) {
  Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
}

void setup() {
  Serial.begin(115200);
  mesh.setDebugMsgTypes(ERROR | STARTUP);
  mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  userScheduler.addTask(taskSendMessage);
  taskSendMessage.enable();
}

void loop() {
  mesh.update();
}

2. ESP32 Gateway Node Code (UART Bridge)

#include 
#include "painlessMesh.h"

#define MESH_PREFIX "khometekconn"
#define MESH_PASSWORD "L8*ITX93IFFKS"
#define MESH_PORT 5555

Scheduler userScheduler;
painlessMesh mesh;

void sendMessage();
Task taskSendMessage(TASK_SECOND * 1 , TASK_FOREVER, &sendMessage);

void sendMessage() {
  String msg = "Hi from ESP32_gateway_2 ";
  mesh.sendBroadcast(msg);
  taskSendMessage.setInterval(TASK_SECOND * 4);

  while(Serial2.available()) {
    String info = Serial2.readStringUntil('\n');
    mesh.sendBroadcast(info + " from mqtt");
  }
}

void receivedCallback(uint32_t from, String &msg) {
  Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
  Serial2.println("from " + String(from) + " " + msg);
}

void newConnectionCallback(uint32_t nodeId) {
  Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}

void changedConnectionCallback() {
  Serial.printf("Changed connections\n");
}

void nodeTimeAdjustedCallback(int32_t offset) {
  Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
}

void setup() {
  Serial.begin(115200);
  Serial2.begin(115200);
  mesh.setDebugMsgTypes(ERROR | STARTUP);
  mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  userScheduler.addTask(taskSendMessage);
  taskSendMessage.enable();
}

void loop() {
  mesh.update();
}

3. MQTT WiFi Node (Serial Forwarder)

#include 

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

void loop() {
  while(Serial.available()) {
    Serial2.write(Serial.read());
  }
  while(Serial2.available()) {
    Serial.write(Serial2.read());
  }
}

4. UART Pin Connections

Use ESP32's built-in UART pins to link the Gateway node and MQTT WiFi node:

  • P17 (TX) on gateway -> P16 (RX) on MQTT node
  • P16 (RX) on gateway -> P17 (TX) on MQTT node

For a streamlined view of my notes on this, please check this PDF I created, ESP32 Mesh Network.

This ESP32 mesh network enables a scalable and wireless communication method among multiple devices. With a UART-based gateway forwarding messages to an MQTT-enabled node, it's easy to log, analyze, or visualize the data using tools like Node-RED or a Raspberry Pi.

This video below will show the illustration of the Mesh Network

Resources

No additional pages available.