/ examples / rfm69_node1.py
rfm69_node1.py
 1  # Example to send a packet periodically between addressed nodes
 2  # Author: Jerry Needell
 3  #
 4  import time
 5  import board
 6  import busio
 7  import digitalio
 8  import adafruit_rfm69
 9  
10  
11  # set the time interval (seconds) for sending packets
12  transmit_interval = 10
13  
14  # Define radio parameters.
15  RADIO_FREQ_MHZ = 915.0  # Frequency of the radio in Mhz. Must match your
16  # module! Can be a value like 915.0, 433.0, etc.
17  
18  # Define pins connected to the chip.
19  CS = digitalio.DigitalInOut(board.CE1)
20  RESET = digitalio.DigitalInOut(board.D25)
21  
22  # Initialize SPI bus.
23  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
24  # Initialze RFM radio
25  rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ)
26  
27  # Optionally set an encryption key (16 byte AES key). MUST match both
28  # on the transmitter and receiver (or be set to None to disable/the default).
29  rfm69.encryption_key = (
30      b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
31  )
32  
33  # set node addresses
34  rfm69.node = 1
35  rfm69.destination = 2
36  # initialize counter
37  counter = 0
38  # send a broadcast message from my_node with ID = counter
39  rfm69.send(
40      bytes("Startup message {} from node {}".format(counter, rfm69.node), "UTF-8")
41  )
42  
43  # Wait to receive packets.
44  print("Waiting for packets...")
45  now = time.monotonic()
46  while True:
47      # Look for a new packet: only accept if addresses to my_node
48      packet = rfm69.receive(with_header=True)
49      # If no packet was received during the timeout then None is returned.
50      if packet is not None:
51          # Received a packet!
52          # Print out the raw bytes of the packet:
53          print("Received (raw header):", [hex(x) for x in packet[0:4]])
54          print("Received (raw payload): {0}".format(packet[4:]))
55          print("Received RSSI: {0}".format(rfm69.last_rssi))
56      if time.monotonic() - now > transmit_interval:
57          now = time.monotonic()
58          counter = counter + 1
59          # send a  mesage to destination_node from my_node
60          rfm69.send(
61              bytes(
62                  "message number {} from node {}".format(counter, rfm69.node), "UTF-8"
63              ),
64              keep_listening=True,
65          )
66          button_pressed = None