/ examples / rfm9x_node2.py
rfm9x_node2.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  # Define radio parameters.
11  RADIO_FREQ_MHZ = 915.0  # Frequency of the radio in Mhz. Must match your
12  # module! Can be a value like 915.0, 433.0, etc.
13  
14  # Define pins connected to the chip.
15  CS = digitalio.DigitalInOut(board.CE1)
16  RESET = digitalio.DigitalInOut(board.D25)
17  
18  # Initialize SPI bus.
19  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
20  
21  # Initialze RFM radio
22  rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
23  
24  # enable CRC checking
25  rfm9x.enable_crc = True
26  # set node addresses
27  rfm9x.node = 2
28  rfm9x.destination = 1
29  # initialize counter
30  counter = 0
31  # send a broadcast message from my_node with ID = counter
32  rfm9x.send(bytes("startup message from node {} ".format(rfm9x.node), "UTF-8"))
33  
34  # Wait to receive packets.
35  print("Waiting for packets...")
36  # initialize flag and timer
37  time_now = time.monotonic()
38  while True:
39      # Look for a new packet: only accept if addresses to my_node
40      packet = rfm9x.receive(with_header=True)
41      # If no packet was received during the timeout then None is returned.
42      if packet is not None:
43          # Received a packet!
44          # Print out the raw bytes of the packet:
45          print("Received (raw header):", [hex(x) for x in packet[0:4]])
46          print("Received (raw payload): {0}".format(packet[4:]))
47          print("Received RSSI: {0}".format(rfm9x.last_rssi))
48          # send reading after any packet received
49          counter = counter + 1
50          # after 10 messages send a response to destination_node from my_node with ID = counter&0xff
51          if counter % 10 == 0:
52              time.sleep(0.5)  # brief delay before responding
53              rfm9x.identifier = counter & 0xFF
54              rfm9x.send(
55                  bytes(
56                      "message number {} from node {} ".format(counter, rfm9x.node),
57                      "UTF-8",
58                  ),
59                  keep_listening=True,
60              )