Sending Data with the RFM69

Do not use this if you have a RFM9x LoRa Radio.

To demonstrate how the RFM69 module sends packets, we'll build an example where one radio transmits (the Pi) and the other radio connected to a Feather receives the incoming transmission. 

RFM69 and CircuitPython

It's easy to use the RFM69HCW radio with CircuitPython and the Adafruit CircuitPython RFM69 library.  This library allows you to easily write Python code that sends and receives packets of data with the radio.

Be careful to note this code is for the RFM69 radio only and will not work with the RFM9X LoRa radios!

You'll also need another radio with a RFM69HCW Module to run the example below. Make sure you have two of the same type of radio (i.e: RFM69HCW) and the same frequency (i.e: 915MHz).

CircuitPython Transmitter/Receiver Example

Below is an example of using the RFM69HCW to transmit, or receive from, another RFM69HCW radio. Save this as radio_rfm69.py on your Raspberry Pi. 

"""
Example for using the RFM69HCW Radio with Raspberry Pi.

Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
Author: Brent Rubell for Adafruit Industries
"""
# Import Python System Libraries
import time
# Import Blinka Libraries
import busio
from digitalio import DigitalInOut, Direction, Pull
import board
# Import the SSD1306 module.
import adafruit_ssd1306
# Import the RFM69 radio module.
import adafruit_rfm69

# Button A
btnA = DigitalInOut(board.D5)
btnA.direction = Direction.INPUT
btnA.pull = Pull.UP

# Button B
btnB = DigitalInOut(board.D6)
btnB.direction = Direction.INPUT
btnB.pull = Pull.UP

# Button C
btnC = DigitalInOut(board.D12)
btnC.direction = Direction.INPUT
btnC.pull = Pull.UP

# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)

# 128x32 OLED Display
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c)
# Clear the display.
display.fill(0)
display.show()
width = display.width
height = display.height

# Configure Packet Radio
CS = DigitalInOut(board.CE1)
RESET = DigitalInOut(board.D25)
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, 915.0)
prev_packet = None
# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08'

while True:
    packet = None
    # draw a box to clear the image
    display.fill(0)
    display.text('RasPi Radio', 35, 0, 1)

    # check for packet rx
    packet = rfm69.receive()
    if packet is None:
        display.show()
        display.text('- Waiting for PKT -', 15, 20, 1)
    else:
        # Display the packet text and rssi
        display.fill(0)
        prev_packet = packet
        packet_text = str(prev_packet, "utf-8")
        display.text('RX: ', 0, 0, 1)
        display.text(packet_text, 25, 0, 1)
        time.sleep(1)

    if not btnA.value:
        # Send Button A
        display.fill(0)
        button_a_data = bytes("Button A!\r\n","utf-8")
        rfm69.send(button_a_data)
        display.text('Sent Button A!', 25, 15, 1)
    elif not btnB.value:
        # Send Button B
        display.fill(0)
        button_b_data = bytes("Button B!\r\n","utf-8")
        rfm69.send(button_b_data)
        display.text('Sent Button B!', 25, 15, 1)
    elif not btnC.value:
        # Send Button C
        display.fill(0)
        button_c_data = bytes("Button C!\r\n","utf-8")
        rfm69.send(button_c_data)
        display.text('Sent Button C!', 25, 15, 1)

    display.show()
    time.sleep(0.1)

To run the example, enter the following into the terminal:

python3 radio_rfm69.py

You'll also want to download the font file, font5x8.bin, and copy it into the same directory as the script:

Both of the radios will listen for a new incoming packet. When they receive a new packet, they'll print the text from the packet to the display and to the terminal.

Press any of the three buttons to send data between radios. Pressing button a will send a packet with data 'Button A!' to the other radio, and so on!

To send a packet using the Pi, press Button A. You should see the text change to Sent Packet!

This guide was first published on Jan 23, 2019. It was last updated on Jan 23, 2019. This page (Sending Data with the RFM69) was last updated on May 26, 2019.