CircuitPython module for the RFM95/6/7/8 LoRa 433/915mhz radio modules. This is adapted from the Radiohead library RF95 code from: http: www.airspayce.com/mikem/arduino/RadioHead/
- Author(s): Tony DiCola, Jerry Needell
RFM9x(spi, cs, reset, frequency, *, preamble_length=8, high_power=True, baudrate=5000000)¶
Interface to a RFM95/6/7/8 LoRa radio module. Allows sending and receivng bytes of data in long range LoRa mode at a support board frequency (433/915mhz).
You must specify the following parameters: - spi: The SPI bus connected to the radio. - cs: The CS pin DigitalInOut connected to the radio. - reset: The reset/RST pin DigialInOut connected to the radio. - frequency: The frequency (in mhz) of the radio module (433/915mhz typically).
You can optionally specify: - preamble_length: The length in bytes of the packet preamble (default 8). - high_power: Boolean to indicate a high power board (RFM95, etc.). Default is True for high power. - baudrate: Baud rate of the SPI connection, default is 10mhz but you might choose to lower to 1mhz if using long wires or a breadboard.
Remember this library makes a best effort at receiving packets with pure Python code. Trying to receive packets too quickly will result in lost data so limit yourself to simple scenarios of sending and receiving single packets at a time.
Also note this library tries to be compatible with raw RadioHead Arduino library communication. This means the library sets up the radio modulation to match RadioHead’s defaults. Features like addressing and guaranteed delivery need to be implemented at an application level.
The frequency of the radio in Megahertz. Only the allowed values for your radio must be specified (i.e. 433 vs. 915 mhz)!
Enter idle standby mode.
Listen for packets to be received by the chip. Use
receive()to listen, wait and retrieve packets as they’re available.
The length of the preamble for sent and received packets, an unsigned 16-bit value. Received packets must match this length or they are ignored! Set to 8 to match the RadioHead RFM95 library.
receive(timeout=0.5, keep_listening=True, with_header=False, rx_filter=255)¶
Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of seconds for a packet to be received and decoded. If a packet is found the payload bytes are returned, otherwise None is returned (which indicates the timeout elapsed with no reception). If keep_listening is True (the default) the chip will immediately enter listening mode after reception of a packet, otherwise it will fall back to idle mode and ignore any future reception. A 4-byte header must be prepended to the data for compatibilty with the RadioHead library. The header consists of a 4 bytes (To,From,ID,Flags). The default setting will accept any incomming packet and strip the header before returning the packet to the caller. If with_header is True then the 4 byte header will be returned with the packet. The payload then begins at packet. rx_fliter may be set to reject any “non-broadcast” packets that do not contain the specfied “To” value in the header. if rx_filter is set to 0xff (_RH_BROADCAST_ADDRESS) or if the “To” field (packet[) is equal to 0xff then the packet will be accepted and returned to the caller. If rx_filter is not 0xff and packet does not match rx_filter then the packet is ignored and None is returned.
Perform a reset of the chip.
The received strength indicator (in dBm) of the last received message.
send(data, timeout=2.0, tx_header=(255, 255, 0, 0))¶
Send a string of data using the transmitter. You can only send 252 bytes at a time (limited by chip’s FIFO size and appended headers). This appends a 4 byte header to be compatible with the RadioHead library. The tx_header defaults to using the Broadcast addresses. It may be overidden by specifying a 4-tuple of bytes containing (To,From,ID,Flags) The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
Enter sleep mode.
Transmit a packet which is queued in the FIFO. This is a low level function for entering transmit mode and more. For generating and transmitting a packet of data use
The transmit power in dBm. Can be set to a value from 5 to 23 for high power devices (RFM95/96/97/98, high_power=True) or -1 to 14 for low power devices. Only integer power levels are actually set (i.e. 12.5 will result in a value of 12 dBm). The actual maximum setting for high_power=True is 20dBm but for values > 20 the PA_BOOST will be enabled resulting in an additional gain of 3dBm. The actual setting is reduced by 3dBm. The reported value will reflect the reduced setting.