BLE Mesh Network: Smart Lighting Control with ESP32
A BLE mesh network for smart lighting control with ESP32 gives you the ability to control dozens or even hundreds of lights across a large space — a home, office, factory floor, or retail store — using a single Bluetooth Low Energy infrastructure. Unlike point-to-point BLE, mesh networking allows every node to relay messages, creating a self-healing, range-extending communication fabric that does not depend on a central hub or WiFi router. In this guide, we explore the architecture, the ESP-BLE-Mesh stack, hardware selection, and a complete implementation walkthrough suitable for Indian makers and professionals.
Introduction to BLE Mesh Networking
Bluetooth Mesh (standardised by the Bluetooth SIG in 2017) is a many-to-many topology built on top of Bluetooth Low Energy advertising. Each node in the mesh can send, receive, and relay messages — a process called message relaying or managed flooding. Because there is no single point of failure, the network continues to function even if several nodes go offline.
Key BLE Mesh concepts you need to understand before building:
- Nodes: Any device participating in the mesh. Can be a light, a switch, a sensor, or a gateway.
- Elements: Addressable sub-units within a node. A single ESP32 can have multiple elements (e.g., two independently controllable LED channels).
- Models: Standard or vendor-specific state machines assigned to elements. The Generic OnOff Server model is used for simple on/off lights; the Light Lightness Server model for dimmable lights.
- Provisioner: The device (typically a smartphone app or a dedicated ESP32) that adds new nodes to the network, assigns addresses, and distributes encryption keys.
- Publish/Subscribe: Nodes publish state changes to group addresses; other nodes subscribed to the same address act on the messages. This is how you control a room full of lights with a single button press.
Ai Thinker NodeMCU-32S-ESP32 Development Board – IPEX Version
The NodeMCU-32S with its dual-core Xtensa processor and integrated BLE 4.2 is an ideal platform for BLE mesh lighting nodes at an affordable price.
Why ESP32 Is Perfect for BLE Mesh Lighting
The ESP32 is one of the very few microcontroller modules that supports full BLE Mesh out of the box, thanks to Espressif’s open-source ESP-BLE-Mesh component included in ESP-IDF v4.0+. Here is why it stands out for lighting applications in India:
| Feature | ESP32 | Nordic nRF52840 | Silicon Labs EFR32 |
|---|---|---|---|
| BLE Mesh support | Yes (ESP-IDF) | Yes (Zephyr) | Yes (Simplicity) |
| WiFi (for gateway) | Yes (built-in) | No | No (separate chip) |
| Cost in India | ₹200-400 | ₹800-1500 | ₹1000-2000 |
| Arduino support | Yes | Partial | Limited |
| PWM channels | 16 | 8 | 8 |
The dual-radio architecture of ESP32 also enables a crucial use case: a single node can act as both a BLE Mesh relay and a WiFi gateway simultaneously, bridging the mesh network to the internet for remote control via MQTT or HTTP APIs.
System Architecture and Node Roles
A typical ESP32 BLE Mesh smart lighting deployment consists of the following node types:
1. Light Node (Server)
This is the light fixture itself — an ESP32 module connected to an LED driver, relay, or PWM-controlled dimmer. It implements the Generic OnOff Server model and/or the Light Lightness Server model. It listens for messages addressed to its unicast address or subscribed group addresses, and controls the light output accordingly.
2. Switch Node (Client)
A wall switch or wireless button implements the Generic OnOff Client model. When pressed, it publishes an OnOff Set message to a group address. All light nodes subscribed to that group address respond simultaneously — creating a many-to-one or many-to-many control relationship.
3. Sensor Node (optional)
A PIR motion sensor or ambient light sensor node can automatically publish messages to turn lights on/off based on occupancy or lux levels. This is particularly useful for corridors, stairwells, and offices in Indian commercial buildings trying to reduce electricity bills.
4. Gateway Node
An ESP32 acting as both BLE Mesh node and WiFi station. It receives mesh messages and forwards them to an MQTT broker (running on a cloud server or locally on a Raspberry Pi), enabling smartphone and voice assistant control without a separate hub.
AC 220V Security PIR Human Body Motion Sensor Detector
Add occupancy-based automation to your BLE mesh lighting system — have lights turn on automatically when someone enters a room.
Provisioning and Network Configuration
Provisioning is the process of bringing an unprovisioned device (a fresh ESP32) into the mesh network. The Bluetooth Mesh standard defines two provisioning bearers: PB-ADV (advertising) and PB-GATT (for mobile apps). For an ESP32-based system, you have two options:
Option A: Smartphone Provisioner App
Use the nRF Mesh app (Nordic Semiconductor, available for Android and iOS) or Espressif’s EspBlufiApp. These apps scan for unprovisioned devices, assign network and application keys, configure publish/subscribe addresses, and set relay/proxy flags. This is the easiest approach for small deployments.
Option B: ESP32 Provisioner Node
For commercial or large-scale deployments, dedicate one ESP32 as the provisioner. This node runs the provisioner role code from ESP-IDF’s BLE Mesh examples and can automatically provision new nodes when they are powered on, using a pre-shared provisioning key baked into the firmware. This enables factory-floor provisioning without any app.
After provisioning, you configure:
- AppKey binding: Each model on each node must have an AppKey bound to it before it can send or receive messages.
- Publication address: The address a node publishes its state changes to. For a switch, this is typically a group address like 0xC000 (“Living Room Lights”).
- Subscription address: The address a node listens on. For a light node in the living room, subscribe to 0xC000.
Code Implementation: Light Node and Switch Node
The following examples use ESP-IDF v5.x with the esp-ble-mesh component. The Arduino BLE Mesh library exists but is limited; for production projects, use ESP-IDF directly or via PlatformIO.
Light Node (Generic OnOff Server)
// Simplified light node — see ESP-IDF examples for full implementation
#include "esp_ble_mesh_defs.h"
#include "esp_ble_mesh_common_api.h"
#include "esp_ble_mesh_networking_api.h"
#include "esp_ble_mesh_provisioning_api.h"
#include "esp_ble_mesh_config_model_api.h"
#include "esp_ble_mesh_generic_model_api.h"
#define LED_PIN GPIO_NUM_2
// Called when OnOff Set message received
static void generic_onoff_set(esp_ble_mesh_generic_server_cb_event_t event,
esp_ble_mesh_generic_server_cb_param_t *param) {
if (param->ctx.recv_op == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
uint8_t state = param->value.set.onoff.onoff;
gpio_set_level(LED_PIN, state);
ESP_LOGI("LIGHT", "Light turned %s", state ? "ON" : "OFF");
}
}
Switch Node (Generic OnOff Client)
// Send OnOff Set message to group address 0xC000
void send_light_toggle(bool on) {
esp_ble_mesh_generic_client_set_state_t set = {
.onoff_set = {
.op_en = false,
.onoff = on ? 1 : 0,
.tid = tid++,
}
};
esp_ble_mesh_client_common_param_t common = {
.opcode = ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET,
.model = onoff_client_model,
.ctx = {
.net_idx = APP_NET_IDX,
.app_idx = APP_KEY_IDX,
.addr = 0xC000, // Group address
.send_ttl = 7,
},
.msg_timeout = 0,
};
esp_ble_mesh_generic_client_set_state(&common, &set);
}
Ai Thinker ESP32-C3-01M Wi-Fi + BLE Module
The compact C3 module supports BLE Mesh and is perfect for embedding inside light fixtures where space is limited.
Scaling Up: Multi-Room and Commercial Deployments
BLE Mesh is designed to scale. The standard supports up to 32,767 unicast addresses per network, and with proper group address planning you can control thousands of lights independently and in groups. Here is a practical room/zone addressing scheme for a typical Indian office building:
| Zone | Group Address | Nodes |
|---|---|---|
| All lights (master) | 0xFFFF (broadcast) | All nodes |
| Floor 1 | 0xC001 | All floor 1 nodes |
| Conference Room A | 0xC010 | Conf room lights |
| Corridor | 0xC020 | Corridor fixtures |
For ranges larger than a single floor (BLE typically covers 10-20 metres per hop), the relay feature automatically extends coverage. Enable the relay feature on nodes that are physically spread out to create a reliable message path. In very large buildings, you may need to tune the TTL (Time To Live) value — the number of relay hops a message can take — to ensure messages reach far nodes without creating excessive network traffic.
Frequently Asked Questions
Q: What is the maximum range of a BLE Mesh network with ESP32?
A: Each ESP32 node has a BLE range of approximately 10-30 metres depending on obstacles. With relay nodes every 10-15 metres, a mesh network can theoretically cover an entire building floor. The maximum path length (TTL) defaults to 7 hops, giving roughly 70-200 metres of end-to-end range in a chain topology, and much more in a dense mesh.
Q: Can I mix ESP32 BLE Mesh nodes with commercial smart bulbs?
A: Only if the commercial bulbs support the Bluetooth Mesh standard (not all do — some use proprietary protocols). Bulbs certified as “Bluetooth Mesh” compliant (check the Bluetooth SIG qualification list) should interoperate with ESP32 mesh nodes that implement the same standard models. However, vendor-specific models will not be compatible.
Q: Does BLE Mesh work without a smartphone or hub?
A: Yes. Once provisioned, a BLE Mesh network operates entirely independently. A wall switch node can control light nodes directly with no hub, no internet, and no smartphone. A gateway is only needed if you want remote control from outside the building or voice assistant integration.
Q: How does BLE Mesh handle power outages?
A: When power is restored, each node boots up and re-joins the network using its stored provisioning data (stored in NVS flash). There is typically a 2-5 second startup delay per node. Nodes restore their last known state if you implement state persistence using the Preferences/NVS library.
Build Your BLE Mesh Lighting System
Get all the ESP32 modules, sensors, and accessories you need to build a professional BLE mesh smart lighting system. Zbotic.in offers fast delivery across India with genuine components.
Add comment