# Echo any messages received (using custom LoRa parameters).
#
# The pin configuration used here is for the first LoRa module of these boards:
# https://makerfabs.com/esp32-lora-gateway.html
from lora import LoRa
from machine import Pin, SPI
from time import sleep
# SPI pins
SCK = 2
MOSI = 3
MISO = 4
CS = 5
RX = 6
# Initialize SPI
spi = SPI(0,
baudrate=1000000,
polarity=1,
phase=1,
bits=8,
firstbit=machine.SPI.MSB,
sck=machine.Pin(SCK),
mosi=machine.Pin(MOSI),
miso=machine.Pin(MISO))
spi.init()
# Setup LoRa
lora = LoRa(
spi,
cs=Pin(CS, Pin.OUT),
rx=Pin(RX, Pin.IN),
frequency=915.0,
bandwidth=250000,
spreading_factor=10,
coding_rate=5,
)
# Receive handler
def handler(x):
# Echo message
lora.send(x)
# Put module back in recv mode
lora.recv()
# Set handler
lora.on_recv(handler)
# Put module in recv mode
lora.recv()
# No need for main loop, code is asynchronous