Supports LoRaWAN. Measure distance and water level with Dragino and The Things Network(TTN V3).
Tags: dragino, lorawan, iot, ttn
Published: 2022-05-30 05:29:25

## Table of Contents
- [Devices and Services Used](#1-devices-and-services-used)
- [Gateway Settings](#2-gateway-settings)
- [TTN settings](#3-ttn-settings)
- [Code changes and Communication frequency settings](#4-code-changes-and-communication-frequency-settings)

# 1. Devices and Services Used
This article shows you how to upload data to The Things Network V3 using Dragino's sensors and gateways.

- End Nodes : LDDS75-8(Dragino)
- Concentrator / Gateway : LIG16(Dragino)
- Network Server : TTN V3(The Things Network V3)

#2. Gateway Settings
## 2.1 Connect via Wifi
When you turn on the gateway,**"dragino-xxxxxx"** will be displayed in the **Wifi SSID**. Please connect to this.

The Wifi network password is "**dragino+dragino**".

## 2.2 Access to Management Screen
Enter "**http://10.130.1.1**" in your browser.

**User Name: root
Password: dragino**

### 2.2.1 LoRa Settings
Select **"LoRa" from "LoRa"** in the menu.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ifh5htkphsrxdb42dj1.png)

- Change the "**Frequency Plan**" to suit your country. I live in Japan, so I set it up for Japan.

Click "**Save & Apply**" when the settings are complete.

### 2.2.2 LoRaWAN Settings
Select **"LoRaWAN" from "LoRaWAN"** in the menu.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2sjq2btj28hoc4eyh90p.png)

- **Do not change** the "**Gateway ID**". This ID is used in the TTN settings.

- Select "**The Things Network V3**" for "Service Provider".
Then, for "Server Address", select "Cluster" of "TTN V3". I chose "**eu1.cloud.things.network**".

Click "**Save & Apply**" when the settings are complete.

### 2.2.3 WiFi Settings
Select **"WiFi" from "Network"** in the menu.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vx6k4g9ubyti1xfguwll.png)

- Enable "Enable WiFi WAN Client".
- Please enter the information of the Wifi to connect.
- Please use 2.4GHz band WiFi.

Click "**Save & Apply**" when the settings are complete.

# 3. TTN settings
## 3.1 Access to TTN
- Go to "**https://www.thethingsnetwork.org**".

- After registering for free, click "Start building"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8gz43eltl8ckj7rekchd.PNG)


- Select the Cluster set in the gateway. I chose "**Europe 1**".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eh2voknhvwcpjk1uho9q.PNG)


## 3.2 Gateway Registration

- Select "**Register a gateway**".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jwkb7x36e2i87z39flp.png)

- Set the "gateway ID" and "gateway EUI". Any "gateway ID" is OK.

- **For the "gateway EUI", enter the "gateway ID of the Dragino gateway".**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7j0y9ty58r5x1l9oopxj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2sjq2btj28hoc4eyh90p.png)

- Choose "**Frequency Plan**" that suits your country. I chose "Asia 920-923 MHz with LBT".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t94duf9xwws255pvu471.png)

- Click "**Create gateway**" when the settings are complete.
- After waiting for a while, the gateway will be connected.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tdxz60tflq83gd6voii.png)


## 3.3 Application Registration
- Select "**Go to applications**".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/343ecmw1of4s2nsnrewq.PNG)

- Click "add application"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hlf2jvinjcbjvdc3uzf.png)


- Any "Application ID" is OK.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewny2ifasts1wvrc3v4e.png)

- Click "add end device".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9impf48f77704v5uco8d.png)

- Enter the information for your end device.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njx4hmgqbtwhh2vncgtc.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdb95l09q1w2mxjfldqj.png)

- If the end device is turned on, communication will start.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bsbk3o7eat8rub07axf.png)

- If the data is not displayed, for example **"No Sensor"**, you need to change the code. If you want to change the code, please refer to "4.1".

# 4. Code changes and Communication frequency settings
## 4.1 Code changes

- If you are using LDDS75 and "**No Sensor**" is displayed, change the program as follows.
- If you look at this table, you can see that the values are stored in the 3rd and 4th.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egnf5zdnh4xx2y0gy69c.png)

- Select **"Payload formatters"** for the end device. 
- Then select **"Custom Javascript formatter" for "Formatter type"**. Now you can edit the code.
- Click **"Save changes"** and you're done.

- Please change it according to the type of end device to be used.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78w74y3quzoohxt42d0r.png)

```javascript
function decodeUplink(input) {
  var data = {};
  var len=input.bytes.length;
  var value=(input.bytes[0]<<8 | input.bytes[1]) & 0x3FFF;
  var batV=value/1000;//Battery,units:V
  var distance = 0;
  var interrupt = input.bytes[len-1]; 
  switch (input.fPort) {
    case 2:
  if(len==8)  
  {
    distance=input.bytes[2]<<8 | input.bytes[3];
    data.distance=distance;//distance,units:mm
   
   if(distance<=20)
    data.distance = "Invalid Reading";
  }
  else
   data.distance = "No Sensor";
  return {
       data:data,
  };

default:
    return {
      errors: ["unknown FPort"]
    }
}
}
```

- Here is the code I used.



## 4.2 Communication Frequency Settings
- If you want to change the **time interval** on a Dragino end device, do the following.

[How to Time Interval Setting](http://wiki.dragino.com/xwiki/bin/view/Main/How%20to%20set%20the%20transmit%20time%20interval/)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3xg2obcinp5mq8gnvo0.PNG)

How was that?
If you have any questions, please feel free to comment.