/ examples / rfm9x_node1_bonnet.py
rfm9x_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_rfm9x
 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 rfm9x Module
 57  try:
 58      rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
 59      display.text("rfm9x: Detected", 0, 0, 1)
 60  except RuntimeError:
 61      # Thrown on version mismatch
 62      display.text("rfm9x: ERROR", 0, 0, 1)
 63  
 64  display.show()
 65  
 66  # enable CRC checking
 67  rfm9x.enable_crc = True
 68  
 69  # set node addresses
 70  rfm9x.node = 1
 71  rfm9x.destination = 2
 72  # initialize counter
 73  counter = 0
 74  # send a broadcast message from my_node with ID = counter
 75  rfm9x.send(
 76      bytes("Startup message {} from node {}".format(counter, rfm9x.node), "UTF-8")
 77  )
 78  
 79  # Wait to receive packets.
 80  print("Waiting for packets...")
 81  button_pressed = None
 82  while True:
 83      # Look for a new packet: only accept if addresses to my_node
 84      packet = rfm9x.receive(with_header=True)
 85      # If no packet was received during the timeout then None is returned.
 86      if packet is not None:
 87          # Received a packet!
 88          # Print out the raw bytes of the packet:
 89          print("Received (raw header):", [hex(x) for x in packet[0:4]])
 90          print("Received (raw payload): {0}".format(packet[4:]))
 91          print("Received RSSI: {0}".format(rfm9x.last_rssi))
 92      # Check buttons
 93      if not btnA.value:
 94          button_pressed = "A"
 95          # Button A Pressed
 96          display.fill(0)
 97          display.text("AAA", width - 85, height - 7, 1)
 98          display.show()
 99      if not btnB.value:
100          button_pressed = "B"
101          # Button B Pressed
102          display.fill(0)
103          display.text("BBB", width - 75, height - 7, 1)
104          display.show()
105      if not btnC.value:
106          button_pressed = "C"
107          # Button C Pressed
108          display.fill(0)
109          display.text("CCC", width - 65, height - 7, 1)
110          display.show()
111          # send reading after any button pressed
112      if button_pressed is not None:
113          counter = counter + 1
114          # send a  mesage to destination_node from my_node
115          rfm9x.send(
116              bytes(
117                  "message number {} from node {} button {}".format(
118                      counter, rfm9x.node, button_pressed
119                  ),
120                  "UTF-8",
121              ),
122              keep_listening=True,
123          )
124          button_pressed = None