/ examples / rfm9x_node1.py
rfm9x_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_rfm9x
 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  rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
26  
27  # enable CRC checking
28  rfm9x.enable_crc = True
29  # set node addresses
30  rfm9x.node = 1
31  rfm9x.destination = 2
32  # initialize counter
33  counter = 0
34  # send a broadcast message from my_node with ID = counter
35  rfm9x.send(
36      bytes("Startup message {} from node {}".format(counter, rfm9x.node), "UTF-8")
37  )
38  
39  # Wait to receive packets.
40  print("Waiting for packets...")
41  now = time.monotonic()
42  while True:
43      # Look for a new packet: only accept if addresses to my_node
44      packet = rfm9x.receive(with_header=True)
45      # If no packet was received during the timeout then None is returned.
46      if packet is not None:
47          # Received a packet!
48          # Print out the raw bytes of the packet:
49          print("Received (raw header):", [hex(x) for x in packet[0:4]])
50          print("Received (raw payload): {0}".format(packet[4:]))
51          print("Received RSSI: {0}".format(rfm9x.last_rssi))
52      if time.monotonic() - now > transmit_interval:
53          now = time.monotonic()
54          counter = counter + 1
55          # send a  mesage to destination_node from my_node
56          rfm9x.send(
57              bytes(
58                  "message number {} from node {}".format(counter, rfm9x.node), "UTF-8"
59              ),
60              keep_listening=True,
61          )
62          button_pressed = None