Getting started with LoRaWAN requires a gateway. For a lot of us experimenting, we are using inexpensive, single channel gateways. It takes a little extra setup to get your LoPy working on a single channel gateway.
If you don't have a single channel gateway yet, check out my Raspberry Pi Single Channel Gateway
Before we start, you'll need to have your LoPy updated to the latest firmware. The Pycom Quick Start Guide will walk you through that procedure and also has some great information to get you more familiar with your LoPy.
To get started we will setup an application and a device on The Things Network.
Login to console.thethingsnetwork.org and select 'Applications'.
Select 'add application'. Fill out the form - be sure to choose an appropriate region, I'm going to choose us-west:
Then click 'Add application' at the bottom.
Note - it can take a few seconds for the application to be registered with a handler, so it is not a problem if you see some messages about 'error not connected to handler'. Wait 30 seconds and refresh the page - everything should be good.
Don't close this browser window, we're going to be using again very soon.
Before we can add our device we must know its unique identifier, or Device EUI.
Connect your LoPy and open up the Pymakr application (or telnet to your LoPy if you prefer).
If the bottom of your Pymakr screen has messages about failure to connect, please read through the Quick Start Guide - Connecting your board through Pymakr.
You should see this:
Here is what we type, or copy and paste:
from network import LoRa import binascii binascii.hexlify(LoRa(mode=LoRa.LORAWAN).mac())
These commands will generate output that looks like:
The string between the single quotes is your Device EUI. Copy that to a safe place, we'll need it later.
My Device EUI from the above example was: 70b3d5499e8e7877
Now we will add our device to The Things Network.
Note - if you have closed the browser window, no problem, log back into console.thethingsnetwork.org and select Applications from the top. Then select the application name that we were working on from the list presented.
Click 'register device'. Fill out the form, you can paste the device id right into the 'Device EUI' field - the page will format it for you. Here is my form:
Once you're ready, click 'Register'. We need to setup the registration via ABP, instead of the default OTAA, so click 'Settings' and then select the ABP box:
Then scroll down to the bottom and click 'Save'.
Note - you will need to know what frequency your single channel gateway is on. At the bottom of this guide I have included a channel to frequency chart for the USA 915Mhz.
Return to Pymakr and click inside the console at the bottom.
Create a new file called main.py.
Then copy and paste the code snippet below into the new file - after setting the app_eui and app_key variables to the values you got from The Things Network.
Also, update the freq variable to the frequency that your gateway is on.
from network import LoRa import struct import binascii import socket freq = 902300000 # Initialize LoRa in LORAWAN mode. lora = LoRa(mode=LoRa.LORAWAN) #Setup the single channel for connection to the gateway for channel in range(0, 72): lora.remove_channel(channel) for chan in range(0, 8): lora.add_channel(chan, frequency=freq, dr_min=0, dr_max=3) #Device Address dev_addr = struct.unpack(">l", binascii.unhexlify('2602122D')) #Network Session Key nwk_swkey = binascii.unhexlify('CD5762032F4BFC67F368CA39304587C1') #App Session Key app_swkey = binascii.unhexlify('53E2A0418722F81D845279774460D040') lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey)) # 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, 3) # make the socket non-blocking s.setblocking(False)
Once you have pasted the code-snippet into main.py - save the file.
Now upload the code via FTP to /flash/main.py on the LoPy.
To find instructions for connecting to your LoPy and uploading files via FTP, refer to the Pycom Quick Start Guide - 2.4 Local File System and FTP Access
Restart your LoPy. Watch the console as the LoPy comes back up to make sure there are no errors.
Let's send something!
In the Pymakr console try this:
That should send the string 'test' to The Things Network.
Your device on console.thethingsnetwork.org should now look like this:
Notice the 'Frames up' now equals 1. We have send a single frame. If you click 'Data' you can see the hex representation of the messages you send.
You can copy and paste that same code snippet into anything that requires a connection through your single channel gateway. The socket that is created can be used to send and receive i.e. s.send('test') or s.recv(64).
The code which does the channel setup is:
#Setup the single channel for connection to the gateway for channel in range(0, 72): lora.remove_channel(channel) for chan in range(0, 8): lora.add_channel(chan, frequency=freq, dr_min=0, dr_max=3)
Without that, the LoPy attempts to use any of the 72 available LoRa channels in the USA. That means your message doesn't have good odds of selecting the one channel your gateway is on.