Instantly share code, notes, and snippets.

Embed
What would you like to do?
LoPy node
""" OTAA Node example compatible with the LoPy Nano Gateway """
from network import LoRa
import socket
import binascii
import struct
import time
# Initialize LoRa in LORAWAN mode.
lora = LoRa(mode=LoRa.LORAWAN)
# create an OTA authentication params
dev_eui = binascii.unhexlify('70B3D5499852A8CB'.replace(' ',''))
app_eui = binascii.unhexlify('70B3D57EF0004674'.replace(' ',''))
app_key = binascii.unhexlify('9897E97CE0FBD2224AC559EDD224BB67'.replace(' ',''))
# instead of set the 3 default channels to the same frequency. loop to remove all existing channels add just one - @jmarcelino
for channel in range(0, 72):
lora.remove_channel(channel)
lora.add_channel(0, frequency=915100000, dr_min=0, dr_max=4)
# join a network using OTAA
lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)
# wait until the module has joined the network
while not lora.has_joined():
time.sleep(2.5)
print('Not joined yet...')
# remove all the non-default channels
for i in range(3, 16):
lora.remove_channel(i)
# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
# make the socket blocking
s.setblocking(False)
time.sleep(5.0)
for i in range (200):
s.send(b'PKT #' + bytes([i]))
time.sleep(4)
rx = s.recv(256)
if rx:
print(rx)
time.sleep(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment