Package: pyLoraRFM9x
pyLoraRFM9xThis project is a fork ofraspi-loraproject.pyLoraRFM9x is a interrupt based Python library for using HopeRF RFM95/96/97/98 LoRa radios with a Raspberry Pi. The design was inspired by theRadioHeadproject that is popular on Arduino-based platforms. Several handy features offered by RadioHead are present here, including encryption, addressing, optional acknowledgments and retransmission. The motivation of this project is to allow Raspberry Pis to communicate with devices using theRadioHead RF95driver.This fork fixes bugs in the interrupt handling, and supports both acknowledged and unacknowledged transfers with the use of flags in the packet header.UsageInstallationRequires Python >= 3.5.RPi.GPIOandspidevwill be installed as requirementspipinstall--upgradepyLoraRFM9xWiringExample wiring for theHopeRF RFM95Wand the Raspberry Pi:RFM module pinRaspberry Pi GPIO pinMISOMISOMOSIMOSINSS/CSCE1CLKSCKRESETGPIO 25DIO0GPIO 53.3V3.3VGNDGNDRemember to connect an antenna or a quarter wavelength wire to the RFM modules ANT pin.Getting StartedFirst ensure that SPI is enabled on your Raspberry Pi:sudoraspi-configGo to5 Interfacing Options, selectP4 SPIand selectYesto enable.Reboot and verify that you see two devices when writingls-l/dev/spidev*in the terminal.Next, here is a quick example that sets things up and sends a message:frompyLoraRFM9ximportLoRa,ModemConfig# This is our callback function that runs when a message is receiveddefon_recv(payload):print("From:",payload.header_from)print("Received:",payload.message)print("RSSI:{}; SNR:{}".format(payload.rssi,payload.snr))# Use chip select 1. GPIO pin 5 will be used for interrupts and set reset pin to 25# The address of this device will be set to 2lora=LoRa(1,5,2,reset_pin=25,modem_config=ModemConfig.Bw125Cr45Sf128,tx_power=14,acks=True)lora.on_recv=on_recv# Send a message to a recipient device with address 10# Retry sending the message twice if we don't get an  acknowledgment from the recipientmessage="Hello there!"status=lora.send_to_wait(message,10,retries=2)ifstatusisTrue:print("Message sent!")else:print("No acknowledgment from recipient")EncryptionIf you'd like to send and receive encrypted packets, you'll need to install thePyCryptopackage. If you're working with devices running RadioHead with RHEncryptedDriver, I recommend using the AES cipher.pipinstallpycryptoand in your code:fromCrypto.CipherimportAEScrypto=AES.new("my-secret-encryption-key")then pass incryptowhen instantiating theLoRaobject:lora=LoRa(0,17,2,crypto=crypto)ConfigurationInitializationLoRa(channel,interrupt,this_address,reset_pin=reset_pin,freq=915,tx_power=14,modem_config=ModemConfig.Bw125Cr45Sf128,acks=False,crypto=None)channelSPI channel to use (either 0 or 1, if your LoRa radio if connected to CE0 or CE1, respectively)interruptGPIO pin (BCM-style numbering) to use for the interruptthis_addressThe address number (0-254) your device will use when sending and receiving packets.reset_pinThe pin that resets the module. Defaults to NonefreqFrequency used by your LoRa radio. Defaults to 915Mhztx_powerTransmission power level from 5 to 23. Keep this as low as possible. Defaults to 14model_configModem configuration. SeeRadioHead docs. Default to Bw125Cr45Sf128.acksIfTrue, send an acknowledgment packet when a message is received and wait for an acknowledgment when transmitting a message. This is equivalent to using RadioHead's RHReliableDatagramcryptoAn instance ofPyCrypto Cipher.AES(see above example)Other options:ALoRainstance also has the following attributes that can be changed:cad_timeoutTimeout for channel activity detection. Default is 1 secondretry_timeoutTime to wait for an acknowledgment before attempting a retry. Defaults to 0.5 secondswait_packet_sent_timeoutTimeout for waiting for a packet to transmit. Default is 0.5 secondsMethodssend_to_wait(data, header_to, header_flags=0)Send a message and block until an acknowledgment is received or a timeout occurs. ReturnsTrueif successfuldataYour message. Can be a string or byte stringheader_toAddress of recipient (0-255). If address is 255, the message will be broadcast to all devices andsend_to_waitwill returnTruewithout waiting for acknowledgmentsheader_flagsBitmask that can contain flags specific to your applicationsend(data, header_to, header_id=0, header_flags=0)Similar tosend_to_waitbut does not block or wait for acknowledgments and will always returnTruedataYour message. Can be a string or byte stringheader_idUnique ID of message (0-255)header_toAddress of recipient (0-255). If address is 255, the message will be broadcast to all devicesheader_flagsBitmask that can contain flags specific to your applicationset_mode_rx()Set radio to RX continuous modeset_mode_tx()Set radio to TX modeset_mode_idle()Set radio to idle (disabling receiving or transmitting)sleep()Set radio to low-power sleep modewait_packet_sent()Blocks until a packet has finished transmitting. ReturnsFalseif a timeout occursclose()Cleans up GPIO pins and closes the SPI connection. This should be called when your program exits.Callbackson_recv(payload)Callback function that runs when a message is receivedpayloadhas the following attributes:header_from,header_to,header_id,header_flags,message,rssi,snrResourcesRadioHead- The RadioHead project. Very useful source of information on working with LoRa radios.Forked version of RadioHead for Raspberry Pi- A fork of the original RadioHead project that better accommodates the Raspberry Pi. Currently is a few years out of date.pySX127x- Another Python LoRa library that allows for a bit more configuration.Adafruit CircuitPython module for the RFM95/6/7/8- LoRa library for CircuitPython