Package: pyLoRa
This fork - pyLoRaThis fork is an adaptation and an improved version of the original mayeranalytic work (mayeranalytics/pySX127x)
pyLoRa can be used to communicate with the Arduino through the RADIOHEAD library, for more information see these examples ->rpsreal/LoRa_Ra-02_Arduino.Update 05/2018 - Added encrypted versionsFor security reasons it is advisable to use the encrypted versions that useAdvanced Encryption Standard(AES). You can also use them to communicate with the Arduino.Update 08/2018 - Added support for 2 modules at the same timeNow it is possible to use 2 radio modules at the same time in Raspberry Pi. To do this, simply use the BOARD2 settings like this:# Use BOARD 2 (you can use BOARD1 and BOARD2 at the same time just give another name)fromSX127x.LoRaimportLoRa2asLoRafromSX127x.board_configimportBOARD2asBOARDUpdate 04/2019 - Clarification on the use of different communication frequenciesIt is possible to use several different communication frequencies using pyLora. The default frequency is 868MHz (Europe), but you can change the communication frequency to use Ai-Thinker Ra-02 Modules (433MHz) for example. To do this, look for the "868" in the SX127x/LoRa.py file and replace them with the desired frequency.It supports Python 3 or newer and PyPi.https://pypi.org/project/pyLoRa/Easy setup on Raspberry Pi:sudoraspi-confignonintdo_spi0sudoapt-getinstallpython-devpython3-devInstallation:ForInstall with pipperform the following installation steps:sudoapt-getinstallpython-pippython3-pip
pipinstallRPi.GPIO
pipinstallspidev
pipinstallpyLoRaForencrypted versions onlyit is necessary to perform the following installation step:pipinstallpycryptodome
pipinstallpycryptoHardwareMake the connections as shown below.
If it is necessary to change edit the file board_config.pyRa-02 LoRa BOARD1RaspPi GPIORa-02 LoRa BOARD2RaspPi GPIOMOSIGPIO 10MOSIGPIO 10MISOGPIO 9MISOGPIO 9SCK (SCLK)GPIO 11SCK (SCLK)GPIO 11NSSGPIO 8 (CE0)NSSGPIO 7 (CE1)DIO0 (IRQ)GPIO 4DIO0 (IRQ)GPIO 23DIO1GPIO 17DIO1GPIO 24DIO2GPIO 18DIO2GPIO 25DIO3GPIO 27DIO3GPIO 5RST (Reset)GPIO 22RST (Reset)GPIO 6LEDGPIO 13LEDGPIO 19LED external with 1k ohm or 330ohm (optional)How to UseView the sample files.
If you downloaded the library and sample files, now you can start LORA_SERVER or LORA_CLIENT (encrypted or non-encripted).
To work, there must be another LORA_SERVER or LORA_CLIENT running on another device (Raspberry Pi or Arduino)For example, if you are running on anArduino the LORA_CLIENTthen start theLORA_SERVER.py on Raspberry Pilike this:cdpySX127x
python3./LORA_SERVER.pyExtraIf you do not need to install the library you can use it simply in the same directory.
ToDownload library and example files:sudoapt-getinstallpython-rpi.gpiopython3-rpi.gpio
sudoapt-getinstallpython-spidevpython3-spidev
sudoapt-getinstallgit
sudogitclonehttps://github.com/rpsreal/pySX127xIf it is necessary to run the library from anywhere:nano~/.bashrcPut this at the end of the file:export PYTHONPATH=/home/pi/pySX127x/And then:source~/.bashrcDeveloped by Rui Silva, Porto, PortugalForked from mayeranalytics/pySX127xOverviewThis is a python interface to theSemtech SX1276/7/8/9long range, low power transceiver family.The SX127x have both LoRa and FSK capabilities. Here the focus lies on the
LoRa spread spectrum modulation hence only the LoRa modem interface is implemented so far
(but see theroadmapbelow for future plans).Spread spectrum modulation has a number of intriguing features:High interference immunityUp to 20dBm link budget advantage (for the SX1276/7/8/9)High Doppler shift immunityMore information about LoRa can be found on theLoRa Alliance website.
Links to some LoRa performance reports can be found in thereferencessection below.MotivationTransceiver modules are usually interfaced with microcontroller boards such as the
Arduino and there are already many fine C/C++ libraries for the SX127x family available ongithubandmbed.org.Although C/C++ is the de facto standard for development on microcontrollers,pythonrunning on a Raspberry Pi is becoming a viable alternative for rapid prototyping.High level programming languages like python require a full-blown OS such as Linux. (There are some exceptions likePyMiteand most notablyMicroPython.)
But using hardware capable of running Linux contradicts, to some extent, the low power specification of the SX127x family.
Therefore it is clear that this approach aims mostly at prototyping and technology testing.Prototyping on a full-blown OS using high level programming languages has several clear advantages:Working prototypes can be built quicklyTechnology testing ist fasterProof of concept is easier to achieveThe application development phase is reached quickerHardwareThe transceiver module is a SX1276 based ModtronixinAir9B.
It is mounted on a prototyping board to a Raspberry Pi rev 2 model B.Proto board pinRaspPi GPIODirectioninAir9B DIO0GPIO 22INinAir9B DIO1GPIO 23INinAir9B DIO2GPIO 24INinAir9B DIO3GPIO 25INinAir9b ResetGPIO ?OUTLEDGPIO 18OUTSwitchGPIO 4INTodo:Add picture(s)Wire the SX127x reset to a GPIO?Code ExamplesOverviewFirst import the modulesfromSX127x.LoRaimport*fromSX127x.board_configimportBOARDthen set up the board GPIOsBOARD.setup()The LoRa object is instantiated and put into the standby modelora=LoRa()lora.set_mode(MODE.STDBY)Registers are queried like so:printlora.get_version()# this prints the sx127x chip versionprintlora.get_freq()# this prints the frequency settingand setting registers is easy, toolora.set_freq(433.0)# Set the frequency to 433 MHzIn applications theLoRaclass should be subclassed while overriding one or more of the callback functions that
are invoked on successful RX or TX operations, for example.classMyLoRa(LoRa):def__init__(self,verbose=False):super(MyLoRa,self).__init__(verbose)# setup registers etc.defon_rx_done(self):payload=self.read_payload(nocheck=True)# etc.In the end the resources should be freed properlyBOARD.teardown()More detailsMost functions ofSX127x.Loraare setter and getter functions. For example, the setter and getter for
the coding rate are demonstrated hereprintlora.get_coding_rate()# print the current coding ratelora.set_coding_rate(CODING_RATE.CR4_6)# set it to CR4_6@todoInstallationMake sure SPI is activated on you RaspberryPi:SPIpySX127xrequires these two python packages:RPi.GPIOfor accessing the GPIOs, it should be already installed on
a standard Raspian Linux imagespidevfor controlling SPIIn order to install spidev download the source code and run setup.py manually:wgethttps://pypi.python.org/packages/source/s/spidev/spidev-3.1.tar.gz
tarxfvzspidev-3.1.tar.gzcdspidev-3.1
sudopythonsetup.pyinstallAt this point you may want to confirm that the unit tests pass. See the sectionTestsbelow.You can now run the scripts. For example dump the registers withlora_util.py:rasp$sudo./lora_util.py
SX127xLoRaregisters:modeSLEEPfreq434.000000MHzcoding_rateCR4_5bwBW125spreading_factor128chips/symbimplicit_hdr_modeOFF...andsoon....Class ReferenceThe interface to the SX127x LoRa modem is implemented in the classSX127x.LoRa.LoRa.
The most important modem configuration parameters are:FunctionDescriptionset_modeChange OpMode, use the constants.MODE classset_freqSet the frequencyset_bwSet the bandwidth 7.8kHz ... 500kHzset_coding_rateSet the coding rate 4/5, 4/6, 4/7, 4/8@todoMost set_* functions have a mirror get_* function, but beware that the getter return types do not necessarily match
the setter input types.Register naming conventionThe register addresses are defined in classSX127x.constants.REGand we use a specific naming convention which
is best illustrated by a few examples:RegisterModemSemtech doc.pySX127x0x0ELoRaRegFifoTxBaseAddrREG.LORA.FIFO_TX_BASE_ADDR0x0EFSKRegRssiCOnfigREG.FSK.RSSI_CONFIG0x1DLoRaRegModemConfig1REG.LORA.MODEM_CONFIG_1etc.HardwareHardware related definition and initialisation are located inSX127x.board_config.BOARD.
If you use a SBC other than the Raspberry Pi you'll have to adapt the BOARD class.Script referencesContinuous receiverrx_cont.pyThe SX127x is put in RXCONT mode and continuously waits for transmissions. Upon a successful read the
payload and the irq flags are printed to screen.usage: rx_cont.py [-h] [--ocp OCP] [--sf SF] [--freq FREQ] [--bw BW]
                  [--cr CODING_RATE] [--preamble PREAMBLE]

