simplify rfm69 example to match rfm9x example,update rfm9x example to decode utf8 instead of ascii

This commit is contained in:
brentru 2019-01-18 12:52:12 -05:00
parent 30ce6c95da
commit b949734db2
2 changed files with 30 additions and 30 deletions

View file

@ -1,16 +1,19 @@
""" """
Demo of using the RFM69HCW Radio with Raspberry Pi. Example for using the RFM69HCW Radio with Raspberry Pi.
Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
Author: Brent Rubell for Adafruit Industries Author: Brent Rubell for Adafruit Industries
""" """
# Import Python System Libraries
import time import time
# Import Blinka Libraries
import busio import busio
from digitalio import DigitalInOut, Direction, Pull from digitalio import DigitalInOut, Direction, Pull
import board import board
# Import the RFM69 radio module.
import adafruit_rfm69
# Import the SSD1306 module. # Import the SSD1306 module.
import adafruit_ssd1306 import adafruit_ssd1306
# Import the RFM69 radio module.
import adafruit_rfm69
# Button A # Button A
btnA = DigitalInOut(board.D5) btnA = DigitalInOut(board.D5)
@ -22,6 +25,11 @@ btnB = DigitalInOut(board.D6)
btnB.direction = Direction.INPUT btnB.direction = Direction.INPUT
btnB.pull = Pull.UP btnB.pull = Pull.UP
# Button C
btnC = DigitalInOut(board.D12)
btnC.direction = Direction.INPUT
btnC.pull = Pull.UP
# Create the I2C interface. # Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA) i2c = busio.I2C(board.SCL, board.SDA)
@ -38,13 +46,10 @@ CS = DigitalInOut(board.CE1)
RESET = DigitalInOut(board.D25) RESET = DigitalInOut(board.D25)
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, 915.0) rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, 915.0)
prev_packet = None
# Optionally set an encryption key (16 byte AES key). MUST match both # Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default). # on the transmitter and receiver (or be set to None to disable/the default).
rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08' rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08'
# Data to send
packet_data = bytes('Hello Feather!\r\n',"utf-8")
prev_packet = None
while True: while True:
packet = None packet = None
@ -54,41 +59,36 @@ while True:
# check for packet rx # check for packet rx
packet = rfm69.receive() packet = rfm69.receive()
print(packet)
if packet is None: if packet is None:
display.show() display.show()
display.text('- Waiting for PKT -', 0, 20, 1) display.text('- Waiting for PKT -', 15, 20, 1)
else: else:
# Display the packet text and rssi # Display the packet text and rssi
display.fill(0) display.fill(0)
prev_packet = packet prev_packet = packet
packet_text = str(prev_packet, 'ascii') packet_text = str(prev_packet, "utf-8")
display.text('RX: ', 0, 0, 1) display.text('RX: ', 0, 0, 1)
display.text(packet_text, 25, 0, 1) display.text(packet_text, 25, 0, 1)
display.text('RSSI: ', 0, 20, 1) time.sleep(1)
display.text(str(rfm69.rssi), 35, 20, 1)
time.sleep(2)
if not btnA.value: if not btnA.value:
# Send Packet # Send Button A
rfm69.send(packet_data)
display.fill(0) display.fill(0)
display.text('Sent Packet', 0, 25, 1) button_a_data = bytes("Button A!\r\n","utf-8")
display.show() rfm69.send(button_a_data)
time.sleep(0.2) display.text('Sent Button A!', 25, 15, 1)
elif not btnB.value: elif not btnB.value:
# Display the previous packet text and rssi # Send Button B
display.fill(0) display.fill(0)
if prev_packet is not None: button_b_data = bytes("Button B!\r\n","utf-8")
packet_text = str(prev_packet, 'ascii') rfm69.send(button_b_data)
display.text('RX: ', 0, 0, 1) display.text('Sent Button B!', 25, 15, 1)
display.text(packet_text, 25, 0, 1) elif not btnC.value:
display.text('RSSI: ', 0, 20, 1) # Send Button C
display.text(str(rfm69.rssi), 35, 20, 1) display.fill(0)
else: button_c_data = bytes("Button C!\r\n","utf-8")
display.text('No Pkt RCVd', 0, 16, 1) rfm69.send(button_c_data)
display.show() display.text('Sent Button C!', 25, 15, 1)
time.sleep(2)
display.show() display.show()
time.sleep(0.1) time.sleep(0.1)

View file

@ -64,7 +64,7 @@ while True:
# Display the packet text and rssi # Display the packet text and rssi
display.fill(0) display.fill(0)
prev_packet = packet prev_packet = packet
packet_text = str(prev_packet, 'ascii') packet_text = str(prev_packet, "utf-8")
display.text('RX: ', 0, 0, 1) display.text('RX: ', 0, 0, 1)
display.text(packet_text, 25, 0, 1) display.text(packet_text, 25, 0, 1)
time.sleep(1) time.sleep(1)