Вы находитесь на странице: 1из 9

INTRODUCTION

Lately LoRaWAN has been increasingly popular in Internet of Things (IoT)


applications. LoRaWAN offers low power, low payload Internet connection without
using WIFI. It can run on a tiny coin cell battery for years. It is perfect for daily
applications that require remote connection and monitoring but does not require high
power or data bandwidth.

In LoRaWAN, LoRa end devices (also called LoRa nodes) gather useful data and
transmits them to a network server using LoRa protocol. Usually LoRa nodes are
embedded circuits with built-in LoRa technology with several sensors.

LoRa nodes are implemented in most low power wireless sensor network applications
such as irrigation systems, smart metering, smart cities, smartphone detection, building
automation, etc. There are a lot of industrial grade LoRa sensors available in the market,
but in this tutorial, we will teach you how to build your own LoRa node!

OBJECTIVE
In this tutorial, you will learn:

 How to build and configure a LoRa Node using Cytron LoRa-RFM Shield +
Arduino
 How to create an account in The Things Network
 How to add an application to your network
 How to register your LoRa Node to the network
 How to activate your LoRa Node

HARDWARE REQUIREMENT
1. Cytron LoRa-RFM Shield
2. CT-UNO or other Arduino-compatible boards

SOFTWARE REQUIREMENT
1. Arduino IDE

STEPS
BUILD LORA NODE FROM ZERO
1. The build is easy. Prepare CT-UNO (or other Arduino-compatible board).
2. Plug the Cytron LoRa-RFM shield onto CT-UNO.
3. Install the antenna.

That is all for the hardware setup. Next, we will setup its firmware yet. We are going to
use the famous Arduino IDE to build firmware for it.

ARDUINO IDE BASIC SETUPS FOR LORA NODE


1. Install latest Arduino IDE from the link provided above.
2. Open Arduino IDE. Now we are going to include necessarry libraries to build
firmware for our LoRa node. First of all, download this ZIP file. Then in Arduino
IDE, go to Sketch -> Include Library -> Add .ZIP library, select the downloaded
ZIP file then open.
3. Start a new sketch. Copy the code below and paste it into new sketch.
4. #include <lmic.h>
5. #include <hal/hal.h>
6. #include <SPI.h>
7.
8. // LoRaWAN NwkSKey, network session key
9. static const PROGMEM u1_t NWKSKEY[16] = { 0xAE, 0xBD, 0x9B, 0xBC,
0x43, 0x41, 0xFB, 0x47, 0x3D, 0xA5, 0x8D, 0x0D, 0x48, 0x98, 0xEF, 0xCF
};
10.
11. // LoRaWAN AppSKey, application session key
12. static const u1_t PROGMEM APPSKEY[16] = { 0xE1, 0x51, 0x0F, 0xB1,
0x59, 0x4C, 0xC8, 0xD7, 0xAA, 0x43, 0xAC, 0xE7, 0xE2, 0xBC, 0x5F, 0x1B
};
13.
14. // LoRaWAN end-device address (DevAddr)
15. static const u4_t DEVADDR = { 0x260314F7 };
16.
17. // These callbacks are only used in over-the-air activation, so they
are
18. // left empty here (we cannot leave them out completely unless
19. // DISABLE_JOIN is set in config.h, otherwise the linker will
complain).
20. void os_getArtEui (u1_t* buf) { }
21. void os_getDevEui (u1_t* buf) { }
22. void os_getDevKey (u1_t* buf) { }
23.
24. static uint8_t mydata[] = "HI,Lora World!";
25. static osjob_t sendjob;
26.
27. // Schedule data trasmission in every this many seconds (might become
longer due to duty
28. // cycle limitations).
29. // we set 10 seconds interval
30. const unsigned TX_INTERVAL = 10;
31.
32. // Pin mapping according to Cytron LoRa Shield RFM
33. const lmic_pinmap lmic_pins = {
34. .nss = 10,
35. .rxtx = LMIC_UNUSED_PIN,
36. .rst = 7,
37. .dio = {2, 5, 6},
38. };
39.
40. void onEvent (ev_t ev) {
41. Serial.print(os_getTime());
42. Serial.print(": ");
43. switch(ev) {
44. case EV_TXCOMPLETE:
45. Serial.println(F("EV_TXCOMPLETE (includes
waiting for RX windows)"));
46. // Schedule next transmission
47. os_setTimedCallback(&sendjob,
os_getTime()+sec2osticks(TX_INTERVAL), do_send);
48. break;
49. default:
50. Serial.println(F("Unknown event"));
51. break;
52. }
53. }
54.
55. void do_send(osjob_t* j){
56. // Check if there is not a current TX/RX job running
57. if (LMIC.opmode & OP_TXRXPEND) {
58. Serial.println(F("OP_TXRXPEND, not sending"));
59. } else {
60. // Prepare upstream data transmission at the next
possible time.
61. LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
62. Serial.println(F("Packet queued"));
63. }
64. // Next TX is scheduled after TX_COMPLETE event.
65. }
66.
67. void setup() {
68. Serial.begin(115200);
69. Serial.println(F("Starting"));
70.
71. // LMIC init
72. os_init();
73.
74. // Reset the MAC state. Session and pending data transfers
will be discarded.
75. LMIC_reset();
76.
77. // Set static session parameters. Instead of dynamically
establishing a session
78. // by joining the network, precomputed session parameters are
be provided.
79. uint8_t appskey[sizeof(APPSKEY)];
80. uint8_t nwkskey[sizeof(NWKSKEY)];
81. memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
82. memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
83. LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
84.
85. // Select frequencies range
86. LMIC_selectSubBand(3);
87.
88. // Disable link check validation
89. LMIC_setLinkCheckMode(0);
90.
91. // Disable ADR
92. LMIC_setAdrMode(false);
93.
94. // TTN uses SF9 for its RX2 window.
95. LMIC.dn2Dr = DR_SF9;
96.
97. // Set data rate and transmit power for uplink (note: txpow
seems to be ignored by the library)
98. LMIC_setDrTxpow(DR_SF7,14);
99.
100. // Start job
101. do_send(&sendjob);
102. }
103.
104. void loop() {
105. os_runloop_once();
106. }
We’ll need to come back to the Arduino code later, but first we need to register an
account at The Things Network.

