rfm69_node1_bonnet.py
1 # Example to send a packet periodically between addressed nodes 2 # Author: Jerry Needell 3 # 4 import board 5 import busio 6 import digitalio 7 8 # Import the SSD1306 module. 9 import adafruit_ssd1306 10 import adafruit_rfm69 11 12 # Button A 13 btnA = digitalio.DigitalInOut(board.D5) 14 btnA.direction = digitalio.Direction.INPUT 15 btnA.pull = digitalio.Pull.UP 16 17 # Button B 18 btnB = digitalio.DigitalInOut(board.D6) 19 btnB.direction = digitalio.Direction.INPUT 20 btnB.pull = digitalio.Pull.UP 21 22 # Button C 23 btnC = digitalio.DigitalInOut(board.D12) 24 btnC.direction = digitalio.Direction.INPUT 25 btnC.pull = digitalio.Pull.UP 26 27 # Create the I2C interface. 28 i2c = busio.I2C(board.SCL, board.SDA) 29 30 # 128x32 OLED Display 31 reset_pin = digitalio.DigitalInOut(board.D4) 32 display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, reset=reset_pin) 33 # Clear the display. 34 display.fill(0) 35 display.show() 36 width = display.width 37 height = display.height 38 39 40 # set the time interval (seconds) for sending packets 41 transmit_interval = 10 42 43 # Define radio parameters. 44 RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your 45 # module! Can be a value like 915.0, 433.0, etc. 46 47 # Define pins connected to the chip. 48 CS = digitalio.DigitalInOut(board.CE1) 49 RESET = digitalio.DigitalInOut(board.D25) 50 51 # Initialize SPI bus. 52 spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) 53 54 # Initialze RFM radio 55 56 # Attempt to set up the RFM69 Module 57 try: 58 rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, RADIO_FREQ_MHZ) 59 display.text("RFM69: Detected", 0, 0, 1) 60 except RuntimeError: 61 # Thrown on version mismatch 62 display.text("RFM69: ERROR", 0, 0, 1) 63 64 display.show() 65 66 67 # Optionally set an encryption key (16 byte AES key). MUST match both 68 # on the transmitter and receiver (or be set to None to disable/the default). 69 rfm69.encryption_key = ( 70 b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08" 71 ) 72 73 # set node addresses 74 rfm69.node = 1 75 rfm69.destination = 2 76 # initialize counter 77 counter = 0 78 # send a broadcast message from my_node with ID = counter 79 rfm69.send( 80 bytes("Startup message {} from node {}".format(counter, rfm69.node), "UTF-8") 81 ) 82 83 # Wait to receive packets. 84 print("Waiting for packets...") 85 button_pressed = None 86 while True: 87 # Look for a new packet: only accept if addresses to my_node 88 packet = rfm69.receive(with_header=True) 89 # If no packet was received during the timeout then None is returned. 90 if packet is not None: 91 # Received a packet! 92 # Print out the raw bytes of the packet: 93 print("Received (raw header):", [hex(x) for x in packet[0:4]]) 94 print("Received (raw payload): {0}".format(packet[4:])) 95 print("Received RSSI: {0}".format(rfm69.last_rssi)) 96 # Check buttons 97 if not btnA.value: 98 button_pressed = "A" 99 # Button A Pressed 100 display.fill(0) 101 display.text("AAA", width - 85, height - 7, 1) 102 display.show() 103 if not btnB.value: 104 button_pressed = "B" 105 # Button B Pressed 106 display.fill(0) 107 display.text("BBB", width - 75, height - 7, 1) 108 display.show() 109 if not btnC.value: 110 button_pressed = "C" 111 # Button C Pressed 112 display.fill(0) 113 display.text("CCC", width - 65, height - 7, 1) 114 display.show() 115 # send reading after any button pressed 116 if button_pressed is not None: 117 counter = counter + 1 118 # send a mesage to destination_node from my_node 119 rfm69.send( 120 bytes( 121 "message number {} from node {} button {}".format( 122 counter, rfm69.node, button_pressed 123 ), 124 "UTF-8", 125 ), 126 keep_listening=True, 127 ) 128 button_pressed = None