Development

» LoRaWAN 01c: Configure module JP/KR

This example shows how to configure the module and all general settings related to back-end registration process.

Required Materials

1 x Waspmote
1 x Battery
1 x LoRaWAN JP/KR module
1 x 900 MHz Antenna

Code

/*
    ------ LoRaWAN Code Example --------

    Explanation: This example shows how to configure the module
    and all general settings related to back-end registration
    process.

    Copyright (C) 2019 Libelium Comunicaciones Distribuidas S.L.
    http://www.libelium.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Version:           3.0
    Design:            David Gascon
    Implementation:    Luismi Marti
*/

#include <WaspLoRaWAN.h>

//////////////////////////////////////////////
uint8_t socket = SOCKET0;
//////////////////////////////////////////////

// Device parameters for Back-End registration
////////////////////////////////////////////////////////////
char DEVICE_ADDR[] = "05060708";
char NWK_SESSION_KEY[] = "01020304050607080910111213141516";
char APP_SESSION_KEY[] = "000102030405060708090A0B0C0D0E0F";
char APP_KEY[] = "000102030405060708090A0B0C0D0E0F";
////////////////////////////////////////////////////////////

// variable
uint8_t error;



void setup()
{
  USB.ON();
  USB.println(F("LoRaWAN example - Module configuration"));


  USB.println(F(" _____________________________________________________"));
  USB.println(F("|                                                     |"));
  USB.println(F("| It is not mandatory to configure channel parameters.|"));
  USB.println(F("| Server should configure the module during the       |"));
  USB.println(F("| Over The Air Activation process.                    |"));
  USB.println(F("|_____________________________________________________|"));
  USB.println();

  //////////////////////////////////////////////
  // 1. switch on
  //////////////////////////////////////////////

  error = LoRaWAN.ON(socket);

  // Check status
  if ( error == 0 )
  {
    USB.println(F("1. Switch ON OK"));
  }
  else
  {
    USB.print(F("1. Switch ON error = "));
    USB.println(error, DEC);
  }

  if (LoRaWAN._version != ABZ_MODULE)
  {
    USB.println(F("\n---------------------------------------------------------------"));
    USB.println(F("Module version error"));
    USB.println(F("This is not a JP/KR LoRaWAN module"));
    USB.println(F("Please open the example 01a or 01b related to your module"));
    USB.println(F("\n---------------------------------------------------------------"));
    while (1);
  }


  //////////////////////////////////////////////
  // 2. Reset to factory default values
  //////////////////////////////////////////////

  error = LoRaWAN.factoryReset();

  // Check status
  if ( error == 0 )
  {
    USB.println(F("2. Reset to factory default values OK"));
  }
  else
  {
    USB.print(F("2. Reset to factory error = "));
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 3. Get Device EUI
  // Device EUI is hard coded in production line. End customer
  // should NOT modify device EUI. Device EUI will be not be
  // restored in factoryReset() function
  //////////////////////////////////////////////

  // Get Device EUI
  error = LoRaWAN.getDeviceEUI();

  // Check status
  if ( error == 0 )
  {
    USB.print(F("3.1. Get Device EUI OK. "));
    USB.print(F("Device EUI: "));
    USB.println(LoRaWAN._devEUI);
  }
  else
  {
    USB.print(F("3.1. Get Device EUI error = "));
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 4. Set/Get Device Address
  //////////////////////////////////////////////

  // Set Device Address
  error = LoRaWAN.setDeviceAddr(DEVICE_ADDR);

  // Check status
  if ( error == 0 )
  {
    USB.println(F("4.1. Set Device address OK"));
  }
  else
  {
    USB.print(F("4.1. Set Device address error = "));
    USB.println(error, DEC);
  }

  // Get Device Address
  error = LoRaWAN.getDeviceAddr();

  // Check status
  if ( error == 0 )
  {
    USB.print(F("4.2. Get Device address OK. "));
    USB.print(F("Device address: "));
    USB.println(LoRaWAN._devAddr);
  }
  else
  {
    USB.print(F("4.2. Get Device address error = "));
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 5. Set Network Session Key
  //////////////////////////////////////////////

  error = LoRaWAN.setNwkSessionKey(NWK_SESSION_KEY);

  // Check status
  if ( error == 0 )
  {
    USB.println(F("5. Set Network Session Key OK"));
  }
  else
  {
    USB.print(F("5. Set Network Session Key error = "));
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 6. Set Application Session Key
  //////////////////////////////////////////////

  error = LoRaWAN.setAppSessionKey(APP_SESSION_KEY);

  // Check status
  if ( error == 0 )
  {
    USB.println(F("6. Set Application Session Key OK"));
  }
  else
  {
    USB.print(F("6. Set Application Session Key error = "));
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 7. Set retransmissions for uplink confirmed packet
  //////////////////////////////////////////////

  // set retries
  error = LoRaWAN.setRetries(7);

  // Check status
  if ( error == 0 )
  {
    USB.println(F("7.1. Set Retransmissions for uplink confirmed packet OK"));
  }
  else
  {
    USB.print(F("7.1. Set Retransmissions for uplink confirmed packet error = "));
    USB.println(error, DEC);
  }

  // Get retries
  error = LoRaWAN.getRetries();

  // Check status
  if ( error == 0 )
  {
    USB.print(F("7.2. Get Retransmissions for uplink confirmed packet OK. "));
    USB.print(F("TX retries: "));
    USB.println(LoRaWAN._retries, DEC);
  }
  else
  {
    USB.print(F("7.2. Get Retransmissions for uplink confirmed packet error = "));
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 8. Set application key
  //////////////////////////////////////////////

  error = LoRaWAN.setAppKey(APP_KEY);

  // Check status
  if ( error == 0 )
  {
    USB.println(F("8. Application key set OK"));
  }
  else
  {
    USB.print(F("8. Application key set error = "));
    USB.println(error, DEC);
  }


  ////////////////////////////////////////////////////////
  //  ___________________________________________________________
  // |                                                           |
  // |  It is mandatory to configure minimum channel parameters. |
  // |  Server should configure the rest of the channel after    |
  // |  the Over The Air Activation process.                     |
  // |___________________________________________________________|
  //
  ////////////////////////////////////////////////////////

  //////////////////////////////////////////////
  // 9. Configure region and minimum channels
  //////////////////////////////////////////////

  // JAPAN = BAND_JP923 / KOREA = BAND_KR920
  uint8_t band = BAND_JP923;

  // Set band
  error = LoRaWAN.setBand(band);

  // Check status
  if ( error == 0 )
  {
    USB.println(F("9.1. Set band OK"));
  }
  else
  {
    USB.print(F("9.1. Set band error = "));
    USB.println(error, DEC);
  }

  // Get band
  error = LoRaWAN.getBand();

  // Check status
  if ( error == 0 )
  {
    USB.print(F("9.2. Get band OK. "));
    USB.print(F("Band: "));
    USB.print(LoRaWAN._bandABZ, DEC);
  }
  else
  {
    USB.print(F("9.2. Get band error = "));
    USB.println(error, DEC);
  }

  switch (LoRaWAN._bandABZ)
  {
    case BAND_JP923:
      USB.println(F(" - Region is AS923 JAPAN"));

      error = LoRaWAN.setChannelFreq(0, 923200000);
      if ( error == 0 )
      {
        USB.print(F("9.3. Set channel frequency OK. "));
        USB.print(F("Frequency in channel 0: "));
        USB.println(LoRaWAN._freq[0]);
      }
      else
      {
        USB.print(F("9.3. Set channel frequency error = "));
        USB.println(error, DEC);
      }

      error = LoRaWAN.setChannelFreq(1, 923400000);
      if ( error == 0 )
      {
        USB.print(F("9.3. Set channel frequency OK. "));
        USB.print(F("Frequency in channel 1: "));
        USB.println(LoRaWAN._freq[1]);
      }
      else
      {
        USB.print(F("9.3. Set channel frequency error = "));
        USB.println(error, DEC);
      }

      break;

    case BAND_KR920:
      USB.println(F(" - Region is KR920"));

      error = LoRaWAN.setChannelFreq(0, 922100000);
      if ( error == 0 )
      {
        USB.print(F("9.3. Set channel frequency OK. "));
        USB.print(F("Frequency in channel 0: "));
        USB.println(LoRaWAN._freq[0]);
      }
      else
      {
        USB.print(F("9.3. Set channel frequency error = "));
        USB.println(error, DEC);
      }

      error = LoRaWAN.setChannelFreq(1, 922300000);
      if ( error == 0 )
      {
        USB.print(F("9.3. Set channel frequency OK. "));
        USB.print(F("Frequency in channel 1: "));
        USB.println(LoRaWAN._freq[1]);
      }
      else
      {
        USB.print(F("9.3. Set channel frequency error = "));
        USB.println(error, DEC);
      }

      error = LoRaWAN.setChannelFreq(2, 922500000);
      if ( error == 0 )
      {
        USB.print(F("9.3. Set channel frequency OK. "));
        USB.print(F("Frequency in channel 2: "));
        USB.println(LoRaWAN._freq[2]);
      }
      else
      {
        USB.print(F("9.3. Set channel frequency error = "));
        USB.println(error, DEC);
      }
      break;

  }


  //////////////////////////////////////////////
  // 10. Set Adaptive Data Rate (recommended)
  //////////////////////////////////////////////

  // set ADR
  error = LoRaWAN.setADR("on");

  // Check status
  if ( error == 0 )
  {
    USB.println(F("10.1. Set Adaptive data rate status to on OK"));
  }
  else
  {
    USB.print(F("10.1. Set Adaptive data rate status to on error = "));
    USB.println(error, DEC);
  }

  // Get ADR
  error = LoRaWAN.getADR();

  // Check status
  if ( error == 0 )
  {
    USB.print(F("10.2. Get Adaptive data rate status OK. "));
    USB.print(F("Adaptive data rate status: "));
    if (LoRaWAN._adr == true)
    {
      USB.println("on");
    }
    else
    {
      USB.println("off");
    }
  }
  else
  {
    USB.print(F("10.2. Get Adaptive data rate status error = "));
    USB.println(error, DEC);
  }

  //////////////////////////////////////////////
  // 11. Set data format of messages
  //////////////////////////////////////////////

  // set data format
  // 0 = text
  // 1 = hex
  error = LoRaWAN.setDataFormat(1);

  // Check status
  if ( error == 0 )
  {
    USB.println(F("11.1. Set data format OK"));
  }
  else
  {
    USB.print(F("11.1. Set data format error = "));
    USB.println(error, DEC);
  }

  // Get data format
  error = LoRaWAN.getDataFormat();

  // Check status
  if ( error == 0 )
  {
    USB.print(F("11.2. Get data format OK. "));
    USB.print(F("Data format: "));
    if (LoRaWAN._dFormat == 0)
    {
      USB.println("Text");
    }
    else
    {
      USB.println("Hex");
    }
  }
  else
  {
    USB.print(F("11.2. Get data format error = "));
    USB.println(error, DEC);
  }


  USB.println(F("------------------------------------"));
  USB.println(F("Now the LoRaWAN module is ready for"));
  USB.println(F("joining networks and send messages."));
  USB.println(F("Please check the next examples..."));
  USB.println(F("------------------------------------\n"));

}


void loop()
{
  // do nothing
}

Output

J#
LoRaWAN example - Module configuration
_____________________________________________________
| |
| It is not mandatory to configure channel parameters.|
| Server should configure the module during the |
| Over The Air Activation process. |
|_____________________________________________________|

1. Switch ON OK
2. Reset to factory default values OK
3.1. Get Device EUI OK. Device EUI: 303931356F387B02
4.1. Set Device address OK
4.2. Get Device address OK. Device address: 05060708
5. Set Network Session Key OK
6. Set Application Session Key OK
7.1. Set Retransmissions for uplink confirmed packet OK
7.2. Get Retransmissions for uplink confirmed packet OK. TX retries: 7
8. Application key set OK
9.1. Set band OK
9.2. Get band OK. Band: 0 - Region is AS923 JAPAN
9.3. Set channel frequency OK. Frequency in channel 0: 923200000
9.3. Set channel frequency OK. Frequency in channel 1: 923400000
10.1. Set Adaptive data rate status to on OK
10.2. Get Adaptive data rate status OK. Adaptive data rate status: on
11.1. Set data format OK
11.2. Get data format OK. Data format: Hex
------------------------------------
Now the LoRaWAN module is ready for
joining networks and send messages.
Please check the next examples...
------------------------------------

Quick Publish: