This commit is contained in:
parent
0663c6f704
commit
68a1d8a972
47
main.py
47
main.py
|
@ -1,30 +1,33 @@
|
|||
from pyLoraRFM9x import LoRa, ModemConfig
|
||||
import serial
|
||||
|
||||
from digitalio import DigitalInOut, Direction, Pull
|
||||
import board
|
||||
import adafruit_rfm69
|
||||
|
||||
CS = DigitalInOut(board.CE1)
|
||||
RESET = DigitalInOut(board.D25)
|
||||
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
|
||||
rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, 915.0)
|
||||
|
||||
addr = '/tmp/virttty'
|
||||
conn = serial.serial_for_url(addr)
|
||||
|
||||
# This is our callback function that runs when a message is received
|
||||
def on_recv(payload):
|
||||
print("From:", payload.header_from)
|
||||
print("Received:", payload.message)
|
||||
print("RSSI: {}; SNR: {}".format(payload.rssi, payload.snr))
|
||||
conn.write(payload.message)
|
||||
ser = serial.serial_for_url(addr)
|
||||
|
||||
|
||||
# Use chip select 1. GPIO pin 5 will be used for interrupts and set reset pin to 25
|
||||
# The address of this device will be set to 2
|
||||
lora = LoRa(1, 5, 2, reset_pin = 25, modem_config=ModemConfig.Bw125Cr45Sf128, tx_power=14, acks=True)
|
||||
lora.on_recv = on_recv
|
||||
|
||||
# Send a message to a recipient device with address 10
|
||||
# Retry sending the message twice if we don't get an acknowledgment from the recipient
|
||||
while True:
|
||||
message = conn.readline()
|
||||
if message != '':
|
||||
print("new messsage:", message)
|
||||
status = lora.send_to_wait(message, 10, retries=2)
|
||||
if status is True:
|
||||
print("Message sent!")
|
||||
packet = None
|
||||
packet = rfm69.receive()
|
||||
ser_data = bytearray()
|
||||
while ser.inWaiting() > 0:
|
||||
recieved_byte = ser.read(1)
|
||||
ser_data += recieved_byte
|
||||
if ser_data == b'':
|
||||
pass
|
||||
else:
|
||||
print("No acknowledgment from recipient")
|
||||
print("new message:", ser_data.decode())
|
||||
rfm69.send(ser_data)
|
||||
if packet is None:
|
||||
pass
|
||||
else:
|
||||
print("Received:", packet.decode())
|
||||
ser.write(packet)
|
Loading…
Reference in New Issue