ESP32 Mesh WiFi Project Code Easy

📅 Mar 30, 2026
👁️ 5 Views
✅ Verified
ESP32 Mesh WiFi Project Code Easy

Hey there! If you're looking to get a simple ESP32 mesh WiFi project up and running, you're in the right place. Think of a mesh network like a group of friends who can all talk to each other directly, not just through one central person. This guide will walk you through the basics in a straightforward way.

Getting Started with Your ESP32 Mesh

First things first, you'll need a couple of ESP32 boards. The idea is to make them form their own little network where they can pass messages back and forth, even if one of them can't see your main WiFi router.

What You Need to Do

  • Set up the ESP32 Mesh: We'll use a library called painlessMesh which makes this whole process much simpler.
  • Connect Your Devices: All your ESP32s need to be configured to talk to each other on the same mesh network.
  • Upload the Code: I'll show you a basic example to upload to each board.
  • Test the Connection: Send a message from one board and see if the others receive it.

A Simple Code Example

Let's look at a basic script. This code sets up a mesh network and lets each board broadcast a "Hello from node" message.

arduino
#include 

// Set your WiFi credentials for the mesh
#define MESH_PREFIX "myMeshNetwork"
#define MESH_PASSWORD "myMeshPassword"
#define MESH_PORT 5555

// Create a mesh object
painlessMesh mesh;

void setup() {
  Serial.begin(115200);
  
  // Initialize the mesh
  mesh.init(MESH_PREFIX, MESH_PASSWORD, MESH_PORT);
  
  // What to do when a message is received
  mesh.onReceive([](uint32_t from, String &msg) {
    Serial.printf("Received from %u: %s\n", from, msg.c_str());
  });
}

void loop() {
  // This keeps the mesh network running
  mesh.update();
  
  // Every 5 seconds, send a hello message
  static unsigned long lastSent = 0;
  if (millis() - lastSent > 5000) {
    String msg = "Hello from node ";
    msg += mesh.getNodeId();
    mesh.sendBroadcast(msg);
    lastSent = millis();
    Serial.println("Message sent!");
  }
}

To use this, you'll need to install the painlessMesh library in your Arduino IDE. Go to Sketch > Include Library > Manage Libraries... and search for "painlessMesh".

Steps to Make It Work

  1. Install the painlessMesh library in the Arduino IDE as mentioned above.
  2. Connect your first ESP32 to your computer via USB.
  3. Copy the code above into a new sketch.
  4. Select your ESP32 board and the correct COM port from the Tools menu.
  5. Click Upload. Once it's done, open the Serial Monitor (set to 115200 baud) to see the messages.
  6. Repeat steps 2-5 for your second (and third, etc.) ESP32 board. Use the exact same code for each one.
  7. Watch the Serial Monitor on each board. You should see them sending and receiving "Hello" messages from each other, showing they've formed a mesh.

Why Use a Mesh Network?

A regular WiFi setup needs every device to be in range of the main router. A mesh network is different. Each ESP32 (or "node") can talk to its neighbors. If one node is too far from the router, it can still send data by hopping through other nodes that are within range. This is great for covering a larger area, like in a big home, a farm, or an industrial site. For more on how mesh networking extends range, you can read this external guide on Wi-Fi Mesh Networking.

If you need to work with other data formats in your projects, check out our JSON Editor and CSV Editor tools.

Frequently Asked Questions

How many ESP32 devices can I connect in a mesh?

You can connect quite a few! The painlessMesh library can theoretically handle hundreds of nodes, but in practice, it depends on your network traffic and the memory on your ESP32. For most hobby projects, 5-10 devices work very well.

Can I connect this mesh to my home internet?

Yes, you can. One of the ESP32 nodes can be set up as a "root" node that connects to your home WiFi router. This allows the whole mesh to send data to the internet. The painlessMesh library has examples for this bridge setup.

My devices aren't talking to each other. What should I check?

First, make sure the MESH_PREFIX and MESH_PASSWORD are identical in the code on every single ESP32. Second, check that they are all powered on and within a reasonable range (start with them in the same room). Finally, open the Serial Monitor for each one to see if there are any error messages.

Do I need a special router?

No, you don't. The ESP32s create their own mesh network independently. Your existing home router isn't involved in the direct communication between the ESP32 nodes.