RF24Mesh: an example that runs
Yes. You can use the RF24Mesh library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (2.4 GHz radio, nRF24L01+). The example below, "A node joins the mesh and reports to the master, both on one board", runs on the Arduino Uno R3.
Built in on Mokxi: #include <RF24Mesh.h> and it builds, with nothing to install. Free, and no account needed.
What RF24Mesh does
A mesh of nRF24L01 radios on RF24Network.h: setNodeID(), begin(), update(), the master's DHCP() handing out addresses, write(data, type, size, nodeID), getNodeID(), getAddress(), checkConnection() and renewAddress(). That is what RF24Mesh, by TMRh20, covers. The sketch below uses it on the Arduino Uno: “A node joins the mesh and reports to the master, both on one board”.
As upstream: begin() starts the radio (channel 97, 1 Mbps) and, on the master, the network at 00. A node joins at NETWORK_DEFAULT_ADDRESS (04444) and asks level by level: it multicasts a NETWORK_POLL to the master's level, then the next, and every node there that can take children answers.
mesh.write(data, type, size, nodeID) looks the address up (on the master, in its list; elsewhere by asking the master) and writes it; getNodeID(), getAddress(), checkConnection(), renewAddress(), releaseAddress(), setChild(), setChannel(), setAddress(), addrList and addrListTop, mesh_address and setCallback() are upstream's.
The calls follow release 2.0.2 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.
| Library Manager name | RF24Mesh |
|---|---|
| Author | TMRh20 |
| Include | #include <RF24Mesh.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | 2.4 GHz radio, nRF24L01+ |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <RF24Mesh.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "RF24Mesh" by TMRh20 from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add RF24Mesh to lib_deps in PlatformIO. Mokxi follows the calls of release 2.0.2, so a sketch written here builds there too.
What to know in the simulator: Nodes join with upstream's poll and address request frames, at any depth. saveDHCP() and loadDHCP() and the nRF52 radio are not here, and the master's list holds 16 nodes. setCallback()'s function also runs during lookups, so two radios on one board can mesh.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
RF24Mesh master(radioMaster, networkMaster)
Makes master, the RF24Mesh object the calls below are made on.
master.update()
Updates it and returns a whole number.
master.setNodeID(0)
Sets the node id to the value you pass in. Arguments: node id.
master.begin()
Starts it up and returns true if it started, false if it did not. Call it once in setup(). Arguments: channel, data rate, timeout.
node.setCallback(serveMaster)
Sets the callback.
master.getNodeID(header.from_node)
Returns the node id as a whole number. Arguments: address.
Example: A node joins the mesh and reports to the master, both on one board
On the Arduino Uno R3. It runs in the browser exactly as printed.
// RF24Mesh_Example (node 1) and RF24Mesh_Example_Master (node 0), with
// both radios on one board so one sketch shows both ends.
#include "RF24.h"
#include "RF24Network.h"
#include "RF24Mesh.h"
RF24 radioMaster(5, 6); // CE, CSN
RF24Network networkMaster(radioMaster);
RF24Mesh master(radioMaster, networkMaster);
RF24 radioNode(7, 8);
RF24Network networkNode(radioNode);
RF24Mesh node(radioNode, networkNode);
// The master's loop: keep the network up and hand out addresses.
void serveMaster() {
master.update();
master.DHCP();
}
void setup() {
Serial.begin(115200);
master.setNodeID(0);
if (!master.begin()) {
Serial.println(F("Radio hardware not responding."));
while (1) {}
}
node.setNodeID(1);
node.setCallback(serveMaster); // runs while the node waits for its address
Serial.println(F("Connecting to the mesh..."));
if (!node.begin()) {
Serial.println(F("Could not join the mesh"));
while (1) {}
}
Serial.print(F("node 1 joined at address 0"));
Serial.println(node.mesh_address, OCT);
}
uint32_t displayTimer = 0;
void loop() {
node.update();
serveMaster();
// The node: send the master millis() every 300 ms.
if (millis() - displayTimer >= 300) {
displayTimer = millis();
if (!node.write(&displayTimer, 'M', sizeof(displayTimer))) {
Serial.println(F("Send fail"));
} else {
Serial.print(F("Send OK: "));
Serial.println(displayTimer);
}
}
// The master: read what came in.
while (networkMaster.available()) {
RF24NetworkHeader header;
uint32_t dat = 0;
networkMaster.read(header, &dat, sizeof(dat));
if (header.type == 'M') {
Serial.print(F("master got "));
Serial.print(dat);
Serial.print(F(" from node "));
Serial.println(master.getNodeID(header.from_node));
}
}
}Wiring
- Arduino Uno R3 13 to 2.4 GHz radio, nRF24L01+ SCK
- Arduino Uno R3 11 to 2.4 GHz radio, nRF24L01+ MOSI
- Arduino Uno R3 12 to 2.4 GHz radio, nRF24L01+ MISO
- Arduino Uno R3 5 to 2.4 GHz radio, nRF24L01+ CE
- Arduino Uno R3 6 to 2.4 GHz radio, nRF24L01+ CSN
- Arduino Uno R3 3V3 to 2.4 GHz radio, nRF24L01+ VCC
- Arduino Uno R3 GND to 2.4 GHz radio, nRF24L01+ GND
- Arduino Uno R3 13 to 2.4 GHz radio, nRF24L01+ SCK
- Arduino Uno R3 11 to 2.4 GHz radio, nRF24L01+ MOSI
- Arduino Uno R3 12 to 2.4 GHz radio, nRF24L01+ MISO
- Arduino Uno R3 7 to 2.4 GHz radio, nRF24L01+ CE
- Arduino Uno R3 8 to 2.4 GHz radio, nRF24L01+ CSN
- Arduino Uno R3 3V3 to 2.4 GHz radio, nRF24L01+ VCC
- Arduino Uno R3 GND to 2.4 GHz radio, nRF24L01+ GND
Try RF24Mesh without installing anything
Open the example, change the sketch, and keep your version in a free account.