LoRa und TheThingsNetwork

H. Högl, <Hubert.Hoegl@hs-augsburg.de>

LoRaWAN Code fuer ABP Aktivierung. Laeuft auf dem LoPy Modul:

# H. Hoegl, 4/2018

import binascii, struct
from network import LoRa
import time
import socket
import struct

# ABP
def lora():
    # https://www.thethingsnetwork.org/docs/devices/lopy/usage.html
    # https://docs.pycom.io/chapter/gettingstarted/registration/lora.html
    # https://docs.pycom.io/chapter/tutorials/lora/lorawan-abp.html

    # Device EUI = 70B3D5499C804184 (wird von TTN ignoriert)
    # print(binascii.hexlify(lora.mac()).upper().decode('utf-8'))

    # Auf ttn gibt es eine "button_count_app" AppEUI=70B3D57ED000BBD2
    # Das Device heisst lopy_button_device
    print("LoRa ABP example")
    lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)

    # die folgenden drei Teile werden von TTN berechnet, wenn man das
    # Device anlegt. dev_addr wird zur Kommunikation verwendet.
    dev_addr = struct.unpack(">l", binascii.unhexlify('26011BEE'))[0]
    nwk_swkey = binascii.unhexlify('0C5ACBB5F46C48CE110EF6C20C430A09')
    app_swkey = binascii.unhexlify('80846A7ED4E027D02464F6FC7A84AB48')

    lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))
    print(lora)

    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
    # s.settimeout(3.0)

    s.setblocking(True)
    s.send(bytes([1, 2, 3]))

    time.sleep(3)

    s.setblocking(False)
    try:
        data = s.recv(64)
        print("received data:", data)
    except socket.timeout:
        print('No packet received')

lora()