added class variables for radio parameters (frequency, spreading factor, ...) and nice output for recv() function
This commit is contained in:
parent
23a63cac12
commit
88c6d94a78
|
@ -20,30 +20,35 @@ class LoRaTransceiver:
|
|||
""" set spreading factor
|
||||
possible values are 7-12
|
||||
"""
|
||||
self.spreadingfactor = spreadingfactor
|
||||
self.lora.setSpreadingFactor(spreadingfactor)
|
||||
|
||||
def setFrequency(self, frequency):
|
||||
""" set frequency (= channel used)
|
||||
for now only 868 Mhz and 868.1 MHz are supported
|
||||
"""
|
||||
self.frequency = frequency
|
||||
self.lora.setFrequency(frequency)
|
||||
|
||||
def setSignalBandwidth(self, bandwidth):
|
||||
""" set the bandwidth
|
||||
possible values should be 125 kHz or 250 kHz according to EU 868 frequency plan
|
||||
"""
|
||||
self.bandwidth = bandwidth
|
||||
self.lora.setSignalBandwidth(bandwidth)
|
||||
|
||||
def setSyncWord(self, syncword):
|
||||
""" set the sync word
|
||||
should be 0x12 for plain LoRa and 0x34 for LoRaWAN
|
||||
"""
|
||||
self.syncword = syncword
|
||||
self.lora.setSyncWord(syncword)
|
||||
|
||||
def send(self, data):
|
||||
""" send raw data
|
||||
"""
|
||||
print("Sending {}".format(data))
|
||||
data_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), data))).upper()
|
||||
print("Sending data {}".format(data_hex))
|
||||
self.lora.println_raw(data)
|
||||
|
||||
if self.display is not None:
|
||||
|
@ -53,7 +58,7 @@ class LoRaTransceiver:
|
|||
""" send a message
|
||||
message should be a string
|
||||
"""
|
||||
print("Sending {}".format(message))
|
||||
print("Sending string {}".format(message))
|
||||
self.lora.println(message)
|
||||
|
||||
if self.display is not None:
|
||||
|
@ -61,18 +66,26 @@ class LoRaTransceiver:
|
|||
|
||||
def recv(self):
|
||||
""" start listening for messages and output them on the console """
|
||||
print("Receiving at frequency {} with spreading factor {} ...".format(self.frequency, self.spreadingfactor))
|
||||
if self.display is not None:
|
||||
self.display.show_text_wrap("Receiving ...")
|
||||
self.display.show_text_wrap("Receiving at frequency {} with spreading factor {} ...".format(self.frequency, self.spreadingfactor))
|
||||
|
||||
while True:
|
||||
# check for new message
|
||||
if self.lora.receivedPacket():
|
||||
self.lora.blink_led()
|
||||
|
||||
print(" got packet: ")
|
||||
try:
|
||||
payload = self.lora.read_payload()
|
||||
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()))
|
||||
if self.display is not None:
|
||||
self.display.show_text_wrap("Received: {0} RSSI: {1}".format(payload.decode(), lora.packetRssi()))
|
||||
self.display.show_text_wrap("Received: {0} RSSI: {1}".format(payload_bytes, self.lora.packetRssi()))
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
|
Loading…
Reference in New Issue
Block a user