2019-11-20 17:24:28 +00:00
|
|
|
from uPyLora import config_lora
|
|
|
|
from uPyLora.sx127x import SX127x
|
|
|
|
from uPyLora.controller_esp32 import ESP32Controller
|
|
|
|
|
|
|
|
class LoRaTransceiver:
|
2019-11-20 17:37:43 +00:00
|
|
|
def __init__(self, spreadingfactor=7, bandwidth=125E3, frequency=868E6, syncword=0x12, display=None):
|
2019-11-30 19:59:38 +00:00
|
|
|
""" set up radio module
|
|
|
|
"""
|
2019-11-20 17:24:28 +00:00
|
|
|
self.controller = ESP32Controller()
|
|
|
|
self.lora = self.controller.add_transceiver(SX127x(name='LoRa'), pin_id_ss=ESP32Controller.PIN_ID_FOR_LORA_SS, pin_id_RxDone=ESP32Controller.PIN_ID_FOR_LORA_DIO0)
|
|
|
|
|
|
|
|
self.setSpreadingFactor(spreadingfactor)
|
|
|
|
self.setSignalBandwidth(bandwidth)
|
|
|
|
self.setFrequency(frequency)
|
|
|
|
self.setSyncWord(syncword)
|
|
|
|
|
|
|
|
self.display = display
|
|
|
|
|
|
|
|
def setSpreadingFactor(self, spreadingfactor):
|
|
|
|
""" set spreading factor
|
|
|
|
possible values are 7-12
|
|
|
|
"""
|
2019-11-21 15:38:49 +00:00
|
|
|
self.spreadingfactor = spreadingfactor
|
2019-11-20 17:24:28 +00:00
|
|
|
self.lora.setSpreadingFactor(spreadingfactor)
|
|
|
|
|
|
|
|
def setFrequency(self, frequency):
|
|
|
|
""" set frequency (= channel used)
|
|
|
|
for now only 868 Mhz and 868.1 MHz are supported
|
|
|
|
"""
|
2019-11-21 15:38:49 +00:00
|
|
|
self.frequency = frequency
|
2019-11-20 17:24:28 +00:00
|
|
|
self.lora.setFrequency(frequency)
|
|
|
|
|
|
|
|
def setSignalBandwidth(self, bandwidth):
|
|
|
|
""" set the bandwidth
|
2019-11-20 17:37:43 +00:00
|
|
|
possible values should be 125 kHz or 250 kHz according to EU 868 frequency plan
|
2019-11-20 17:24:28 +00:00
|
|
|
"""
|
2019-11-21 15:38:49 +00:00
|
|
|
self.bandwidth = bandwidth
|
2019-11-20 17:24:28 +00:00
|
|
|
self.lora.setSignalBandwidth(bandwidth)
|
|
|
|
|
|
|
|
def setSyncWord(self, syncword):
|
|
|
|
""" set the sync word
|
|
|
|
should be 0x12 for plain LoRa and 0x34 for LoRaWAN
|
|
|
|
"""
|
2019-11-21 15:38:49 +00:00
|
|
|
self.syncword = syncword
|
2019-11-20 17:24:28 +00:00
|
|
|
self.lora.setSyncWord(syncword)
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
""" send raw data
|
|
|
|
"""
|
2019-11-21 15:38:49 +00:00
|
|
|
data_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), data))).upper()
|
|
|
|
print("Sending data {}".format(data_hex))
|
2019-11-30 18:37:07 +00:00
|
|
|
if self.display is not None:
|
|
|
|
self.display.show_text_wrap("Sending data {}".format(data_hex))
|
2019-11-20 17:24:28 +00:00
|
|
|
self.lora.println_raw(data)
|
|
|
|
|
|
|
|
|
|
|
|
def send_string(self, message):
|
|
|
|
""" send a message
|
|
|
|
message should be a string
|
|
|
|
"""
|
2019-11-21 15:38:49 +00:00
|
|
|
print("Sending string {}".format(message))
|
2019-11-30 18:37:07 +00:00
|
|
|
if self.display is not None:
|
|
|
|
self.display.show_text_wrap("Sending string {}".format(message))
|
2019-11-20 17:24:28 +00:00
|
|
|
self.lora.println(message)
|
|
|
|
|
|
|
|
|
|
|
|
def recv(self):
|
|
|
|
""" start listening for messages and output them on the console """
|
2019-11-21 15:38:49 +00:00
|
|
|
print("Receiving at frequency {} with spreading factor {} ...".format(self.frequency, self.spreadingfactor))
|
2019-11-20 17:24:28 +00:00
|
|
|
if self.display is not None:
|
2019-11-21 15:38:49 +00:00
|
|
|
self.display.show_text_wrap("Receiving at frequency {} with spreading factor {} ...".format(self.frequency, self.spreadingfactor))
|
2019-11-20 17:24:28 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
# check for new message
|
|
|
|
if self.lora.receivedPacket():
|
|
|
|
self.lora.blink_led()
|
|
|
|
|
2019-11-21 15:38:49 +00:00
|
|
|
print(" got packet: ")
|
2019-11-20 17:24:28 +00:00
|
|
|
try:
|
2019-11-21 15:38:49 +00:00
|
|
|
payload_bytes = bytes(self.lora.read_payload_raw())
|
|
|
|
payload_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), payload_bytes))).upper()
|
|
|
|
payload_string = self.lora.read_payload_raw()
|
|
|
|
print(" length : {0}".format(len(payload_bytes)))
|
|
|
|
print(" hex : {0}".format(payload_hex))
|
|
|
|
print(" bytes : {0}".format(payload_bytes))
|
|
|
|
print(" RSSI : {0}\n".format(self.lora.packetRssi()))
|
2019-11-20 17:24:28 +00:00
|
|
|
if self.display is not None:
|
2019-11-21 15:38:49 +00:00
|
|
|
self.display.show_text_wrap("Received: {0} RSSI: {1}".format(payload_bytes, self.lora.packetRssi()))
|
2019-11-20 17:24:28 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|