Skip to content

3. Understanding LoRaWan

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

Configuration

A number of features can be configured or disabled by editing the config.h file in the library folder. Unfortunately, the Arduino environment does not offer any way to do this (compile-time) configuration from the sketch, so be careful to recheck your configuration when you switch between sketches or update the library.

Config.h file

The config.h file is responsible for storing various configuration settings for the LoRaWAN library. In this repository, you can reach the file in the src directory. On Windows, it will look something like this:

C:\Users\[Username]\Documents\Arduino\libraries\BeelanLoRaWAN\src\arduino-rfm

Use a text editor of your choice to open the config.h file.

You will find a section where you can define the LoRaWAN region by uncommenting the corresponding line. For example, if you want to use the US915 frequency band, remove the double slashes // before #define US_915. Similarly, if you want to use the AU915 frequency band, uncomment #define AU_915.

Depending on your specific requirements or regulatory considerations, you may need to select a specific subband within the chosen LoRaWAN region. In the config.h file, you will find multiple lines starting with #define SUBND_X, where X represents a subband number. Uncomment the line corresponding to the desired subband by removing the double slashes // before it.

After defining the LoRaWAN region and (optional) subband, save the changes.

To apply the changes to your LoRaWAN device, recompile your code and upload it.

By following these steps, you will be able to configure the LoRaWAN frequency by modifying the config.h file according to your desired region and subband. Remember the importance of selecting the appropriate region and subband based on the intended deployment location and compliance with regulatory requirements.

Connections

To make this library work, your board should be connected to the transceiver. The exact connections are a bit dependent on the transceiver board used, so this section tries to explain what each connection is for and in what cases it is (not) required.

Pin mapping

Most connections can use arbitrary I/O pins on the Arduino side. To tell the library about these, a pin mapping struct is used in the sketch file.

For example, this could look like this:

RFM_pins RFM_pins = {
  	.CS = SS,
  	.RST = RFM_RST,
  	.DIO0 = RFM_DIO0,
  	.DIO1 = RFM_DIO1,
  	.DIO2 = RFM_DIO2,
  	.DIO5 = RFM_DIO5,
}; 

Basic Configuration (Pin Mapping, using an Arduino UNO)

RFM_pins RFM_pins = {
  	.CS = 10,
  	.RST = 9,
  	.DIO0 = 2,
  	.DIO1 = 3,
  	.DIO2 = 4,
  	.DIO5 = -1,
};

Set Channel

You can set channel allowed in your region (AS_923, EU_868 or US915).

  • For US_915 the channels can be [0 - 7]
  • Use MULTI if you want random channel

Syntax

void setChannel(unsigned char channel);

Example

  void setup() {
  // Setup loraid access
  if(!lora.init()){
  Serial.println("RFM95 not detected");
  while(1);
  }
  ...

  // Set random Channel
 lora.setChannel(MULTI);
 }

Set Data Rate

You can set data rate allowed in your region (AS_923, EU_868 or US915).

For AU915

data_rate Name Config Direction
0 DR0 SF12 BW 125 KHz Uplink
1 DR1 SF11 BW 125 KHz Uplink
2 DR2 SF10 BW 125 KHz Uplink
3 DR3 SF9 BW 125 KHz Uplink
4 DR4 SF8 BW 500 KHz Uplink
5 DR5 SF7 BW 500 KHz Uplink
6 DR6 SF8 BW 500 KHz Uplink
7 RFU N/A N/A
8 DR8 SF12 BW 500 KHz Downlink
9 DR9 SF11 BW 500 KHz Downlink
10 DR10 SF10 BW 500 KHz Downlink
11 DR11 SF9 BW 500 KHz Downlink
12 DR12 SF8 BW 500 KHz Downlink
13 DR13 SF7 BW 500 KHz Downlink
14 RFU N/A N/A
15 Defined in LoRaWAN N/A

For AU915, is important to remark that DR0-DR5 are only for UPLINKS and DR8-DR13 are only for DOWNLINKS

For AS923 or EU868

data_rate Name Config Direction
0 DR0 SF12 BW 125 KHz Uplink/Downlink
1 DR1 SF11 BW 125 KHz Uplink/Downlink
2 DR2 SF10 BW 125 KHz Uplink/Downlink
3 DR3 SF9 BW 125 KHz Uplink/Downlink
4 DR4 SF8 BW 125 KHz Uplink/Downlink
5 DR5 SF7 BW 125 KHz Uplink/Downlink
6 DR6 SF7 BW 250 KHz Uplink/Downlink

For US915

data_rate Name Config Direction
0 DR0 SF10 BW 125 KHz Uplink
1 DR1 SF9 BW 125 KHz Uplink
2 DR2 SF8 BW 125 KHz Uplink
3 DR3 SF7 BW 125 KHz Uplink
4 DR4 SF8 BW 500 KHz Uplink
5:7 RFU N/A N/A
8 DR8 SF12 BW 500 KHz Downlink
9 DR9 SF11 BW 500 KHz Downlink
10 DR10 SF10 BW 500 KHz Downlink
11 DR11 SF9 BW 500 KHz Downlink
12 DR12 SF8 BW 500 KHz Downlink
13 DR13 SF7 BW 500 KHz Downlink

For US915, is important to remark that DR0-DR4 are only for UPLINKS and DR8-DR10 are only for DOWNLINKS

RFU: Reserved for future use

Send data to LoRaWAN

You need to specify the length of data you want to send and also the message type (unconfirmed or confirmed message). Set confirm = 0 to send unconfirmed message and confirm = 1 to send confirmed message.

Syntax

void LoRaWANClass::sendUplink(char *data, unsigned int len, unsigned char confirm, unsigned char mport)

Example

void loop() {
  // put your main code here, to run repeatedly:
  char myStr[] = "Ini data LoRaku";  

  lora.sendUplink(myStr, strlen(myStr), 0, 1);
  ...

} 

Update and run LoRa FSM

Update and run the LoRa Finite State Machine (FSM). This line should be put inside the Arduino loop block.

Syntax

void update(void);

Example

void loop() {
  ...

  // Check Lora RX
  lora.update();
}

In this version, we'll try to use interrupts in order to eliminate this FSM.

Check and retrieve incoming data

Check for the latest incoming data from server, either in binary or string format. You need to provide a char buffer to read the data.

Syntax

void readData(void);

Example


char buffer_rx[255];

void setup() {
  ...
}

void loop() {
  int recvStatus;
  ...

  // LoRa FSM
  lora.update();

  // Check data
  recvStatus = lora.readData(buffer_rx);
  if(recvStatus) {
    Serial.println(buffer_rx);
  }
}