Source: https://mokxi.com/libraries/painlessmesh
Updated: 2026-11-22

Libraries / Network

Arduino library · Network

# painlessMesh: a self-organizing WiFi mesh of ESP boards

Yes, you can build with the Painless Mesh library online in Mokxi with nothing to install. Its hardware is not simulated, so a sketch says plainly that it cannot reach the device. The example below, "A mesh node alone: its tasks run, nobody answers", runs on the ESP32-C3-DevKitM-1.

Run the example in Mokxi See the calls

Built in on Mokxi: #include <painlessMesh.h> and it builds, with nothing to install. Free, and no account needed.

## What Painless Mesh does

painlessMesh links several ESP32 and ESP8266 boards into a network with no router. Every node runs its own access point and also joins a neighbor’s, the library builds the tree of connections on its own and repairs it when a node leaves, and a message sent to the mesh hops from node to node until it reaches everyone or the one node it was addressed to. Messages are short strings, usually JSON.

Every node runs the same sketch. mesh.init() takes the mesh name, password, a TaskScheduler and a port, all of which must match across nodes. onReceive() registers the function that gets each message with the sender’s node ID. Periodic work is a Task added to the scheduler rather than a delay(), and mesh.update() runs the mesh and the tasks, so it must be called on every pass of loop().

The example here is the library’s basic sketch on an ESP32-C3: a task broadcasts "Hello from node" with the board’s ID every second. In Mokxi a node runs alone. A mesh needs other boards, an access point of each board’s own and TCP links, none of which is simulated, so the node starts, gets its ID and runs its tasks, but never finds a neighbor and its sends return false. init() says so on Serial.

Library Manager name | Painless Mesh

Author | Coopdis, Scotty Franzyshen, Edwin van Leeuwen, Germán Martín, Maximilian Schwarz, Doanh Doanh

Include | #include <painlessMesh.h>

Boards | ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM, ESP8266 NodeMCU

In the simulator | Builds and runs, and says plainly that the hardware behind it is not simulated.

## Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM, ESP8266 NodeMCU.

In Mokxi it is built in: #include <painlessMesh.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Painless Mesh" by Coopdis, Scotty Franzyshen, Edwin van Leeuwen, Germán Martín, Maximilian Schwarz, Doanh Doanh from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Painless Mesh to lib_deps in PlatformIO. Mokxi follows the calls of release 1.5.7, so a sketch written here builds there too.

What to know in the simulator: A mesh needs other boards, an access point of each board’s own and TCP links between them, none of which is simulated: the node starts with its node id and runs its tasks in update(), never finds a neighbor, and its sends return false. init() says so on Serial.

- ESP32-C3
- ESP32-C6
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32 DevKit
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- ESP32-S3-DevKitC-1
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- ESP32-S3-CAM
- ESP8266 NodeMCU

## The calls you will use

In the order a sketch meets them.

### mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT)

Starts the node with the mesh name, password, your Scheduler and the TCP port. All four must be identical on every node.

### mesh.onReceive(&receivedCallback)

Registers void receivedCallback(uint32_t from, String &msg), called for each message that reaches this node.

### mesh.sendBroadcast(msg)

Sends a String to every other node. Returns false when no node is connected.

### mesh.sendSingle(nodeId, msg)

Sends a String to one node by its 32-bit ID, routed through the mesh.

### mesh.update()

Runs the mesh and the scheduler’s tasks. The only thing loop() should contain in most sketches.

### mesh.getNodeId()

This node’s ID, a 32-bit number made from its MAC address, the same after every restart.

### Task taskSendMessage(TASK_SECOND * 1, TASK_FOREVER, &sendMessage)

A TaskScheduler task that calls sendMessage() every second, forever. Add it with userScheduler.addTask() and call enable().

### mesh.onNewConnection(&newConnectionCallback)

Registers a function given the node ID of each neighbor that joins.

## Example: A mesh node alone: its tasks run, nobody answers

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino · ESP32-C3-DevKitM-1

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### Two nodes sit side by side and never connect

Their MESH_PREFIX, MESH_PASSWORD or MESH_PORT differ, even by one letter or a trailing space. Copy the same three defines into both sketches. Turn on CONNECTION in setDebugMsgTypes() to watch the nodes look for each other.

### The mesh keeps breaking apart and re-forming

Something blocks loop(), so update() runs too rarely to answer neighbors. Remove every delay() and move periodic work into Tasks; a node that stalls for a second or two gets dropped by the others.

### The mesh works until one node also joins the home router

A board’s radio can be on one channel only, and the mesh then has to follow the router’s. Pass the router’s channel to init(), or use stationManual() on just one bridge node, as the library’s bridge example does.

## Questions and answers

### painlessMesh or ESP-NOW?

ESP-NOW sends one hop, board to board, with no routing and very low delay, and Mokxi simulates it between boards. painlessMesh forwards messages across many hops and heals itself, at the cost of more memory and setup.

## Keep going

### Learn

- ESP32 Projects for Beginners You Can Run Right Now
- ESP32 IoT Projects for Final Year Students

### More network libraries

- PubSubClient
- MQTT
- Blynk
- ThingSpeak
- WiFiManager
- WiFi, HTTPClient and WebServer (ESP32 core)
- WebSockets
- NTPClient

Every built-in library · Open the editor

## Try Painless Mesh without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
