This commit is contained in:
parent
0663c6f704
commit
68a1d8a972
49
main.py
49
main.py
|
@ -1,30 +1,33 @@
|
||||||
from pyLoraRFM9x import LoRa, ModemConfig
|
from pyLoraRFM9x import LoRa, ModemConfig
|
||||||
import serial
|
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'
|
addr = '/tmp/virttty'
|
||||||
conn = serial.serial_for_url(addr)
|
ser = 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)
|
|
||||||
|
|
||||||
|
|
||||||
# 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:
|
while True:
|
||||||
message = conn.readline()
|
packet = None
|
||||||
if message != '':
|
packet = rfm69.receive()
|
||||||
print("new messsage:", message)
|
ser_data = bytearray()
|
||||||
status = lora.send_to_wait(message, 10, retries=2)
|
while ser.inWaiting() > 0:
|
||||||
if status is True:
|
recieved_byte = ser.read(1)
|
||||||
print("Message sent!")
|
ser_data += recieved_byte
|
||||||
else:
|
if ser_data == b'':
|
||||||
print("No acknowledgment from recipient")
|
pass
|
||||||
|
else:
|
||||||
|
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