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¶
from RFM69 import Radio
import datetime
import time
from config import *
node_id = 1
network_id = 100
recipient_id = 2
with Radio(FREQUENCY, node_id, network_id, isHighPower=True, verbose=True, interruptPin=INTERRUPT_PIN, resetPin=RESET_PIN, spiDevice=SPI_DEVICE) as radio:
print ("Starting loop...")
rx_counter = 0
tx_counter = 0
while True:
# Every 10 seconds get packets
if rx_counter > 10:
rx_counter = 0
# Process packets
for packet in radio.get_packets():
print (packet)
# Every 5 seconds send a message
if tx_counter > 5:
tx_counter=0
# Send
print ("Sending")
if radio.send(2, "TEST", attempts=3, waitTime=100):
print ("Acknowledgement received")
else:
print ("No Acknowledgement")
print("Listening...", len(radio.packets), radio.mode_name)
delay = 0.5
rx_counter += delay
tx_counter += delay
time.sleep(delay)