/ pi_radio / radio_rfm9x.py
radio_rfm9x.py
 1  # SPDX-FileCopyrightText: 2018 Brent Rubell for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Example for using the RFM9x 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 RFM9x
20  import adafruit_rfm9x
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 LoRa Radio
50  CS = DigitalInOut(board.CE1)
51  RESET = DigitalInOut(board.D25)
52  spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
53  rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, 915.0)
54  rfm9x.tx_power = 23
55  prev_packet = None
56  
57  while True:
58      packet = None
59      # draw a box to clear the image
60      display.fill(0)
61      display.text('RasPi LoRa', 35, 0, 1)
62  
63      # check for packet rx
64      packet = rfm9x.receive()
65      if packet is None:
66          display.show()
67          display.text('- Waiting for PKT -', 15, 20, 1)
68      else:
69          # Display the packet text and rssi
70          display.fill(0)
71          prev_packet = packet
72          packet_text = str(prev_packet, "utf-8")
73          display.text('RX: ', 0, 0, 1)
74          display.text(packet_text, 25, 0, 1)
75          time.sleep(1)
76  
77      if not btnA.value:
78          # Send Button A
79          display.fill(0)
80          button_a_data = bytes("Button A!\r\n","utf-8")
81          rfm9x.send(button_a_data)
82          display.text('Sent Button A!', 25, 15, 1)
83      elif not btnB.value:
84          # Send Button B
85          display.fill(0)
86          button_b_data = bytes("Button B!\r\n","utf-8")
87          rfm9x.send(button_b_data)
88          display.text('Sent Button B!', 25, 15, 1)
89      elif not btnC.value:
90          # Send Button C
91          display.fill(0)
92          button_c_data = bytes("Button C!\r\n","utf-8")
93          rfm9x.send(button_c_data)
94          display.text('Sent Button C!', 25, 15, 1)
95  
96  
97      display.show()
98      time.sleep(0.1)