rfm9x_node1_ack.py
1 # Example to send a packet periodically between addressed nodes with ACK 2 # Author: Jerry Needell 3 # 4 import time 5 import board 6 import busio 7 import digitalio 8 import adafruit_rfm9x 9 10 # set the time interval (seconds) for sending packets 11 transmit_interval = 10 12 13 # Define radio parameters. 14 RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your 15 # module! Can be a value like 915.0, 433.0, etc. 16 17 # Define pins connected to the chip. 18 # set GPIO pins as necessary -- this example is for Raspberry Pi 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 delay before sending ACK 30 rfm9x.ack_delay = 0.1 31 # set node addresses 32 rfm9x.node = 1 33 rfm9x.destination = 2 34 # initialize counter 35 counter = 0 36 ack_failed_counter = 0 37 # send startup message from my_node 38 rfm9x.send_with_ack(bytes("startup message from node {}".format(rfm9x.node), "UTF-8")) 39 40 # Wait to receive packets. 41 print("Waiting for packets...") 42 # initialize flag and timer 43 time_now = time.monotonic() 44 while True: 45 # Look for a new packet: only accept if addresses to my_node 46 packet = rfm9x.receive(with_ack=True, with_header=True) 47 # If no packet was received during the timeout then None is returned. 48 if packet is not None: 49 # Received a packet! 50 # Print out the raw bytes of the packet: 51 print("Received (raw header):", [hex(x) for x in packet[0:4]]) 52 print("Received (raw payload): {0}".format(packet[4:])) 53 print("RSSI: {0}".format(rfm9x.last_rssi)) 54 # send reading after any packet received 55 if time.monotonic() - time_now > transmit_interval: 56 # reset timeer 57 time_now = time.monotonic() 58 counter += 1 59 # send a mesage to destination_node from my_node 60 if not rfm9x.send_with_ack( 61 bytes("message from node node {} {}".format(rfm9x.node, counter), "UTF-8") 62 ): 63 ack_failed_counter += 1 64 print(" No Ack: ", counter, ack_failed_counter)