Example - The Basics

Initialise

To initialise the radio you create a context:

from RFM69 import Radio, FREQ_433MHZ

this_node_id = 1
with Radio(FREQ_433MHZ, this_node_id) as radio:
    ... your code here ...

This ensures that the necessary clean-up code is executed when you exit the context.

Note

Frequency selection: FREQ_315MHZ, FREQ_433MHZ, FREQ_868MHZ or FREQ_915MHZ. Select the band appropriate to the radio you have.

Simple Transceiver

Here’s a simple transceiver example.

# pylint: disable=unused-import

import time
from RFM69 import Radio, FREQ_315MHZ, FREQ_433MHZ, FREQ_868MHZ, FREQ_915MHZ

node_id = 1
network_id = 100
recipient_id = 2

# The following are for an Adafruit RFM69HCW Transceiver Radio
# Bonnet https://www.adafruit.com/product/4072
# You should adjust them to whatever matches your radio
board = {'isHighPower': True, 'interruptPin': 15, 'resetPin': 22, 'spiDevice': 1}

# The following are for an RaspyRFM RFM69CW Module #1
# http://www.seegel-systeme.de/2015/09/02/ein-funkmodul-fuer-den-raspberry-raspyrfm/
# board = {'isHighPower': False, 'interruptPin': 22, 'resetPin': None, 'spiDevice': 0}

# The following are for an RaspyRFM RFM69CW Module #2
# http://www.seegel-systeme.de/2015/09/02/ein-funkmodul-fuer-den-raspberry-raspyrfm/
# board = {'isHighPower': False, 'interruptPin': 18, 'resetPin': None, 'spiDevice': 1}

with Radio(FREQ_915MHZ, node_id, network_id, verbose=False, **board) as radio:
    print ("Starting loop...")

    while True:
        startTime = time.time()
        # Get packets for at most 5 seconds
        while time.time() - startTime < 5:
            # We end at (startTime+5), so we have (startTime+5 - time.time())
            # seconds left
            timeRemaining = max(0, startTime + 5 - time.time())

            # This call will block until a packet is received,
            # or timeout in however much time we have left
            packet = radio.get_packet(timeout = timeRemaining)

            # If radio.get_packet times out, it will return None
            if packet is not None:
                # Process packet
                print (packet)

        # After 5 seconds send a message
        print ("Sending")
        if radio.send(recipient_id, "TEST", attempts=3, waitTime=100):
            print ("Acknowledgement received")
        else:
            print ("No Acknowledgement")

Better Transceiver

Here’s an even better way to do deal with sending and receiving packets: start up a separate thread to handle incoming packets. Now we’ve eliminated the complex timing code altogether, and the result is much more readable.

# pylint: disable=missing-function-docstring,unused-import,redefined-outer-name

import time
import threading
from RFM69 import Radio, FREQ_315MHZ, FREQ_433MHZ, FREQ_868MHZ, FREQ_915MHZ

node_id = 1
network_id = 100
recipient_id = 2

# We'll run this function in a separate thread
def receiveFunction(radio):
    while True:
        # This call will block until a packet is received
        packet = radio.get_packet()
        print("Got a packet: ", end="")
        # Process packet
        print(packet)

# The following are for an Adafruit RFM69HCW Transceiver Radio
# Bonnet https://www.adafruit.com/product/4072
# You should adjust them to whatever matches your radio
with Radio(FREQ_915MHZ, node_id, network_id, isHighPower=True, verbose=False,
           interruptPin=15, resetPin=22, spiDevice=1) as radio:
    print ("Starting loop...")

    # Create a thread to run receiveFunction in the background and start it
    receiveThread = threading.Thread(target = receiveFunction, args=(radio,))
    receiveThread.start()

    while True:
        # After 5 seconds send a message
        time.sleep(5)
        print ("Sending")
        if radio.send(recipient_id, "TEST", attempts=3, waitTime=100):
            print ("Acknowledgement received")
        else:
            print ("No Acknowledgement")