Joining The Things Network
This code will allow your device to join The Things Network via the Nano Gateway used in this workshop.
You will need to add your own device EUI, App EUI, and App Key in the section below that says "create an OTA authentication".
Details for each workshop device follow this code.
from machine import UART from network import LoRa import binascii import os import pycom pycom.heartbeat(False) # Setting up the UART to dump the output to the console uart = UART(0, 115200) os.dupterm(uart) # Getting the LoRa MAC lora = LoRa(mode=LoRa.LORAWAN, public=1, adr=0, tx_retries=0) print("LORA MAC") print(binascii.hexlify(lora.mac()))
In the above boot code, we're doing a few things:
- Setting up our serial console, as we have done in other examples
- Importing the LoRa wireless protocol
- Importing utilities to handle binary and hex numbers
- Getting and printing the LoRa MAC address of our device
The LoRa MAC address is important to use of the device in LoRaWAN networks. The device address is used to generate unique encryption keys so the device can exchange data with the LoRaWAN controller via a local gateway.
""" OTAA (Over the Air Activation) Node example compatible with the LoPy Nano Gateway """ from network import LoRa import socket import ubinascii import struct import time # Initialize LoRa in LORAWAN mode. lora = LoRa(mode=LoRa.LORAWAN) # create an OTA authentication dev_eui = ubinascii.unhexlify('') # these settings can be found from TTN app_eui = ubinascii.unhexlify('') # these settings can be found from TTN app_key = ubinascii.unhexlify('') # these settings can be found from TTN # set the 3 default channels to the same frequency (must be before sending the OTAA join request) lora.add_channel(0, frequency=916800000, dr_min=0, dr_max=5) lora.add_channel(1, frequency=916800000, dr_min=0, dr_max=5) lora.add_channel(2, frequency=916800000, dr_min=0, dr_max=5) # 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...') print('Network joined!') # 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 non-blocking s.setblocking(False) time.sleep(5.0) """ Your own code can be written below! """ 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)
At the end of the code example above is a section you can replace with your own code.
In order to join "The Things Network" please take the application_eui for our classroom project (the same for everyone), the Device EUI (derived from your LoRa MAC address), and the app_key (derived from the device eui and the application eui) and insert them into the ('') section below "# create an OTA authentication" in the main.py code.
You can find the appropriate details for your device below. The serial number is printed on a barcode at the bottom of your device.
LoPy Serial Numbers & Join Details
- 182001369 (pysense issues)
- dev_eui: 0021C3AAF82E91A6
- app_eui: 70B3D57ED00113A2
- app_key: 56ED92FA7C2365051CEA04369E4775E5
- 1802000743
- dev_eui: 000D63BE4B49BC05
- app_eui: 70B3D57ED00113A2
- app_key: C74806CEBCCFA8606C5076212A5ED9CC
- 1802001867
- dev_eui: 0045278A2DACD166
- app_eui: 70B3D57ED00113A2
- app_key: 798345C05A0CB35AA91034885A1750EE
- 1802000134
- dev_eui: 004F1205CE255D54
- app_eui: 70B3D57ED00113A2
- app_key: 19A8BA193035B5F6BBE0F62FABD85799
- 1802000378
- dev_eui: 00F610529D10E1BB
- app_eui: 70B3D57ED00113A2
- app_key: 2C5ED3CF5DA523EF5357E3D40B2588F5
- 18020002289
- dev_eui: 0083E1479AF92E16
- app_eui: 70B3D57ED00113A2
- app_key: F3FA9D6B1016D7674D198B8044EA8C6C
- 1802000978
- dev_eui: 006EFCCD13739684
- app_eui: 70B3D57ED00113A2
- app_key: F41114F66272D51635237F5AC475062B
- 1802000130 (pysense issues)
- dev_eui: 00A884CD216C4101
- app_eui: 70B3D57ED00113A2
- app_key: D7C3A48936CF17B4197803C4730DC4DE
- 1802001328
- dev_eui: 004CDC94DF569DAD
- app_eui: 70B3D57ED00113A2
- app_key: 3738B4BBF2909308F9F06756CC5C297B
- 1802000751
- dev_eui: 00503B4672E4EA14
- app_eui: 70B3D57ED00113A2
- app_key: 462D89D9BC0C19815A7221AC48FB1B1F