/ pi_radio / radio_rfm69.py
radio_rfm69.py
 1  # SPDX-FileCopyrightText: 2018 Brent Rubell for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Example for using the RFM69HCW Radio with Raspberry Pi.
 7  
 8  Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
 9  Author: Brent Rubell for Adafruit Industries
10  """
11  # Import Python System Libraries
12  import time
13  # Import Blinka Libraries
14  import busio
15  from digitalio import DigitalInOut, Direction, Pull
16  import board
17  # Import the SSD1306 module.
18  import adafruit_ssd1306
19  # Import the RFM69 radio module.
20  import adafruit_rfm69
21  
22  # Button A
23  btnA = DigitalInOut(board.D5)
24  btnA.direction = Direction.INPUT
25  btnA.pull = Pull.UP
26  
27  # Button B
28  btnB = DigitalInOut(board.D6)
29  btnB.direction = Direction.INPUT
30  btnB.pull = Pull.UP
31  
32  # Button C
33  btnC = DigitalInOut(board.D12)
34  btnC.direction = Direction.INPUT
35  btnC.pull = Pull.UP
36  
37  # Create the I2C interface.
38  i2c = busio.I2C(board.SCL, board.SDA)
39  
40  # 128x32 OLED Display
41  reset_pin = DigitalInOut(board.D4)
42  display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, reset=reset_pin)
43  # Clear the display.
44  display.fill(0)
45  display.show()
46  width = display.width
47  height = display.height
48  
49  # Configure Packet Radio
50  CS = DigitalInOut(board.CE1)
51  RESET = DigitalInOut(board.D25)
52  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
53  rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, 915.0)
54  prev_packet = None
55  # Optionally set an encryption key (16 byte AES key). MUST match both
56  # on the transmitter and receiver (or be set to None to disable/the default).
57  rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08'
58  
59  while True:
60      packet = None
61      # draw a box to clear the image
62      display.fill(0)
63      display.text('RasPi Radio', 35, 0, 1)
64  
65      # check for packet rx
66      packet = rfm69.receive()
67      if packet is None:
68          display.show()
69          display.text('- Waiting for PKT -', 15, 20, 1)
70      else:
71          # Display the packet text and rssi
72          display.fill(0)
73          prev_packet = packet
74          packet_text = str(prev_packet, "utf-8")
75          display.text('RX: ', 0, 0, 1)
76          display.text(packet_text, 25, 0, 1)
77          time.sleep(1)
78  
79      if not btnA.value:
80          # Send Button A
81          display.fill(0)
82          button_a_data = bytes("Button A!\r\n","utf-8")
83          rfm69.send(button_a_data)
84          display.text('Sent Button A!', 25, 15, 1)
85      elif not btnB.value:
86          # Send Button B
87          display.fill(0)
88          button_b_data = bytes("Button B!\r\n","utf-8")
89          rfm69.send(button_b_data)
90          display.text('Sent Button B!', 25, 15, 1)
91      elif not btnC.value:
92          # Send Button C
93          display.fill(0)
94          button_c_data = bytes("Button C!\r\n","utf-8")
95          rfm69.send(button_c_data)
96          display.text('Sent Button C!', 25, 15, 1)
97  
98      display.show()
99      time.sleep(0.1)