rfm69_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_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 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 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 node addresses 31 rfm69.node = 2 32 rfm69.destination = 1 33 # initialize counter 34 counter = 0 35 # send a broadcast message from my_node with ID = counter 36 rfm69.send(bytes("startup message from node {} ".format(rfm69.node), "UTF-8")) 37 38 # Wait to receive packets. 39 print("Waiting for packets...") 40 # initialize flag and timer 41 time_now = time.monotonic() 42 while True: 43 # Look for a new packet: only accept if addresses to my_node 44 packet = rfm69.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(rfm69.last_rssi)) 52 # send reading after any packet received 53 counter = counter + 1 54 # after 10 messages send a response to destination_node from my_node with ID = counter&0xff 55 if counter % 10 == 0: 56 time.sleep(0.5) # brief delay before responding 57 rfm69.identifier = counter & 0xFF 58 rfm69.send( 59 bytes( 60 "message number {} from node {} ".format(counter, rfm69.node), 61 "UTF-8", 62 ), 63 keep_listening=True, 64 )