Connect to TTN

From Wiki for Dragino Project
Jump to: navigation, search

Introduction

This page describes how to use Dragino devices to connect to TheThingsNetwork(TTN) LoRaWAN IoT Server. Other LoRaWAN IoT Server should have similiar set up procedure as this.

TheThingsNetwork is a LoRaWAN-enable IoT Network.The introductions for the network structure can be found on this link: Learn TTN.

The most simple network structure is as below. And we will show how to connect the devices to TTN network in this page.

TTN Network Structure with Dragino Products



Create TTN account

  • Go to TTN and set up a TTN according.
  • After Login, Choose Console to enter the TTN console page.



The different modes of LG01 configuration

WAN Port

Connect the cable
The settings



Wifi Client

Connect the cable
The settings



Create Gateway

A LoRa Gateway is used to

  1. Receive the packets from LoRa Devices and forward it to the TTN network.
  2. Receive message from TTN and Send it to LoRa Devices.

We can use different products to act as a gateway, for example: LG01 / LoRa GPS HAT + RPI. Please be aware that the LG01 and LoRa GPS HAT is a single channel gateway and they have limitation to use in a LoRaWAN network.


Warning: Below instruction will show the use of Single Channel Gateway to connect to TTN server, a single channel gateway is not full LoRaWAN compatible. more info please check this link.

Create Single Channel Gateway By LG01

Notice:

  1. Below instruction if for Activation by Personalization (ABP) and Legacy Packet Forward connection.
  2. Make Sure the LG01 has firmware 4.3.2 or higher. Download Link.


Create a Gateway in TTN Console

User need to create a gateway in TTN Console, please make sure to use legacy packet forward method. for the gateway ID, user can use any 8 Bytes Hex ID. For unique purpose. We recommend user can get the MAC address from the bottom of the LG01, add a suffix of it as the gateway ID. For example: as below, the mac for the device is a8:40:41:17:c6:92, use then can add ffff suffix and get the gateway ID: a8404117c692ffff.

Find Gateway ID
In TTN Console. Register a Gateway
In TTN Console. Create a Gateway




Upload Arduino Sketch to LG01 Linux System

  • Go to this link and download hex file single_pkt_fwd_v003.hex
  • In LG01, Go to page Sensor --> Flash MCU , Select the single_pkt_fwd_v003.hex file and upload it to LG01.
  • Reboot LG01, After reboot, check page Sensor --> MicroController , See if the MCU versoion option shows the correct version, if yes, it means you have upload the code correctly.
Check MCU Sketch Version

notice:

  • We keep improving the sketch, so user is not limited to use single_pkt_fwd_v002.hex, but can use v003,v004 etc in future. We will keep the latest in the same link as above
  • The sketch source code can be found at single_pkt_fwd source code.



Configure LG01 LoRa Radio Parameter

In LG01 Sensor --> LoRaWAN , input the correct LoRa Radio Settings. These settings are used to receive the LoRa Radio info from LoRa nodes.

Below is a setting example for EU-868 LoRaWAN 868.1Mhz .

The LoRa RX frequency use one of the frequencies being TX by the LoRa End Device.

Configure LoRa Radio
Configure LoRa Radio



Configure LoRaWAN Server

In LG01 Sensor --> LoRaWAN , input the TTN Server Address, Port and Gateway ID.

Below is an example to connect to the TTN Europe Router.

Configure TTN info in LG01

Then, Enable to use LoRaWAN server;

Enable to use LoRaWAN



Check Test Result--Uplink

Check Gateway Update
Check if LG01 got packets from LoRa Node. In page Sensor --> Sensor Data
Check if LoRa Data arrive TTN



Check Test Result--Downlink

Downlink Frequency
downlink flow and result



Create Single Channel Gateway By LoRa/GPS HAT with Raspbian

Step 1: Prepare a Raspberry Pi with Raspbian OS, Set up it with Internet Access

Step 2: Enable SPI support

Enter into the RPi Console and run
pi@raspberrypi:~$sudo raspi-config
go to advance option to ensure that the SPI can be used for RPI. 

Step 3: Install wiringpi to enable GPIO access

