LoRa-Workshop/micropython/lorawan_example.py

43 lines
1.5 KiB
Python
Raw Normal View History

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!")