RadioHead: an example that runs
Yes. You can use the RadioHead (RH_NRF24 driver) 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, "nrf24_client and nrf24_server: two nRF24 radios on one board", runs on the Arduino Uno R3.
Built in on Mokxi: #include <RH_NRF24.h> and it builds, with nothing to install. Free, and no account needed.
What RadioHead (RH_NRF24 driver) does
RadioHead on nRF24L01 radios: init(), setChannel(), setRF(), send() and waitPacketSent(), available(), recv() and waitAvailableTimeout() with the four-byte header, so RHDatagram and RHReliableDatagram run over an nRF24 too. That is what RadioHead, by Mike McCauley, covers. The sketch below uses it on the Arduino Uno: “nrf24_client and nrf24_server: two nRF24 radios on one board”.
A message to RH_BROADCAST_ADDRESS goes out with no acknowledgment asked for, as upstream sends it.
Simulated. The SPI register calls (spiReadRegister and the rest), setOpMode() and statusRead() are not here, and send() waits for the radio's acknowledgment itself, so isSending() is false once it returns. lastRssi() is 0: the nRF24 measures no RSSI.
The calls follow release 1.143.1 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 | RadioHead (RH_NRF24 driver) |
|---|---|
| Author | Mike McCauley |
| Include | #include <RH_NRF24.h>#include <RadioHead.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 <RH_NRF24.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "RadioHead (RH_NRF24 driver)" by Mike McCauley from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add RadioHead (RH_NRF24 driver) to lib_deps in PlatformIO. Mokxi follows the calls of release 1.143.1, so a sketch written here builds there too.
What to know in the simulator: Built on RF24.h: send() waits for the radio's acknowledgment itself, and the SPI register calls, setOpMode() and statusRead() are not here. lastRssi() is 0, as the nRF24 measures none.
- 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.
RH_NRF24 client(5, 6)
Makes client, the RH_NRF24 object the calls below are made on.
client.init()
Starts it up and returns true if it started, false if it did not. Call it once in setup().
client.setChannel(1)
Sets the channel to the value you pass in. Arguments: channel.
client.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm)
Sets the rf to the value you pass in. Arguments: data rate, power.
server.setModeRx()
Sets the mode rx.
client.send(data, sizeof(data))
Sends it and returns true or false.
client.waitPacketSent()
Waits the packet sent and returns true or false.
Example: nrf24_client and nrf24_server: two nRF24 radios on one board
On the Arduino Uno R3. It runs in the browser exactly as printed.
// RadioHead's nrf24_client and nrf24_server, with both radios on one board
// so one sketch shows both ends.
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 client(5, 6); // CE, CSN
RH_NRF24 server(7, 8);
void setup() {
Serial.begin(9600);
if (!client.init() || !server.init()) {
Serial.println("init failed");
while (1);
}
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
client.setChannel(1);
server.setChannel(1);
client.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
server.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm);
server.setModeRx();
}
void loop() {
Serial.println("Sending to nrf24_server");
uint8_t data[] = "Hello World!";
client.send(data, sizeof(data));
Serial.println(client.waitPacketSent() ? "acknowledged" : "not acknowledged");
client.setModeRx(); // listen for the reply while the server answers
// The server's half.
if (server.available()) {
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (server.recv(buf, &len)) {
Serial.print("got request: ");
Serial.println((char*)buf);
uint8_t reply[] = "And hello back to you";
server.send(reply, sizeof(reply));
server.waitPacketSent();
Serial.println("Sent a reply");
server.setModeRx();
}
}
// The client's half.
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (client.waitAvailableTimeout(500)) {
if (client.recv(buf, &len)) {
Serial.print("got reply: ");
Serial.println((char*)buf);
} else {
Serial.println("recv failed");
}
} else {
Serial.println("No reply, is nrf24_server running?");
}
delay(400);
}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
Keep going
Try RadioHead (RH_NRF24 driver) without installing anything
Open the example, change the sketch, and keep your version in a free account.