pi@raspberrypi:~$ sudo apt-get install wiringpi
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  wiringpi
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 43.9 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian/ jessie/main wiringpi armhf 2.32 [43.9 kB]
Fetched 43.9 kB in 13s (3,207 B/s)                                                                                                                                    
Selecting previously unselected package wiringpi.
(Reading database ... 31216 files and directories currently installed.)
Preparing to unpack .../wiringpi_2.32_armhf.deb ...
Unpacking wiringpi (2.32) ...
Setting up wiringpi (2.32) ...
Processing triggers for libc-bin (2.19-18+deb8u6) ...

Step 4: Download Single Channel Gateway.

pi@raspberrypi:~$ wget https://github.com/tftelkamp/single_chan_pkt_fwd/archive/master.zip
pi@raspberrypi:~$ unzip master.zip
pi@raspberrypi:~$ cd single_chan_pkt_fwd-master/

Step 5: Replace the Server Address to TTN Server IP.

The line #define SERVER1 "54.72.145.119" defines the server IP , Change this to #define SERVER1 "13.76.168.68", which point to router.au.thethings.network

Step 6: Run Packet Forward and Get Gateway ID

pi@raspberrypi:~$ make
pi@raspberrypi:~/single_chan_pkt_fwd-master$ sudo ./single_chan_pkt_fwd 
SX1276 detected, starting.
Gateway ID: b8:27:eb:ff:ff:49:62:bc"
Listening at SF7 on 868.100000 Mhz.
------------------
stat update: {"stat":{"time":"2017-05-13 15:54:24 GMT","lati":0.00000,"long":0.00000,"alti":0,"rxnb":0,"rxok":0,"rxfw":0,"ackr":0.0,"dwnb":0,"txnb":0,"pfrm":"Single Channel Gateway","mail":"","desc":""}}

From above, we can see the gateway ID is b8:27:eb:ff:ff:49:62:bc. then we can use this ID to register a gateway in the TTN Console.

Step 7: Register Gatewat in TTN Console

Register a Gateway In TTN
Result shows running status in RPi




Create LoRa end devices

Use LoRa Shield and Arduino as LoRa End Device

Step 1: Register a device in TTN Console

In TTN Console--> Add Application
In TTN Console--> Application page, register a device
In TTN Console--> Application page, register a device--2
In TTN Console--> Application page, Get the Device Address/ Network Key and App Key
In TTN Console--> Application page, make sure it is ABP mode

Step 2: Upload Sketch to LoRa End Devices (LoRa Shield + UNO) Upload the Sketch from [1] to LoRa Shield.

Make sure:

  • Install the requirement library as specify in the Sketch.
  • Change the Device Address/ Network Key and App Key according to the exactly key from TTN Console.

(When you download the development board select Arduino Uno, port select com.)

Step 3: Open Serial Monitor and see result

In Arduino IDE--> Serial Monitor



Use LoRa GPS Shield and Arduino as LoRa End Device

Step 1: Register a device in TTN Console

In TTN Console--> Add Application
In TTN Console--> Application page, register a device
In TTN Console--> Application page, Get the Device Address/ Network Key and App Key
In TTN Console--> Application page, make sure it is ABP mode

Step 2: Hardware preparation and concatenation Connect the LoRa/GPS Shield to UNO, Remove the GPS_RXD and GPS_TXD jumper and connect the GPS with the arduino as below:

LoRa GPS Shield hardware connection


Step 3: Upload Sketch to LoRa End Devices (LoRa GPS + UNO)

In Arduino IDE-->Replace the value what you registered
In Arduino IDE-->Select UNO Board and USB port


Step 4: Open Serial Monitor and see result

In Arduino IDE-->Select UNO Board and USB port



Use LoRa GPS HAT and RPi 3 as LoRa End Device

Step 1: Register a device in TTN Console

In TTN Console--> Add Application
In TTN Console--> Application page, register a device
In TTN Console--> Application page, register a device--2
In TTN Console--> Application page, Get the Device Address/ Network Key and App Key
In TTN Console--> Application page, make sure it is ABP mode

Step 2: Prepare a Raspberry Pi with Raspbian OS, Set up it with Internet Access

Step 3: Enable SPI support