CREATE AN ACCOUNT AT THE THINGS NETWORK


(TTN):
We will be using The Things Network (TTN), one of the best free 3rd party network
servers, to view and monitor our LoRa Node. The LoRa Node gathers information from
sensors then transmits the data to the network server. All we need to do is create a TTN
account and register our Lora Node as an application. After that, you will see the status
of the LoRa node on our account’s overview page. As for how data is channelled to the
TTN network server, this is the job of another LoRaWAN component – LoRa Gateway,
which will be discussed in our next tutorial.Let’s start creating an account!

1. Go to account.thethingsnetwork.org and click create an account. You will receive


confirmation email to activate your account.
2. Select CONSOLE from the top menu.
3. Select APPLICATIONS to start registration of LoRa node.

ADD AN APPLICATION
Devices need to be registered with an application to communicate with. Let’s add one.

1. On Applications page, click .


2. The following screen will appear.

3. For Application ID, choose a unique ID of lower case, alphanumeric characters


and nonconsecutive – and _.
4. For Description, enter anything you like.
5. For Application EUI, we can leave it to let TTN generate for us.
6. For Handler registration, we are going to use ttn-handler-brazil.
7. Click Add application at the bottom of screen to finish.

You will be redirected to the newly added application.

REGISTER YOUR DEVICE


You are now ready to register your device to the application.

1. On the Applications > Your Application ID page, scroll down to DEVICES.

2. In DEVICES category, click .


3. You will come to the page as shown in the following.

4. Device ID: For LoRaWAN applications, use lower case alphanumeric characters
without consecutive – and _ .
5. Device EUI: Leave empty so it will be randomly generated.
6. App Key: Leave empty so it will be randomly generated.
7. App EUI: Use the default settings.
8. Click Register to finish. (You will be redirected to the newly registered device)

ACTIVATE YOUR DEVICE


In LoRaWAN, to secure the data traffic, each LoRa node will use “session keys”
provided by the network server to encrypt the content of sent data. When the LoRa data
packets reach the network server, the network server will use the session key to identify
the owner of the LoRa node. This is also why we need to register our device to an
account first.

There are 2 types of device activation, Over-The-Air Activation (OTAA) and Activation
By Personalization (ABP).

 OTAA – A secret key called App Key is generated upon device registration. It is
used to request session keys from network server, where each request will result
in different session key.
 ABP – Session keys are fixed and have to be hard-coded into devices. In TTN,
you set or generate the session keys via the console and hard-code them on your
device. The device will use those keys forever for any data transmission until a
different set of session keys is hard coded into them.

In this tutorial, we will be using ABP instead of OTAA.

1. In the newly registered device page (Applications > Your Application ID >
Devices > Your Device ID), choose Settings from menu located on top-right.
2. Change activation method from OTAA to ABP

3. At the same page, uncheck Frame Counter Checks box as well as shown in
picture below to disable frame counter check for testing purpose.

4. Click Save at the bottom of the page


5. You will see 2 session keys generated for this device, Network Session
Key and Application Session Key. These 2 keys will be used in Arduino Coding.
(We said in ABP, session keys are fixed and have to be hard-coded into devices.
Remember?)
6. You can click on small eye icon in each keys to view t

Вам также может понравиться