Close

Combine ABP and OTAA mode in one class

A project log for RP2040 MiniPill LoRa

Raspberry Pi Pico based LoRa Board

dominik-kuhnDominik Kuhn 09/30/2024 at 13:410 Comments

By simplifying and extending our LoRaWAN class, we can develop applications with very little effort. Below is a small example in both modes to send a simple string message to The Things Network (TTN): 

from machine import Pin, SPI
import machine
import time
from random import randrange

from PyLoRaWAN import PyLoRaWAN, ActivationType


# The Things Network (TTN) device details (available in TTN console)
# TTN device address, 4 Bytes, MSB (REPLACE WITH YOUR OWN!!!)
devaddr = [0x26, 0x00, 0x00, 0x00]
# TTN network session key, 16 Bytes, MSB (REPLACE WITH YOUR OWN!!!)
nwskey = [0xE0, 0x92, 0xEA, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
# TTN application session key, 16 Bytes, MSB (REPLACE WITH YOUR OWN!!!)
appskey = [0xB2, 0xB5, 0xC8, 0xDF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]


deveui = [0x70, 0xB3, 0xD5, 0x00, 0x00, 0x00, 0x00, 0x00]
appeui = [0x45, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
appkey = [0x44, 0x99, 0xB4, 0xD3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
devnonce = [randrange(256), randrange(256)]


spi = machine.SPI(0,
    baudrate=100000,
    polarity=1,
    phase=1,
    bits=8,
    firstbit=machine.SPI.MSB,
    sck=machine.Pin('GP2'),
    mosi=machine.Pin('GP3'),
    miso=machine.Pin('GP4')) 

cs = machine.Pin( 'GP5' ) # , Pin.OUT, value=1)

time.sleep(10)

lorawan = PyLoRaWAN(spi, cs)
lorawan.modem.set_output_power(16)
lorawan.modem.set_spreading_factor(12)

lorawan.join( ActivationType.ABP, auth={'devaddr':devaddr, 'nwskey':nwskey, 'appskey':appskey} )

for i in range(5):
    lorawan.send_test_msg()
    time.sleep(60)

time.sleep(5)

lorawan.join( ActivationType.OTAA, auth={'deveui':deveui, 'appeui':appeui, 'appkey':appkey, 'devnonce':devnonce} )

for i in range(5):
    lorawan.send_test_msg()
    time.sleep(60)

We are still developing and present more details later. Then we will also publish the link to our GitHub repo with the hardware and software for our board. 

Discussions