Continous LoRa receiver

optional arguments:
  -h, --help            show this help message and exit
  --ocp OCP, -c OCP     Over current protection in mA (45 .. 240 mA)
  --sf SF, -s SF        Spreading factor (6...12). Default is 7.
  --freq FREQ, -f FREQ  Frequency
  --bw BW, -b BW        Bandwidth (one of BW7_8 BW10_4 BW15_6 BW20_8 BW31_25
                        BW41_7 BW62_5 BW125 BW250 BW500). Default is BW125.
  --cr CODING_RATE, -r CODING_RATE
                        Coding rate (one of CR4_5 CR4_6 CR4_7 CR4_8). Default
                        is CR4_5.
  --preamble PREAMBLE, -p PREAMBLE
                        Preamble length. Default is 8.Simple LoRa beacontx_beacon.pyA small payload is transmitted in regular intervals.usage: tx_beacon.py [-h] [--ocp OCP] [--sf SF] [--freq FREQ] [--bw BW]
                    [--cr CODING_RATE] [--preamble PREAMBLE] [--single]
                    [--wait WAIT]

A simple LoRa beacon

optional arguments:
  -h, --help            show this help message and exit
  --ocp OCP, -c OCP     Over current protection in mA (45 .. 240 mA)
  --sf SF, -s SF        Spreading factor (6...12). Default is 7.
  --freq FREQ, -f FREQ  Frequency
  --bw BW, -b BW        Bandwidth (one of BW7_8 BW10_4 BW15_6 BW20_8 BW31_25
                        BW41_7 BW62_5 BW125 BW250 BW500). Default is BW125.
  --cr CODING_RATE, -r CODING_RATE
                        Coding rate (one of CR4_5 CR4_6 CR4_7 CR4_8). Default
                        is CR4_5.
  --preamble PREAMBLE, -p PREAMBLE
                        Preamble length. Default is 8.
  --single, -S          Single transmission
  --wait WAIT, -w WAIT  Waiting time between transmissions (default is 0s)TestsExecutetest_lora.pyto run a few unit tests.ContributorsPlease feel free to comment, report issues, or contribute!Contact me via my company websiteMayer Analyticsand my private blogmcmayer.net.Follow me on twitter@markuscmayerand@mayeranalytics.Roadmap95% of functions for the Sx127x LoRa capabilities are implemented. Functions will be added when necessary.
