Skip to content

5. Examples

Eric Chavez edited this page Dec 20, 2023 · 1 revision

In the code section, you will find the examples that demonstrate how to utilize the library for two different activation modes: Over-The-Air Activation (OTAA) and Activation-By-Personalization (ABP). These examples serve as practical references for integrating Beelan-LoRaWAN with your IoT projects. By understanding the code functionality and flow, you can gain insights into implementing LoRaWAN communication with ease.

Both examples demonstrate the core functionalities of Beelan-LoRaWAN, including data transmission, reception, and device configuration. By reviewing the code and understanding the steps involved, you can adapt and extend the examples to suit your specific use cases. Remember to customize the examples with your own network credentials and sensor data formats to integrate Beelan-LoRaWAN effectively into your IoT applications.

It is essential to refer to the repository's documentation and consult the LoRaWAN specifications for a comprehensive understanding of the protocol and its security considerations.

ABP Example

Send Class A ABP example

This example demonstrate how to use the Beelan-LoRaWAN library for an ABP (Activation By Personalization) device.

Let's take a closer look at the key aspects of the ABP example code:

  1. Initialization and Configuration:
    • The setup section of the code begins by initializing the Beelan hardware and setting the LoRaWAN class and data rate. This ensures that the device operates in the desired mode and uses the appropriate data transmission settings.
    • The ABP-specific parameters, such as Device Address, Network Session Key, and Application Session Key, are defined in the code. These values are provided by the network operator or application server.

  1. Setting ABP Credentials:
    • The ABP parameters, including Device Address, Network Session Key, and Application Session Key, are set using the lora.setDevAddr(), lora.setNwkSKey(), and lora.setAppSKey() functions, respectively. These credentials uniquely identify the device and enable secure communication with the LoRaWAN network.

  1. Joining the Network:

    • In the ABP mode, there is no join procedure required. The device establishes an immediate connection with the network server using the pre-configured credentials.
    • The code does not contain a join request or join acceptance handling, since the device is already provisioned with the necessary security keys and device address.
  2. Data Transmission:

    • The code includes a loop that executes periodically, controlled by the interval variable. Inside the loop, the device constructs a payload message, which can be sensor data or any other relevant information.
    • The lora.sendUplink() function is used to send the payload to the network server. It takes the payload data, its length, a port number, and a confirmation flag as parameters.
    • After transmitting the data, the code increments a counter to keep track of the number of messages sent.

  1. Data Reception:
    • The device listens for downlink messages from the network server using the lora.readData() function. If a downlink message is received, it is stored in the outStr variable.
    • The received message can be processed or utilized as per the application requirements. In the example, the code simply prints the received message to the serial monitor using Serial.println().

  1. LoRaWAN Updates:
    • The lora.update() function is called to check for any updates or changes in the LoRaWAN network status. This function ensures that the device remains synchronized with the network and handles any necessary protocol operations.

OTAA Example

Send Class A OTAA

This example demonstrate how to use the Beelan-LoRaWAN library for an OTAA (Over-the-Air Activation) device.

Let's delve into the details of the OTAA example code:

  1. Initialization and Configuration:
    • The setup section of the code begins by initializing the Beelan hardware and setting the LoRaWAN class and data rate. This ensures that the device operates in the desired mode and uses the appropriate data transmission settings.
    • OTAA-specific parameters, including the Device EUI, Application EUI, and Application Key, are defined in the code. These parameters are unique identifiers and security keys provided by the network operator or application server.

  1. Setting OTAA Credentials:
    • The OTAA credentials, Device EUI, Application EUI, and Application Key, are set using the lora.setDevEUI(), lora.setAppEUI(), and lora.setAppKey() functions, respectively. These credentials are necessary for the device to authenticate and join the LoRaWAN network.

  1. Joining the Network:
    • The example demonstrates the join procedure, where the device sends a join request to the network server using the lora.join() function. This function initiates the OTAA join process and waits for a response from the network server.
    • The code includes a loop that continues the join process until the device successfully joins the network. It sends a join request at regular intervals and waits for a response.
    • Once the device successfully joins the network, the code proceeds to the main loop for data transmission and reception.

  1. Data Transmission:
    • Within the main loop, the code constructs a payload message, which can contain sensor data or any other relevant information.
    • The lora.sendUplink() function is used to send the payload to the network server. It takes the payload data, its length, a port number, and a confirmation flag as parameters.
    • After transmitting the data, the code increments a counter to keep track of the number of messages sent.

  1. Data Reception:
    • The device listens for downlink messages from the network server using the lora.readData() function. If a downlink message is received, it is stored in the outStr variable.
    • The received message can be processed or utilized as per the application requirements. In the example, the code simply prints the received message to the serial monitor using Serial.println().

  1. LoRaWAN Updates:
    • The lora.update() function is called to check for any updates or changes in the LoRaWAN network status. This function ensures that the device remains synchronized with the network and handles any necessary protocol operations.

Change class

The class can be changed by modifying the function lora.setDeviceClass();, by default the function is set for CLASS_A. To change it to CLASS_C, which represents Class C devices, you can modify the function lora.setDeviceClass(CLASS_A); in your code as:

lora.setDeviceClass(CLASS_C);

After making this change, recompile and upload the code to your device, and it will operate in the desired LoRaWAN class.

Remember that there are some differences and considerations between Class A and Class C devices in terms of power consumption, packages, and timing. Make sure to review the documentation of your LoRaWAN device and network server to understand the specific behavior and requirements of each class.