Low-level LoRa radio configuration and status
Direct control of the SX1262 LoRa radio hardware. Configure frequency, bandwidth, spreading factor, and transmit power. Most applications should use ez.mesh instead, which provides higher-level mesh networking on top of the radio. The radio status indicator shows "!RF" if initialization fails.
if ez.radio.available() then
local data = ez.radio.receive()
print("Got packet:", data)
end
local cfg = ez.radio.get_config()
if cfg then
print(string.format("Freq: %.1f MHz, SF%d, BW %.0f kHz",
cfg.frequency, cfg.spreading_factor, cfg.bandwidth))
end
local rssi = ez.radio.get_last_rssi()
if rssi > -80 then
print("Good signal")
elseif rssi > -100 then
print("Weak signal")
else
print("Very weak signal")
end
local snr = ez.radio.get_last_snr()
if snr > 5 then
print("Excellent signal quality")
elseif snr > 0 then
print("Good signal quality")
else
print("Signal below noise floor, SNR:", snr)
end
-- Wait for radio to be idle
while ez.radio.is_busy() do
ez.system.delay(10)
end
ez.radio.send("data")
if ez.radio.is_initialized() then
print("Radio ready")
else
print("Radio failed - check LoRa module")
end
if not ez.radio.is_receiving() then
ez.radio.start_receive()
end
if ez.radio.is_transmitting() then
display:draw_text(10, 10, "TX", 0xFF0000)
end
local data, rssi, snr = ez.radio.receive()
if data then
print("Data:", data)
print(string.format("Signal: %d dBm, SNR: %.1f dB", rssi, snr))
end
| Parameter | Description |
|---|---|
| data | String or table of bytes to send (max 255 bytes) |
local result = ez.radio.send("Hello LoRa")
if result == "ok" then
print("Packet sent")
else
print("Send failed:", result)
end
| Parameter | Description |
|---|---|
| khz | Bandwidth in kHz (7.8 to 500) |
ez.radio.set_bandwidth(125) -- Standard bandwidth
| Parameter | Description |
|---|---|
| cr | Coding rate (5-8) |
ez.radio.set_coding_rate(5) -- Minimal FEC, highest throughput
ez.radio.set_coding_rate(8) -- Maximum FEC, best error recovery
| Parameter | Description |
|---|---|
| mhz | Frequency in MHz (e.g., 915.0 for US) |
local result = ez.radio.set_frequency(915.0)
if result == "ok" then
print("Frequency set to 915 MHz")
end
| Parameter | Description |
|---|---|
| sf | Spreading factor (6-12) |
ez.radio.set_spreading_factor(10) -- Good balance of range and speed
| Parameter | Description |
|---|---|
| sw | Sync word value (0-255) |
ez.radio.set_sync_word(0x12) -- Standard LoRa sync word
| Parameter | Description |
|---|---|
| dbm | Power in dBm (0-22) |
ez.radio.set_tx_power(17) -- Moderate power, good battery life
ez.radio.set_tx_power(22) -- Maximum power, maximum range
-- Save power when idle
ez.radio.sleep()
ez.system.delay(60000) -- Sleep for 1 minute
ez.radio.wake()
ez.radio.start_receive()
ez.radio.start_receive()
while true do
if ez.radio.available() then
local data, rssi, snr = ez.radio.receive()
print("Received:", data, "RSSI:", rssi)
end
ez.system.delay(10)
end
ez.radio.wake()
ez.radio.start_receive() -- Resume listening