The test coverage is rather low but we intend to change that soon.Semtech SX1272/3 vs. SX1276/7/8/9pySX127xis not entirely compatible with the 1272.
The 1276 and 1272 chips are different and the interfaces not 100% identical. For example registers 0x26/27.
But the pySX127x library should get you pretty far if you use it with care. Here are the two datasheets:Semtech - SX1276/77/78/79 - 137 MHz to 1020 MHz Low Power Long Range TransceiverSemtech SX1272/73 - 860 MHz to 1020 MHz Low Power Long Range TransceiverHopeRF transceiver ICsHopeRF has a family of LoRa capable transceiver chipsRFM92/95/96/98that have identical or almost identical SPI interface as the Semtech SX1276/7/8/9 family.Microchip transceiver ICLikewise Microchip has the chipRN2483ThepySX127xproject will therefore be renamed to pyLoRa at some point.LoRaWANLoRaWAN is a LPWAN (low power WAN) and, andpySX127xhas almost no relationship with LoRaWAN. Here we only deal with the interface into the chip(s) that enable the physical layer of LoRaWAN networks.ReferencesHardware referencesSemtech SX1276/77/78/79 - 137 MHz to 1020 MHz Low Power Long Range TransceiverModtronix inAir9Spidev DocumentationMake: Tutorial: Raspberry Pi GPIO Pins and PythonLoRa performance testsExtreme Range Links: LoRa 868 / 900MHz SX1272 LoRa module for Arduino, Raspberry Pi and Intel GalileoUK LoRa versus FSK - 40km LoS (Line of Sight) test!Andreas Spiess LoRaWAN World Record AttemptSpread spectrum modulation theoryAn Introduction to Spread Spectrum TechniquesTheory of Spread-Spectrum Communications-A Tutorial(technical paper)Copyright and License© 2015 Mayer Analytics Ltd., All Rights Reserved.Short versionThe license isGNU AGPL.Long versionpySX127x is free software: you can redistribute it and/or modify it under the terms of the
GNU Affero General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.pySX127x is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.You can be released from the requirements of the license by obtaining a commercial license.
Such a license is mandatory as soon as you develop commercial activities involving
pySX127x without disclosing the source code of your own applications, or shipping pySX127x with a closed source product.You should have received a copy of the GNU General Public License
along with pySX127.  If not, seehttp://www.gnu.org/licenses/.