Projects / ESP32 mesh network

ESP32 mesh network

Advanced4 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Install the library

    painlessMesh and its dependencies.

  2. ESP32

    Flash two nodes

    Same name and password.

  3. ESP32

    Watch them join

    Serial messages.

  4. ESP32DS18B2027.6°C

    Add the temperature

    Broadcast every 10 seconds.

  5. ESP32

    Add more nodes

    Spread them out.

  6. ESP32

    Mark one as the root

    Plug it into a computer.

  7. ESP32

    Switch a node off

    See the mesh heal.

In short

How do you build an ESP32 mesh network?

Many ESP32s form a self-healing mesh so sensors far from the router still get data home. You need ESP32 DevKit V1 × 4, DS18B20 Temperature Sensor × 3 and Resistor 4.7 kΩ × 3. It takes 7 steps and about 240 minutes.

Level
Advanced
Time
About 240 min
Parts
3

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
DS18B20 data (each sensor node)GPIO4, with 4.7 kΩ to 3V3Temperature
DS18B20 VCC and GNDESP32 3V3 and GNDPower
All nodesUSB or 5 VPower
Root nodeUSB to a computerPrints what the mesh sends

Code

In a mesh, every node forwards messages for its neighbours, so a sensor at the end of the garden can send its reading through two other nodes to reach the one near the house. The painlessMesh library handles joining, routing and healing; every node runs the same sketch, with the mesh name and password the same on all, and exactly one node marked as root. Put four boards in different rooms or at different ends of a garden, plug the root into a computer, and watch the readings arrive. Switch one in the middle off and watch the others find a new route within about a minute. A mesh uses Wi-Fi channels and keeps radios on, so nodes cost more power than deep-sleep sensors; for battery nodes, other designs are better. To forward the data to MQTT, add a bridge on the root as a next step. Change the mesh password to your own and keep it private. The library needs the ESP32 package and its dependencies installed from the Library Manager. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// painlessMesh node: every 10 s it broadcasts its temperature; the root prints everything it hears.
// Libraries: painlessMesh (with ArduinoJson, TaskScheduler, AsyncTCP). Board: ESP32 Dev Module. Same sketch on every node.
#include "painlessMesh.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define MESH_PREFIX   "ElectroLarkMesh"
#define MESH_PASSWORD "CHANGE_THIS_MESH_PASSWORD"
#define MESH_PORT     5555
#define IS_ROOT       false                          // set true on exactly one node
Scheduler userScheduler;
painlessMesh mesh;
OneWire ow(4);
DallasTemperature probe(&ow);
void sendReading();
Task taskSend(TASK_SECOND * 10, TASK_FOREVER, &sendReading);
void sendReading() {
  probe.requestTemperatures();
  String msg = String("{\"node\":") + mesh.getNodeId() + ",\"temp\":" + String(probe.getTempCByIndex(0), 1) + "}";
  mesh.sendBroadcast(msg);
}
void receivedCallback(uint32_t from, String &msg) {
  Serial.printf("From %u: %s\n", from, msg.c_str());   // on the root this shows every node's readings
}
void newConnectionCallback(uint32_t nodeId) { Serial.printf("New node %u joined\n", nodeId); }
void droppedConnectionCallback(uint32_t nodeId) { Serial.printf("Node %u left\n", nodeId); }
void setup() {
  Serial.begin(115200);
  probe.begin();
  mesh.setDebugMsgTypes(ERROR | STARTUP);
  mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onDroppedConnection(&droppedConnectionCallback);
  if (IS_ROOT) { mesh.setRoot(true); }
  mesh.setContainsRoot(true);
  userScheduler.addTask(taskSend);
  taskSend.enable();
}
void loop() { mesh.update(); }

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Nodes find each otherJoin messages appear
RootPrints every reading
Middle node offOthers reconnect within a minute
Far nodeStill reaches the root

If it does not work

Nodes do not join

Check name, password and port on all.

Compile errors

Install the missing dependencies.

Few messages

Check the temperature probe wiring.

Nodes reset

Check the power supply.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32 mesh network?

ESP32 DevKit V1 × 4, DS18B20 Temperature Sensor × 3 and Resistor 4.7 kΩ × 3.

How long does the ESP32 mesh network take to build?

About 240 minutes for a advanced maker, in 7 steps.

How do I know the ESP32 mesh network works?

Nodes find each other: Join messages appear. Root: Prints every reading. Middle node off: Others reconnect within a minute.

Nodes do not join: what should I check?

Check name, password and port on all.

Compile errors: what should I check?

Install the missing dependencies.

Few messages: what should I check?

Check the temperature probe wiring.

Nodes reset: what should I check?

Check the power supply.