/ examples / rfm9x_node2_ack.py
rfm9x_node2_ack.py
 1  # Example to receive addressed packed with ACK and send a response
 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  # set GPIO pins as necessary - this example is for Raspberry Pi
16  CS = digitalio.DigitalInOut(board.CE1)
17  RESET = digitalio.DigitalInOut(board.D25)
18  
19  # Initialize SPI bus.
20  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
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 delay before transmitting ACK (seconds)
27  rfm9x.ack_delay = 0.1
28  # set node addresses
29  rfm9x.node = 2
30  rfm9x.destination = 1
31  # initialize counter
32  counter = 0
33  ack_failed_counter = 0
34  
35  # Wait to receive packets.
36  print("Waiting for packets...")
37  while True:
38      # Look for a new packet: only accept if addresses to my_node
39      packet = rfm9x.receive(with_ack=True, with_header=True)
40      # If no packet was received during the timeout then None is returned.
41      if packet is not None:
42          # Received a packet!
43          # Print out the raw bytes of the packet:
44          print("Received (raw header):", [hex(x) for x in packet[0:4]])
45          print("Received (raw payload): {0}".format(packet[4:]))
46          print("RSSI: {0}".format(rfm9x.last_rssi))
47          # send response 2 sec after any packet received
48          time.sleep(2)
49          counter += 1
50          # send a  mesage to destination_node from my_node
51          if not rfm9x.send_with_ack(
52              bytes("response from node {} {}".format(rfm9x.node, counter), "UTF-8")
53          ):
54              ack_failed_counter += 1
55              print(" No Ack: ", counter, ack_failed_counter)