Sunday, March 29, 2020

LoRa on RPi

LoRa is a low-power and long distance wireless technology.  I have a set of Semetch SX1272 boards.  We'll try to use them to communicate between two Raspberry Pi boards (Pi3 and Pi4).

Wire the 4 SPI signals to the header, and add a GPIO for reset.  I also wire up an LED.  First checkout the connection by reading the "RegVersion" register at address 0x42 and expect a value of 0x22.  The chip operates in Mode 0 (CPOL=0, CPHA=0).
import spidev
spi=spidev.SpiDev(0,0)
spi.max_speed_hz=1000000
spi.xfer([0x42,0]) 
It returns [0, 34].  Note that the SPI max speed depends on the wiring.

There are a few Python modules that support SX1272.  We'll try to make use of Adafruit's Python package for RFM9x, which is said to use SX1272 (although the IC has its own marking).  The first attempt to use adafruit_rfm9x was unsuccessful; it could not find the device.  It appears that RFM9x uses an older version of the chip and there is some additional circuitry on the module Furthermore, the CircuitPython implementation of the SPI device interface for RPi seems a little convoluted.  It fails at the very beginning trying to read the version register.

We move on to use raspi-lora for RFM9x.  It requires only RPi.GPIO and spidev.  One GPIO input is needed as interrupt for receiving.  The code is simple.  It worked on the first try; messages sent from RPi3 were received by RPi4.

Another Python package python-sx127x also works.