rfm69_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_rfm69 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 rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ) 23 24 # Optionally set an encryption key (16 byte AES key). MUST match both 25 # on the transmitter and receiver (or be set to None to disable/the default). 26 rfm69.encryption_key = ( 27 b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08" 28 ) 29 30 # set delay before transmitting ACK (seconds) 31 rfm69.ack_delay = 0.1 32 # set node addresses 33 rfm69.node = 2 34 rfm69.destination = 1 35 # initialize counter 36 counter = 0 37 ack_failed_counter = 0 38 39 # Wait to receive packets. 40 print("Waiting for packets...") 41 while True: 42 # Look for a new packet: only accept if addresses to my_node 43 packet = rfm69.receive(with_ack=True, with_header=True) 44 # If no packet was received during the timeout then None is returned. 45 if packet is not None: 46 # Received a packet! 47 # Print out the raw bytes of the packet: 48 print("Received (raw header):", [hex(x) for x in packet[0:4]]) 49 print("Received (raw payload): {0}".format(packet[4:])) 50 print("RSSI: {0}".format(rfm69.last_rssi)) 51 # send response 2 sec after any packet received 52 time.sleep(2) 53 counter += 1 54 # send a mesage to destination_node from my_node 55 if not rfm69.send_with_ack( 56 bytes("response from node {} {}".format(rfm69.node, counter), "UTF-8") 57 ): 58 ack_failed_counter += 1 59 print(" No Ack: ", counter, ack_failed_counter)