RadioHead RH_RF95: LoRa packets, replies and acknowledgments
Yes, with limits. You can use the RadioHead library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (LoRa module, SX1278 (Ra-02)). Part of it is not simulated, and the notes below say which. The example below, "rf95_client and rf95_server: two LoRa radios on one board", runs on the Arduino Uno R3.
Built in on Mokxi: #include <RH_RF95.h> and it builds, with nothing to install. Free, and no account needed.
What RadioHead does
RadioHead by Mike McCauley is a big packet radio library with a driver for each radio chip and managers stacked on top. For LoRa the driver is RH_RF95, which runs the Semtech SX1276, SX1277 and SX1278 on modules such as the HopeRF RFM95W and the Ai-Thinker Ra-02. It is what the Adafruit Feather M0 LoRa tutorials use, and its rf95_client and rf95_server examples are the usual first test of a pair of radios.
The driver adds a four-byte header to every packet (to, from, an ID and flags), so a message carries up to 251 bytes of your own data. send() starts a transmission and waitPacketSent() waits for it to finish; on the other side available() or waitAvailableTimeout() says a packet is in, and recv() copies it out. The managers build on that: RHReliableDatagram adds addresses, acknowledgments and retries, so sendtoWait() returns true only when the other node confirmed it.
How does it differ from the Arduino LoRa library by Sandeep Mistry and from RadioLib? LoRa gives you raw packets with no header and a stream-style beginPacket() and endPacket(); RadioLib covers many more chips and modulations. RadioHead sits between, with the header and the reliable manager built in. In Mokxi only RH_RF95 and the datagram managers are here, RSSI and SNR are fixed numbers, and the example puts two Ra-02 modules on one Uno (NSS 10 and 9, DIO0 on 2 and 3) so one sketch plays both client and server.
| Library Manager name | RadioHead |
|---|---|
| Author | Mike McCauley |
| Include | #include <RH_RF95.h>#include <RH_ASK.h>#include <RHReliableDatagram.h>#include <RHDatagram.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Runs, with a gap the note below names. |
| Parts it drives | LoRa module, SX1278 (Ra-02), RF transmitter, FS1000A, RF receiver, XY-MK-5V |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <RH_RF95.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "RadioHead" by Mike McCauley from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add RadioHead 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: RH_RF95 and RH_ASK here, and RH_NRF24 under its own name: the other drivers, RHMesh, RHRouter and RHSoftwareSPI are not here. RH_ASK takes Timer1 on the AVR boards, as upstream, so it cannot share a sketch with Servo.h; its Timer2 option is not here. The part does not report the CrcOnPayload bit, so a packet sent without a CRC is taken where upstream counts it as bad; RSSI and SNR are fixed numbers.
- 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_RF95 rf95(10, 2)
The radio with its chip select (NSS) on pin 10 and DIO0 on pin 2, which must be an interrupt pin.
rf95.init()
Checks the chip, then sets 434.0 MHz, 13 dBm, 125 kHz bandwidth, coding rate 4/5 and spreading factor 7. Returns false if the radio does not answer.
rf95.setFrequency(915.0)
The carrier in MHz. Use your region’s band and the module’s: 433 for a Ra-02, 868 in Europe or 915 in the Americas for an RFM95W.
rf95.setTxPower(20, false)
Transmit power in dBm, 5 to 23 on the PA_BOOST pin that RFM95 modules use.
rf95.send(data, sizeof(data))
Starts sending a packet of up to RH_RF95_MAX_MESSAGE_LEN bytes and returns at once. Follow it with waitPacketSent().
uint8_t data[] = "Hello World!";
rf95.send(data, sizeof(data));
rf95.waitPacketSent();rf95.waitAvailableTimeout(3000)
Waits up to 3000 ms for a packet and returns true when one is in. available() is the non-blocking check.
rf95.recv(buf, &len)
Copies the received message into buf; len goes in as the buffer size and comes out as the message length.
rf95.lastRssi()
The signal strength of the last packet in dBm. Closer to 0 is stronger; around -120 is the edge.
RHReliableDatagram manager(rf95, CLIENT_ADDRESS)
Wraps the driver with an address of its own. manager.sendtoWait() and recvfromAck() then send and confirm with retries.
Example: rf95_client and rf95_server: two LoRa radios on one board
On the Arduino Uno R3. It runs in the browser exactly as printed.
// RadioHead's rf95_client and rf95_server, with both radios on one board so
// one sketch shows both ends. Each radio's DIO0 is on an interrupt pin.
#include <SPI.h>
#include <RH_RF95.h>
RH_RF95 client(10, 2); // NSS, DIO0
RH_RF95 server(9, 3);
void setup() {
Serial.begin(9600);
if (!client.init() || !server.init()) {
Serial.println("init failed");
while (1);
}
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
client.setFrequency(434.0);
server.setFrequency(434.0);
server.setModeRx();
}
void loop() {
Serial.println("Sending to rf95_server");
uint8_t data[] = "Hello World!";
client.send(data, sizeof(data));
client.waitPacketSent();
client.setModeRx(); // listen for the reply while the server answers
// The server's half.
if (server.waitAvailableTimeout(1000)) {
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (server.recv(buf, &len)) {
Serial.print("got request: ");
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(server.lastRssi(), DEC);
uint8_t reply[] = "And hello back to you";
server.send(reply, sizeof(reply));
server.waitPacketSent();
Serial.println("Sent a reply");
}
}
// The client's half.
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (client.waitAvailableTimeout(3000)) {
if (client.recv(buf, &len)) {
Serial.print("got reply: ");
Serial.println((char*)buf);
} else {
Serial.println("recv failed");
}
} else {
Serial.println("No reply, is rf95_server running?");
}
delay(400);
}Wiring
- Arduino Uno R3 13 to LoRa module, SX1278 (Ra-02) SCK
- Arduino Uno R3 11 to LoRa module, SX1278 (Ra-02) MOSI
- Arduino Uno R3 12 to LoRa module, SX1278 (Ra-02) MISO
- Arduino Uno R3 10 to LoRa module, SX1278 (Ra-02) NSS
- Arduino Uno R3 2 to LoRa module, SX1278 (Ra-02) DIO0
- Arduino Uno R3 3V3 to LoRa module, SX1278 (Ra-02) 3.3V
- Arduino Uno R3 GND to LoRa module, SX1278 (Ra-02) GND
- Arduino Uno R3 13 to LoRa module, SX1278 (Ra-02) SCK
- Arduino Uno R3 11 to LoRa module, SX1278 (Ra-02) MOSI
- Arduino Uno R3 12 to LoRa module, SX1278 (Ra-02) MISO
- Arduino Uno R3 9 to LoRa module, SX1278 (Ra-02) NSS
- Arduino Uno R3 3 to LoRa module, SX1278 (Ra-02) DIO0
- Arduino Uno R3 3V3 to LoRa module, SX1278 (Ra-02) 3.3V
- Arduino Uno R3 GND to LoRa module, SX1278 (Ra-02) GND
Common errors and how to fix them
init failed
The chip did not answer on SPI. Check MOSI, MISO and SCK (11, 12, 13 on an Uno), the NSS pin in the constructor, and that the module runs from 3.3 V, not 5 V. A 5 V Uno needs level shifting on real hardware.
One node sends, the other never receives
The two radios differ in frequency, bandwidth, spreading factor or coding rate. Every setting after init() must match on both ends. Also check the antenna is fitted: an RFM95 with no antenna barely reaches across a desk.
recv() returns false or the sketch hangs waiting
DIO0 is not wired, or is on a pin with no interrupt. The driver learns of a received packet from DIO0, so put it on pin 2 or 3 of an Uno and pass that pin to the constructor.
Questions and answers
RadioHead or the LoRa library: which is easier?
For two nodes swapping text, the LoRa library is fewer lines. RadioHead pays off when you want addresses, acknowledgments and retries without writing them yourself.
Can RadioHead join a LoRaWAN network?
No. RadioHead sends point-to-point LoRa packets between your own radios. LoRaWAN gateways expect a different protocol stack on top of the radio.
Projects that use RadioHead
Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.
Keep going
Try RadioHead without installing anything
Open the example, change the sketch, and keep your version in a free account.