RadioLib Documentation

repository·master·Indexed 25 days ago

https://github.com/jgromes/radiolib

A universal wireless communication library for embedded devices providing a consistent interface for various radio modules (including SX126x, SX127x, RFM9x, and CC1101) and protocols such as LoRaWAN, FSK, OOK, and AX.25. It features a hardware abstraction layer (HAL) supporting Arduino, ESP-IDF, and Tock OS, with specific support for LoRaWAN Class A, C, and Multicast.

Tokens
17.1K
Snippets
27
Records
105
Agent score
72%

What's inside RadioLib

  1. Overview of RadioLib

    master

    RadioLib is a universal wireless communication library for embedded devices designed to integrate various wireless communication modules, protocols, and digital modes into a single consistent system. It provides a hardware abstraction layer that allows it to run in both Arduino and non-Arduino environments.

    Key resources:

  2. Choose a LoRaWAN activation method: OTAA vs ABP

    master

    RadioLib supports both Over-the-Air Activation (OTAA) and Activation by Personalization (ABP).

    • OTAA (Recommended): The standard and preferred method for most use cases.
    • ABP: Use the LoRaWAN_ABP example if you specifically require ABP.

    Warning: When using ABP, the full session must persist through resets and power loss to comply with the LoRaWAN specification. You will need to implement proper Non-Volatile Memory (NVM) handling to manage this.

  3. Best practices for LoRaWAN Uplink Intervals and Airtime

    master

    LoRaWAN is designed for low-power, infrequent transmissions.

    • Uplink Interval: Aim for intervals between 15 minutes and once per day to maximize battery life (2-5 years on AA batteries).
    • Duty Cycle: Be aware of legal limits on transmission time (often 1% or 0.1% depending on the region).
    • Fair Use Policy (FUP): On TTN Sandbox, limit airtime to 30 seconds per day and approximately 10 downlinks per day. A common recommendation is 1 downlink per fortnight for configuration updates.
    • Payload Efficiency: Avoid sending text/strings. Send data as a sequence of bytes to minimize airtime and improve reliability. For example, split a 16-bit integer into two bytes using highByte() and lowByte() before transmission.
  4. Use LoRaWAN Class C and Multicast

    master

    RadioLib provides support for advanced LoRaWAN device classes and communication patterns:

    • Class C: Useful for continuously-powered devices (e.g., mains-powered lights) that need to receive data at any time. This is implemented on top of Class A using the LoRaWAN_Class_C example.
    • Multicast: If you are deploying groups of similar devices (e.g., a series of street lights), use Multicast over Class C instead of individual Class C implementations. See the LoRaWAN_Multicast example for implementation details.
  5. Getting started with LoRaWAN in RadioLib

    master

    For new users, the LoRaWAN_Starter example is the recommended entry point. It is highly recommended to read the accompanying notes.md file within that example directory to understand LoRaWAN fundamentals and specific RadioLib implementation details before proceeding.

    Note: These examples are intended for rapid development. For production-ready devices, you must implement persistence (e.g., using NVM) to ensure session data survives resets and power loss. See the radiolib-persistence repository for guidance.

  6. Configure LoRaWAN credentials in config.h

    master

    To connect your device to The Things Network (TTN), you must populate the EUI and Key variables in config.h.

    1. devEUI: Copy the DevEUI from the TTN console (ensure it includes the 0x prefix). It is a uint64_t.
    2. appKey and nwkKey: In the TTN console, click the eye icon and then the <> icon to get the correctly formatted key. Copy this and paste it over the existing 0x00 values in the uint8_t arrays in config.h.

    Note: The devEUI must be a single hex value (e.g., 0x70B3D57ED006544E), while appKey and nwkKey are arrays of bytes (e.g., 0x31, 0x16, ...).

    // replace-with-your-device-id
    uint64_t joinEUI =   0x0000000000000000;
    uint64_t devEUI  =   0x0000000000000000;
    uint8_t appKey[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    uint8_t nwkKey[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  7. Use RadioLib as an ESP-IDF component

    master
    RadioLib can be used as a standalone ESP-IDF component without the Arduino framework. It utilizes an internal Hardware Abstraction Layer (HAL) to handle SPI transactions and GPIO operations. To integrate it into an ESP-IDF project, you must declare RadioLib as a dependency in your idf_component.yml file.
  8. Build RadioLib as a Tock application

    master

    RadioLib can be integrated into the Tock embedded operating system using libtock-c. This allows RadioLib to run as a concurrent application on Cortex-M or RISC-V based platforms.

    To build the application, you must clone the RadioLib repository and a specific version of libtock-c. You can skip the RISC-V RadioLib build by setting the SKIP_RISCV environment variable.

    Note: This specific example has been tested on the SparkFun LoRa Thing Plus - expLoRaBLE board, but it is compatible with any LoRa-capable Tock board.

    $ git clone https://github.com/jgromes/RadioLib.git
    $ cd RadioLib/examples/NonArduino/Tock/
    $ git clone https://github.com/tock/libtock-c.git
    $ cd libtock-c; git checkout c0202f9ab78da4a6e95f136cf5250701e3778f63; cd ../
    $ LIBTOCK_C_DIRECTORY="$(pwd)/libtock-c" ./build.sh
  9. Configure LoRaWAN device settings for RadioLib on TTN

    master

    When registering a DIY device using RadioLib on The Things Stack (TTS), use the following configuration parameters to ensure compatibility:

    • Device Type: Select Enter end device specifics manually.
    • Frequency Plan: Choose the plan appropriate for your region (e.g., the recommended option for Europe or the entry marked used by TTN for other regions).
    • LoRaWAN Version: Select LoRaWAN 1.1.0.
    • JoinEUI: Use all zeros (0x0000000000000000) as recommended by the LoRa Alliance TR007 for DIY devices using RadioLib.
    • DevEUI, AppKey, and NwkKey: It is recommended to let the TTN console generate these to ensure proper formatting.

    Important Security Setting for Development: To allow joining and uplinking outside of the standard production sequence during development, you must enable the Resets join nonces option.

    1. Go to your device's General Settings.
    2. Scroll to Join settings and click Expand.
    3. Enable Resets join nonces.
  10. Configure FreeRTOS tick rate for accurate radio timings

    master

    The RadioLib HAL requires a high-resolution delay() to ensure accurate radio timings (e.g., LoRaWAN RX1/RX2 windows).

    Recommended Setup: Set the FreeRTOS tick rate to 1000 Hz by adding CONFIG_FREERTOS_HZ=1000 to your sdkconfig.defaults. This ensures the HAL builds successfully and timings are precise.

    Low Tick Rate Workaround: If you cannot change the tick rate, you can bypass the build check by defining RADIOLIB_RELAX_RTOS_TICK=1 during compilation.

    Warning: In this mode, delay() uses a combination of sleeping and busy-waiting (esp_rom_delay_us()). The busy-wait portion can be as long as one tick period (e.g., 10 ms at 100 Hz), which increases CPU and power consumption.

  11. Test LoRaWAN payloads without hardware

    master

    To avoid waiting for long LoRaWAN join cycles and uplink intervals while debugging sensor logic, use this workflow:

    1. Write a separate sketch that reads your sensors.
    2. Output the resulting byte array to the Serial console.
    3. Copy the hex array from the Serial monitor.
    4. Paste the hex array into the TTN Console Payload Formatters section to verify your decoding logic works correctly.