Introduction
LoRaWAN is a long-range, low-power wireless network specification (LPWAN) based on LoRa modulation, capable of achieving communication over distances of up to 20 km under favorable conditions. It is a Media Access Control (MAC) layer protocol used to connect various LoRa devices and provide authentication through standard protocol-based connections. For more information about LoRa and LoRaWAN, check out the blogs in Related Links.
When working with LoRaWAN, authentication and data encryption are required. Before you can communicate with devices, you’ll need to add an application and a gateway to a network and register devices on it. You’ll also need a network session key and an application session key to encrypt the data. There are two possible methods for obtaining these keys: activating the connection via OTAA or ABP. Below, we’ll show you how to work with both setups, including their advantages and disadvantages. At Industrial Shields, we provide some examples for working with industrial PLCs for automation purposes.
OTAA
What is OTAA?
Over-the-Air Activation (OTAA) is an authentication method where the network session key and application session key are generated with every new connection. When connecting using a join request, a device receives an AppEUI, a DevEUI, and the AppKey. These are used to generate the session keys.
In the OTAA method, keys are generated with each new connection, making it more secure because the keys are also deleted when the connection ends. Additionally, devices can easily switch to a new network, and the keys are less likely to be intercepted. However, the device can store the keys and decide when a join request is necessary.
ABP
What is ABP?
Activation by Personalization (ABP) is an authentication method where the network session key and application session key are preprogrammed into the device. Because the keys are preprogrammed, there’s no need for a join connection each time a new connection is required.
Preprogrammed keys reduce connection times since no join request is needed to connect to the server. However, the method cannot reuse the keys and must ensure they are unique. Additionally, keys can be more easily discovered, even before the connection is established.
How to Program It
To test LoRaWAN, we’ll work with The Things Network, using one of its gateways to establish the connection. Make sure you have an account and have preconfigured your application and gateway. Applications refer to whatever your devices are communicating with on the Internet.
In this basic example, we will send data to the server every 60 seconds through our gateway. This will help you understand LoRaWAN communication and how connections work.
Steps to Configure
- Download and install Arduino IDE for your Industrial Shields Boards.
- Select the M-Duino Family board and the model you are working with.
- We’ll use the M-Duino LoRa 21+. If not, configure it following our tutorial.
- Open examples from the MCCI LoRaWAN LMIC library and choose either
ttn-otaa
orttn-abp
, depending on the activation method you want to use. - Both configurations have similar code but with important differences.
- Know which one you’ll work with, as they require different keys.
- Differences are in the first lines of the code, where the definitions are declared.
- Check the final sections to see which parameters need to be changed for OTAA or ABP.
Example Code for OTAA
Pass the keys AppEUI, DevEUI, and AppKey obtained from The Things Network. Remember:
- AppEUI and DevEUI must be passed in little-endian format (least significant byte first).
- AppKey must be passed in big-endian format.
Replace the placeholder text FILLME
with your keys:
Example Code for ABP
Pass the keys DevAddr, NetwKey, and AppKey obtained from The Things Network. Remember:
- NetwKey and AppKey must be passed in big-endian format.
Replace the placeholder text FILLME
with your keys:
Final Notes
Use the provided examples and instructions to configure your Arduino PLC for LoRaWAN communication. Follow the specific pin mappings for your Industrial Shields PLC, and test the configurations thoroughly to ensure reliable communication.
sales@logicbus.com | support@logicbus.com | +1 619 616 7350 | Start conversation