Skip to content

LoRa Driver API

Header: main/lora_rylr.h

Drives RYLR-series LoRa modules over UART using AT commands. Handles command/response flow and async +RCV receive notifications.

In QEMU mode (CONFIG_QEMU_TEST_MODE), all UART operations are replaced with FreeRTOS queue loopback.

Received packet data.

FieldTypeDescription
addressuint16_tSender LoRa address
data_lenuint16_tPayload length
datachar[241]Null-terminated ASCII payload
rssiint16_tSignal strength in dBm
snrint8_tSignal-to-noise ratio in dB

Module configuration.

FieldTypeDefaultDescription
addressuint16_t1Our LoRa address (0–65535)
dest_addressuint16_t0Default send destination
network_iduint8_t0Network ID (0–16)
banduint32_t915000000Frequency in Hz
sfuint8_t9Spreading factor (7–12)
bwuint8_t7Bandwidth: 7=125kHz, 8=250kHz, 9=500kHz
cruint8_t1Coding rate (1–4 → 4/5 to 4/8)
preambleuint8_t7Preamble length (4–25)

Runtime statistics.

FieldTypeDescription
last_rssiint16_tRSSI of last received packet
last_snrint8_tSNR of last received packet
tx_countuint32_tTotal packets sent
rx_countuint32_tTotal packets received
tx_errorsuint32_tSend failures
rx_errorsuint32_tParse/queue failures
esp_err_t lora_init(const lora_config_t *config);

Initialize the LoRa module. In QEMU mode, creates a loopback queue. On hardware, sends AT commands to configure the RYLR module: address, network ID, frequency band, and RF parameters.

Returns: ESP_OK on success, ESP_FAIL if the module doesn’t respond.

esp_err_t lora_send(uint16_t dest_addr, const char *data, size_t len);

Send ASCII data to a specific address. Data must be printable ASCII, max 240 characters. In QEMU mode, writes to the loopback queue.

esp_err_t lora_send_default(const char *data, size_t len);

Send to the configured default destination address.

esp_err_t lora_send_binary(uint16_t dest_addr, const uint8_t *data, size_t len);

Send raw binary data. Writes the AT prefix, binary payload, and terminator as three separate UART operations. The RYLR998 reads exactly len bytes — null bytes and non-printable data are safe. In QEMU mode, copies the binary payload directly into the loopback queue (binary-safe).

esp_err_t lora_receive(lora_rx_packet_t *pkt, uint32_t timeout_ms);

Receive a packet. Checks the stashed queue first (packets received during AT responses), then polls UART for +RCV messages. Uses line-based parsing — safe for ASCII payloads but not for binary data containing 0x0A (newline) or 0x2C (comma).

Returns: ESP_OK if a packet was received, ESP_ERR_TIMEOUT otherwise.

esp_err_t lora_receive_binary(lora_rx_packet_t *pkt, uint32_t timeout_ms);

Receive a binary-safe packet. In QEMU mode, reads from the loopback queue (identical to lora_receive). On hardware, uses length-delimited parsing: reads the +RCV=addr,len, ASCII prefix, then reads exactly len raw bytes (safe for 0x0A, 0x2C, and null bytes), then parses the ASCII ,rssi,snr suffix.

Needed by the ACK layer (pjpeg_lora_ack) to receive 4-byte ACK/NACK packets whose binary payloads break the line-based lora_receive() path.

Returns: ESP_OK if a packet was received, ESP_ERR_TIMEOUT otherwise.

esp_err_t lora_set_address(uint16_t addr);
esp_err_t lora_set_network(uint8_t id);
esp_err_t lora_set_band(uint32_t freq);
esp_err_t lora_set_rf_params(uint8_t sf, uint8_t bw, uint8_t cr, uint8_t preamble);
esp_err_t lora_sleep(void); // AT+MODE=1
esp_err_t lora_wake(void); // AT (any command wakes module)
const lora_stats_t *lora_get_stats(void);
const lora_config_t *lora_get_config(void);
bool lora_is_initialized(void);

These are the RYLR AT commands used by the driver:

CommandResponseDescription
AT+OKPing / wake
AT+ADDRESS=N+OKSet address (0–65535)
AT+NETWORKID=N+OKSet network ID (0–16)
AT+BAND=N+OKSet frequency (Hz)
AT+PARAMETER=SF,BW,CR,PP+OKSet RF parameters
AT+SEND=addr,len,data+OKSend data (max 240 chars)
AT+MODE=1+OKEnter sleep mode
(async) +RCV=addr,len,data,rssi,snrReceived data notification