Enter into the RPi Console and run
pi@raspberrypi:~$sudo raspi-config
go to advance option to ensure that the SPI can be used for RPI. 

Step 4: Install wiringpi to enable GPIO access

pi@raspberrypi:~$ sudo apt-get install wiringpi
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  wiringpi
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 43.9 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian/ jessie/main wiringpi armhf 2.32 [43.9 kB]
Fetched 43.9 kB in 13s (3,207 B/s)                                                                                                                                    
Selecting previously unselected package wiringpi.
(Reading database ... 31216 files and directories currently installed.)
Preparing to unpack .../wiringpi_2.32_armhf.deb ...
Unpacking wiringpi (2.32) ...
Setting up wiringpi (2.32) ...
Processing triggers for libc-bin (2.19-18+deb8u6) ...

Step 5: Download LMIC Pi code

pi@raspberrypi:~$ wget https://github.com/dragino/lmic_pi/archive/master.zip
pi@raspberrypi:~$ unzip master.zip
pi@raspberrypi:~$ cd lmic_pi-master/examples/thethingsnetwork-send-v1

Step 6: Change the Device Address/ Network Key and App Key according to the exactly key from TTN Console.

Step 7: Compile and Run

pi@raspberrypi:~/lmic_pi-master/examples/thethingsnetwork-send-v1$ make pi@raspberrypi:~/lmic_pi-master/examples/thethingsnetwork-send-v1$ sudo ./thethingsnetwork-send-v1

Result for Sending out packets

pi@raspberrypi:~/lmic_pi-master/examples/thethingsnetwork-send-v1$ sudo ./thethingsnetwork-send-v1 
[4360] (1495508318) Tue May 23 02:58:38 2017
 
OhOh. Unknown interrupt flags for FSK
Event EV_TXCOMPLETE, time: 5
[65dfe] (1495508338) Tue May 23 02:58:58 2017
 
Event EV_TXCOMPLETE, time: 26
[c7889] (1495508358) Tue May 23 02:59:18 2017
 
Event EV_TXCOMPLETE, time: 46
[129321] (1495508378) Tue May 23 02:59:38 2017



Check Result

Use LoRa Shield and Arduino as LoRa End Device

Below shows the result what we should see in the TTN network and local debug

Result shows LG01 gets packets from LoRa end device
Result shows Gateway Traffic in TTN Network
Result shows Devices Update Data in TTN Network

Use LoRa GPS Shield and Arduino as LoRa End Device

Serial port monitor results:

In Arduino IDE-->Select UNO Board and USB port
TTN Gateways-->Traffic
Result shows Devices Update Data in TTN Network



Downlink

Use LoRa Shield and Arduino as LoRa End Device



FAQ

How To Set up only one channel in the node sides

For the Arduino Node use LMIC, user can use the function: LMIC_disableChannel(1); to disable unwanted Channels.

for example as below in the Arduino Sketch:

  // TTN uses SF9 for its RX2 window.
    LMIC.dn2Dr = DR_SF9;
 
    LMIC_disableChannel(1);    //disable channel 1 
    LMIC_disableChannel(2);  // disable channel 2 
 
    // Set data rate and transmit power (note: txpow seems to be ignored by the library)
    LMIC_setDrTxpow(DR_SF7,14);
 
    // Start job
    do_send(&sendjob);

For 915Mhz, there will be 64 channels to be used, so use below method will be good:

    LMIC_setDrTxpow(DR_SF7,14);
    for (int i=1;i<64;i++)
    {
      LMIC_disableChannel(i);  // only the first channel 902.3Mhz works now. 
    }


Always use Serial Monitor to see what frequency the LoRa is using.



Where to find the server address of TTN

Please check this link: TTN Server Address

Please note that in Raspberry Pi example use IP address, so you need to ping the server first to get IP address.

How To Set up to use 915Mhz frequency

in the Arduino Library --> src --> lmic --> Config.h, change

#define CFG_eu868 1
//#define CFG_us915 1

to

//#define CFG_eu868 1
#define CFG_us915 1

