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.
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.
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().
userScheduler.addTask(taskSendMessage);
taskSendMessage.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.
// After the library's basic example.
#include "painlessMesh.h"
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
Scheduler userScheduler;
painlessMesh mesh;
void sendMessage();
Task taskSendMessage(TASK_SECOND * 1, TASK_FOREVER, &sendMessage);
void sendMessage() {
String msg = "Hello from node ";
msg += mesh.getNodeId();
bool sent = mesh.sendBroadcast(msg);
Serial.printf("broadcast %s, %u nodes\n", sent ? "sent" : "not sent", (unsigned)mesh.getNodeList().size());
}
void receivedCallback(uint32_t from, String &msg) {
Serial.printf("startHere: Received from %u msg=%s\n", (unsigned)from, msg.c_str());
}
void setup() {
Serial.begin(115200);
mesh.setDebugMsgTypes(ERROR | STARTUP);
mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
mesh.onReceive(&receivedCallback);
userScheduler.addTask(taskSendMessage);
taskSendMessage.enable();
}
void loop() {
mesh.update();
}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.
Try Painless Mesh without installing anything
Open the example, change the sketch, and keep your version in a free account.