From 0cd1cf2d8cefabe91b697ffe538fcba2c4ac1147 Mon Sep 17 00:00:00 2001 From: spirkelmann Date: Sat, 30 Nov 2019 19:44:45 +0100 Subject: [PATCH] added examples for lora and lorawan --- micropython/examples/lora.py | 20 ++++++++++++++++ micropython/examples/lorawan.py | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 micropython/examples/lora.py create mode 100644 micropython/examples/lorawan.py diff --git a/micropython/examples/lora.py b/micropython/examples/lora.py new file mode 100644 index 0000000..6b2625f --- /dev/null +++ b/micropython/examples/lora.py @@ -0,0 +1,20 @@ +from lora_transceiver import LoRaTransceiver + +from uPySensors.ssd1306_i2c import Display + +d = Display() + +# create transceiver for LoRaWAN frequency (channel 0 = 868.1 Mhz) +lora = LoRaTransceiver(frequency=868.1E6, syncword=0x34, display=d) + +# change the spreading factor +lora.setSpreadingFactor(7) + +# sending string data +lora.send_string('imaginaerraum') + +# sending raw data +#lora.send([0x61, 0x62, 0x63]) + +# start receiving data +#lora.recv() diff --git a/micropython/examples/lorawan.py b/micropython/examples/lorawan.py new file mode 100644 index 0000000..96092d1 --- /dev/null +++ b/micropython/examples/lorawan.py @@ -0,0 +1,42 @@ +from lora_transceiver import LoRaTransceiver + +from uPySensors.ssd1306_i2c import Display + +import uLoRaWAN +from uLoRaWAN.MHDR import MHDR + +d = Display() + +# create transceiver for LoRaWAN frequency (channel 0 = 868.1 Mhz) +lora = LoRaTransceiver(frequency=868.1E6, syncword=0x34, display=d) + +# change the spreading factor +lora.setSpreadingFactor(9) + +# set address and keys for LoRaWAN +devAddr = [0x26, 0x01, 0x16, 0x5C] # ir_test_device_01 +#devAddr = [0x26, 0x01, 0x18, 0x52] # ir_test_device_02 +#devAddr = [0x26, 0x01, 0x1E, 0x4F] # ir_test_device_03 + +nwkSKey = [0x9D, 0x95, 0x0F, 0xAB, 0xCB, 0x63, 0xD3, 0x04, 0xBC, 0x09, 0xC4, 0x9E, 0xC5, 0xDF, 0x3C, 0x37] +appSKey = [0xC9, 0x6C, 0x00, 0xD1, 0xB0, 0x1C, 0x2E, 0x42, 0x11, 0xBA, 0x32, 0x6F, 0x2F, 0xC2, 0x75, 0x6A] + +devAddr_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), devAddr))).upper() +nwkSKey_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), nwkSKey))).upper() +appSKey_hex = " ".join(list(map(lambda x: "{0:02x}".format(x), appSKey))).upper() +print("devAddr: ", devAddr_hex) +print("nwkSKey: ", nwkSKey_hex) +print("appSKey: ", appSKey_hex) + +# lorawan object for conversion of data in LoRaWAN message format +lorawan = uLoRaWAN.new(nwkSKey, appSKey) + +def send_lorawan(message, frame_cnt=0): + lorawan.create(MHDR.UNCONF_DATA_UP, {'devaddr': devAddr, 'fcnt': frame_cnt, 'data': list(map(ord, message)) }) + payload = lorawan.to_raw() + + lora.send(payload) + + print("lorawan message sent") + +send_lorawan("Hello world!")