Recompile and upload the LMIC sketch for Arduino. After upload and open the serial monitor, User will see increase frequency from like 9xx.1 , 9xx.3,9xx.5 etc for every packet send out. User can set up the Gateway to use one of these frequency, when the LoRa node send out a packet of the same frequency, The gateway will get it and show in the serial monitor.



What should i check if the LG01 Example doesn't work?

Check List1: Whether the TTN Console page shows LG01 connected

If not, please check:

  • If you have uploaded the correct hex file for LG01 and shows the sketch version in the sketch.
  • If you have chosen the packet forward as the connect method in the TTN gateway page. If yes, the gateway ID shown in TTN will have a prefix eui-
  • Make sure your LG01 is connected to Internet. You can enter the SSH console of LG01 and ping an Internet IP address.

Check List2: Whether the LG01 has got the LoRa package

This can be checked in the web UI Sensor--> Sensor Data. If not get sensor data, please check if the frequency match for both LG01 and LoRa Node, they should have the same frequency , spread factory and code rate.

If the LG01 already got the sensor data but TTN doesn;t show it. please check the Device Addr, EUI, related settings in the node.

How To add other frequency band such as EU433

Now the Arduino LMIC library we use here only support EU868 and US915 band. If user wants to add support to 433Mhz Band. User can modify the frequency in the EU868 settings to test in the EU433 ABP mode. Below is the instruction,

Notice: Below modification is only for ABP mode

in the Library: src --> lmic --> lorabase.h , we can change the frequency from:

// Default frequency plan for EU 868MHz ISM band
// Bands:
//  g1 :   1%  14dBm
//  g2 : 0.1%  14dBm
//  g3 :  10%  27dBm
//                 freq             band     datarates
enum { EU868_F1 = 868100000,      // g1   SF7-12
       EU868_F2 = 868300000,      // g1   SF7-12 FSK SF7/250
       EU868_F3 = 868500000,      // g1   SF7-12
       EU868_F4 = 868850000,      // g2   SF7-12
       EU868_F5 = 869050000,      // g2   SF7-12
       EU868_F6 = 869525000,      // g3   SF7-12
       EU868_J4 = 864100000,      // g2   SF7-12  used during join
       EU868_J5 = 864300000,      // g2   SF7-12   ditto
       EU868_J6 = 864500000,      // g2   SF7-12   ditto
};
enum { EU868_FREQ_MIN = 863000000,
       EU868_FREQ_MAX = 870000000 };

to:

// Default frequency plan for EU 868MHz ISM band
// Bands:
//  g1 :   1%  14dBm
//  g2 : 0.1%  14dBm
//  g3 :  10%  27dBm
//                 freq             band     datarates
/*enum { EU868_F1 = 868100000,      // g1   SF7-12
       EU868_F2 = 868300000,      // g1   SF7-12 FSK SF7/250
       EU868_F3 = 868500000,      // g1   SF7-12
       EU868_F4 = 868850000,      // g2   SF7-12
       EU868_F5 = 869050000,      // g2   SF7-12
       EU868_F6 = 869525000,      // g3   SF7-12
       EU868_J4 = 864100000,      // g2   SF7-12  used during join
       EU868_J5 = 864300000,      // g2   SF7-12   ditto
       EU868_J6 = 864500000,      // g2   SF7-12   ditto
};
enum { EU868_FREQ_MIN = 863000000,
       EU868_FREQ_MAX = 870000000 }; */
 
//Modify the EU868Mhz band configure for EU433Mhz temporary use.	   
enum { EU868_F1 = 433175000,      // g1   SF7-12
       EU868_F2 = 433375000,      // g1   SF7-12 FSK SF7/250
       EU868_F3 = 433575000,      // g1   SF7-12
       EU868_F4 = 433775000,      // g2   SF7-12
       EU868_F5 = 433975000,      // g2   SF7-12
       EU868_F6 = 434175000,      // g3   SF7-12
       EU868_J4 = 434375000,      // g2   SF7-12  used during join
       EU868_J5 = 434575000,      // g2   SF7-12   ditto
       EU868_J6 = 434775000,      // g2   SF7-12   ditto
 
};
enum { EU868_FREQ_MIN = 433050000,
       EU868_FREQ_MAX = 434900000 };