Skip to main content

Python library used for basic transmitting and receiving data using LoRa modem

Project description

GitHub issues GitHub forks GitHub stars PyPI - Downloads PyPI GitHub license

LoRa-RF Python Library

LoRa-RF Python is a library for basic transmitting and receiving data using LoRa module with Semtech SX126x series or LLCC68. The library works by interfacing SPI port and some GPIO pins under linux kernel. Support configuring frequency, modulation parameter, transmit power, receive gain and other RF parameters on both LoRa and FSK modulation also support handling transmit and receive using interrupt signal.

This readme is written for quick start guide. Visit this link for complete documentation.

Hardware Compatibility

Theoritically all LoRa modules using SX126x series (SX1261, SX1262, SX1268) or LLCC68 will compatible using this library. Some LoRa module which already tested and confirmed compatible are:

  • Ebyte: E22-400M22S, E22-900M22S, E22-400M30S, E22-900M30S

Currently only Raspberry pi zero, zero W, 3A, 3B, 3B+, 4A, and 4B supported as host controller. Support for other single board computer will be added in the future. The Linux distro already tested using this library are:

  • Raspberry pi OS
  • Ubuntu Core 32-bit

In order to connect to a LoRa module, SPI port must be enabled. For Raspberry pi OS, this is done by set SPI interface enable using raspi-config or edit /boot/config.txt by adding following line.

dtparam=spi=on

Installation

Using pip

Using terminal run following command.

pip3 install LoRaRF

Using Git and Build Package

To using latest update of the library, you can clone then build python package manually. Using this method require setuptools and wheel module.

git clone https://github.com/chandrawi/LoRaRF-Python.git
cd LoRaRF-Python
python3 setup.py bdist_wheel
pip3 install dist/LoRaRF-1.3.0-py3-none-any.whl

Initialization

To work with the library, first you must import SX126x python module. Then initialize the module by creating an object.

from LoRaRF import SX126x
LoRa = SX126x()

Before calling any configuration methods, doing transmit or receive operation you must call begin() method.

LoRa.begin()

Hardware Configuration

Wiring Connections

Power pins, SPI pins, RESET, and BUSY pins must be connected between host controller and LoRa module. If you want to use interrupt operation, you can connect one of DIO1, DIO2, or DIO3 pin. You also should connect TXEN and RXEN pins if your LoRa module have those pins.

The default SPI port is using bus id 0 and cs id 0. The default GPIO pins used for connecting to SX126x with Broadcom pin numbering are as follows.

Semtech SX126x Raspberry Pi
VCC 3.3V
GND GND
SCK GPIO 11
MISO GPIO 9
MOSI GPIO 10
NSS GPIO 8
RESET GPIO 22
BUSY GPIO 23
DIO1 -1 (unused)
TXEN -1 (unused)
RXEN -1 (unused)

SPI Port Configuration

To configure SPI port or SPI frequency call setSPI() method before begin() method.

# set to use SPI with bus id 0 and cs id 1 and speed 7.8 Mhz
LoRa.setSPI(0, 0, 7800000)
LoRa.begin()

I/O Pins Configuration

To configure I/O pins (NSS, RESET, BUSY, IRQ, TXEN, RXEN pin) call setPins() before begin() method.

# set RESET->22, BUSY->23, DIO1->26, TXEN->5, RXEN->25
LoRa.setPins(22, 23, 26, 5, 25)
LoRa.begin()

Modem Configuration

Before transmit or receive operation you can configure transmit power and receive gain or matching frequency, modulation parameter, packet parameter, and synchronize word with other LoRa device you want communicate.

Transmit Power

# set transmit power to +22 dBm for SX1262
LoRa.setTxPower(22, LoRa.TX_POWER_SX1262)

Receive Gain

# set receive gain to power saving
LoRa.setRxGain(LoRa.RX_GAIN_POWER_SAVING)

Frequency

# Set frequency to 915 Mhz
LoRa.setFrequency(915000000)

Modulation Parameter

# set spreading factor 8, bandwidth 125 kHz, coding rate 4/5, and low data rate optimization off
LoRa.setLoRaModulation(8, 125000, 5, false)

Packet Parameter

# set explicit header mode, preamble length 12, payload length 15, CRC on and no invert IQ operation
LoRa.setLoRaPacket(LoRa.HEADER_EXPLICIT, 12, 15, true, false)

Synchronize Word

# Set syncronize word for public network (0x3444)
LoRa.setSyncWord(0x3444)

Transmit Operation

Transmit operation begin with calling beginPacket() method following by write() method to write package to be tansmitted and ended with calling endPacket() method. For example, to transmit "HeLoRa World!" message and an increment counter you can use following code.

# message and counter to transmit
message = "HeLoRa World!\0"
messageList = list(message)
for i in range(len(messageList)) : messageList[i] = ord(messageList[i])
counter = 0

LoRa.beginPacket()
LoRa.write(message, sizeof(message)) # write multiple bytes
LoRa.write(counter)                  # write single byte
LoRa.endPacket()
LoRa.wait()
counter += 1

For more detail about transmit operation, please visit this link.

Receive Operation

Receive operation begin with calling request() method following by read() method to read received package. available() method can be used to get length of remaining package. For example, to receive message and a counter in last byte you can use following code.

LoRa.request()
LoRa.wait()

# get message and counter in last byte
message = ""
while LoRa.available() > 1 :
  message += chr(LoRa.read())        # read multiple bytes
counter = LoRa.read()                # read single byte

For more detail about receive operation, please visit this link.

Examples

See examples for SX126x and simple network implementation.

License

This library published under MIT license.

Contributor

Chandra Wijaya Sentosa <chandra.w.sentosa@gmail.com>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

LoRaRF-1.3.0.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

LoRaRF-1.3.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file LoRaRF-1.3.0.tar.gz.

File metadata

  • Download URL: LoRaRF-1.3.0.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

File hashes

Hashes for LoRaRF-1.3.0.tar.gz
Algorithm Hash digest
SHA256 d7ce467e0b2030ba24f589244a3a254a2d7c32d9f45c3e48c71a2ad61074f3f4
MD5 abd036db80f38b24116b1eff9b9efa8d
BLAKE2b-256 fe3d720fbff17253ce95f06324269cd412e6838118eba54d7f1e629cff24942f

See more details on using hashes here.

File details

Details for the file LoRaRF-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: LoRaRF-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

File hashes

Hashes for LoRaRF-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b945b1638ec5417843cd98b87750147046dca8431ad2ac1210d74bcf6e448781
MD5 49161d6447a397994f4f0a1f3b8668cc
BLAKE2b-256 a3234c0aeb8b0b500506fe708824b8604c7770f0e4af0c06ed454ba7f29b6949

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page