def __init__(self, deviceAddress, applicationKey, networkKey):
from network import LoRa
import socket
import binascii
import struct
self.deviceAddress = deviceAddress
self.applicationKey = applicationKey
self.networkKey = networkKey
self.lora = LoRa(mode=LoRa.LORAWAN)
dev_addr = struct.unpack(">l", binascii.unhexlify(deviceAddress.replace(' ','')))[0]
nwk_swkey = binascii.unhexlify(networkKey.replace(' ',''))
app_swkey = binascii.unhexlify(applicationKey.replace(' ',''))
self.lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))
self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
self.s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
self.s.setblocking(False)
python类SOL_LORA的实例源码
def increase_lora_delivary_chances(element):
if (element.attempts == 2): element.DR = 4
elif (element.attempts == 4): element.DR = 2
element.socket.setsockopt(socket.SOL_LORA, socket.SO_DR, element.DR)
def connect(self, dev_eui, app_eui, app_key):
"""
Connect device to LoRa.
Set the socket and lora instances.
"""
dev_eui = unhexlify(dev_eui)
app_eui = unhexlify(app_eui)
app_key = unhexlify(app_key)
# Disable blue blinking and turn LED off
LED.heartbeat(False)
LED.off()
# Initialize LoRa in LORAWAN mode
self.lora = LoRa(mode = LoRa.LORAWAN)
# Join a network using OTAA (Over the Air Activation)
self.lora.join(activation = LoRa.OTAA, auth = (dev_eui, app_eui, app_key), timeout = 0)
# Wait until the module has joined the network
count = 0
while not self.lora.has_joined():
LED.blink(1, 2.5, 0xff0000)
# print("Trying to join: " , count)
count = count + 1
# Create a LoRa socket
LED.blink(2, 0.1)
self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
# Set the LoRaWAN data rate
self.s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
# Make the socket non-blocking
self.s.setblocking(False)
# print ("Joined! ", count)
# print("Create LoRaWAN socket")
# Create a raw LoRa socket
self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
self.s.setblocking(False)