Skip to content

Commit eb43159

Browse files
committed
Move Arduino Library to separate repository
Also renamed library, added includes to properties and keywords for syntax highlighting
0 parents  commit eb43159

File tree

7 files changed

+425
-0
lines changed

7 files changed

+425
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <TheThingsNetwork.h>
2+
3+
#define debugSerial Serial
4+
#define loraSerial Serial1
5+
6+
TheThingsNetwork ttn;
7+
8+
void setup()
9+
{
10+
debugSerial.begin(115200);
11+
loraSerial.begin(57600);
12+
13+
delay(3000);
14+
15+
ttn.init(loraSerial, debugSerial); //Initializing...
16+
}
17+
18+
void loop()
19+
{
20+
debugSerial.println("Device Information");
21+
debugSerial.println();
22+
ttn.showStatus();
23+
debugSerial.println();
24+
debugSerial.println("Use the EUI to register the device for OTAA");
25+
debugSerial.println("-------------------------------------------");
26+
debugSerial.println();
27+
delay(10000);
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <TheThingsNetwork.h>
2+
3+
// Set your device address
4+
const byte devAddr[4] = { <insert devAddr> }; //for example: {0x02, 0xDE, 0xAE, 0x00}
5+
6+
// Set your NwkSKey and AppSKey
7+
const byte nwkSKey[16] = { <insert nwkSKey> }; //for example: {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}
8+
const byte appSKey[16] = { <insert appSKey> }; //for example: {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}
9+
10+
#define debugSerial Serial
11+
#define loraSerial Serial1
12+
13+
TheThingsNetwork ttn;
14+
15+
void setup()
16+
{
17+
debugSerial.begin(115200);
18+
loraSerial.begin(57600);
19+
20+
delay(3000);
21+
22+
ttn.init(loraSerial, debugSerial); //Initializing...
23+
24+
ttn.reset();
25+
ttn.personalize(devAddr, nwkSKey, appSKey);
26+
ttn.showStatus();
27+
28+
debugSerial.println("Setup for The Things Network complete");
29+
30+
delay(2000);
31+
}
32+
33+
void loop() {
34+
ttn.sendString("Hello world!");
35+
36+
delay(20000);
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <TheThingsNetwork.h>
2+
3+
// Set your app Credentials
4+
const byte appEui[8] = { <insert appEui> }; //for example: {0x70, 0xB3, 0xD5, 0x7E, 0xE0, 0xE0, 0x01, 0x4A1}
5+
const byte appKey[16] = { <insert aapKey> }; //for example: {0x73, 0x6D, 0x24, 0xD2, 0x69, 0xBE, 0xE3, 0xAE, 0x0E, 0xCE, 0xF0, 0xBB, 0x6C, 0xA4, 0xBA, 0xFE}
6+
7+
#define debugSerial Serial
8+
#define loraSerial Serial1
9+
10+
TheThingsNetwork ttn;
11+
12+
void setup()
13+
{
14+
debugSerial.begin(115200);
15+
loraSerial.begin(57600);
16+
17+
delay(1000);
18+
ttn.init(loraSerial, debugSerial); //Initializing...
19+
ttn.reset();
20+
ttn.join(appEui, appKey);
21+
22+
delay(6000);
23+
ttn.showStatus();
24+
debugSerial.println("Setup for The Things Network complete");
25+
26+
delay(1000);
27+
}
28+
29+
void loop() {
30+
ttn.sendString("Hello world!");
31+
32+
delay(20000);
33+
}

keywords.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#######################################
2+
# Syntax Coloring Map
3+
#######################################
4+
5+
#######################################
6+
# Class (KEYWORD1)
7+
#######################################
8+
9+
TheThingsNetwork KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
init KEYWORD2
16+
reset KEYWORD2
17+
personalize KEYWORD2
18+
join KEYWORD2
19+
sendBytes KEYWORD2
20+
sendString KEYWORD2
21+
showStatus KEYWORD2
22+
23+
debugPrintLn KEYWORD2
24+
debugPrintLn KEYWORD2
25+
26+
#######################################
27+
# Constants (LITERAL1)
28+
#######################################
29+
30+
DEFAULT_WAIT_TIME LITERAL1
31+
DEFAULT_FSB LITERAL1
32+
DEFAULT_FSB LITERAL1
33+
34+
PWRIDX_868 LITERAL1
35+
PWRIDX_915 LITERAL1

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=TheThingsNetwork
2+
version=0.8
3+
author=Johan Stokking, Ludo Teirlinck, Fokke Zandbergen, The Things Network
4+
maintainer=Johan Stokking <johan@thethingsnetwork.org>
5+
sentence=An Arduino library for the The Things Network.
6+
paragraph=Compatible with any Microchip RN2483 and RN2903 device.
7+
category=Communication
8+
url=https://github.com/TheThingsNetwork/arduino-library
9+
architectures=*
10+
includes=TheThingsNetwork.h

src/TheThingsNetwork.cpp

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
// Copyright © 2016 The Things Network
2+
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
3+
4+
#include "Arduino.h"
5+
#include "TheThingsNetwork.h"
6+
7+
void TheThingsNetwork::init(Stream& modemStream, Stream& debugStream) {
8+
this->modemStream = &modemStream;
9+
this->debugStream = &debugStream;
10+
}
11+
12+
String TheThingsNetwork::readLine(int waitTime) {
13+
unsigned long start = millis();
14+
while (millis() < start + waitTime) {
15+
String line = modemStream->readStringUntil('\n');
16+
if (line.length() > 0) {
17+
return line.substring(0, line.length() - 1);
18+
}
19+
}
20+
return "";
21+
}
22+
23+
bool TheThingsNetwork::waitForOK(int waitTime, String okMessage) {
24+
String line = readLine(waitTime);
25+
if (line == "") {
26+
debugPrintLn("Wait for OK time-out expired");
27+
return false;
28+
}
29+
30+
if (line != okMessage) {
31+
debugPrintLn("Response is not OK");
32+
return false;
33+
}
34+
35+
return true;
36+
}
37+
38+
String TheThingsNetwork::readValue(String cmd) {
39+
modemStream->println(cmd);
40+
return readLine();
41+
}
42+
43+
bool TheThingsNetwork::sendCommand(String cmd, int waitTime) {
44+
debugPrintLn("Sending: " + cmd);
45+
46+
modemStream->println(cmd);
47+
48+
return waitForOK(waitTime);
49+
}
50+
51+
bool TheThingsNetwork::sendCommand(String cmd, String value, int waitTime) {
52+
int l = value.length();
53+
byte buf[l];
54+
value.getBytes(buf, l);
55+
56+
return sendCommand(cmd, buf, l, waitTime);
57+
}
58+
59+
char btohexa_high(unsigned char b) {
60+
b >>= 4;
61+
return (b > 0x9u) ? b + 'A' - 10 : b + '0';
62+
}
63+
64+
char btohexa_low(unsigned char b) {
65+
b &= 0x0F;
66+
return (b > 0x9u) ? b + 'A' - 10 : b + '0';
67+
}
68+
69+
bool TheThingsNetwork::sendCommand(String cmd, const byte *buf, int length, int waitTime) {
70+
debugPrintLn("Sending: " + cmd + " with " + String(length) + " bytes");
71+
72+
modemStream->print(cmd + " ");
73+
74+
for (int i = 0; i < length; i++) {
75+
modemStream->print(btohexa_high(buf[i]));
76+
modemStream->print(btohexa_low(buf[i]));
77+
}
78+
modemStream->println();
79+
80+
return waitForOK(waitTime);
81+
}
82+
83+
void TheThingsNetwork::reset(bool adr, int sf, int fsb) {
84+
modemStream->println("sys reset");
85+
String version = readLine(3000);
86+
if (version == "") {
87+
debugPrintLn("Invalid version");
88+
return;
89+
}
90+
91+
model = version.substring(0, version.indexOf(' '));
92+
debugPrintLn("Version is " + version + ", model is " + model);
93+
94+
sendCommand("mac set adr " + String(adr ? "on" : "off"));
95+
96+
int dr = -1;
97+
if (model == "RN2483") {
98+
sendCommand("mac set pwridx " + String(PWRIDX_868));
99+
100+
switch (sf) {
101+
case 7:
102+
dr = 5;
103+
break;
104+
case 8:
105+
dr = 4;
106+
break;
107+
case 9:
108+
dr = 3;
109+
break;
110+
case 10:
111+
dr = 2;
112+
break;
113+
case 11:
114+
dr = 1;
115+
break;
116+
case 12:
117+
dr = 0;
118+
break;
119+
default:
120+
debugPrintLn("Invalid SF")
121+
break;
122+
}
123+
}
124+
else if (model == "RN2903") {
125+
sendCommand("mac set pwridx " + String(PWRIDX_915));
126+
enableFsbChannels(fsb);
127+
128+
switch (sf) {
129+
case 7:
130+
dr = 3;
131+
break;
132+
case 8:
133+
dr = 2;
134+
break;
135+
case 9:
136+
dr = 1;
137+
break;
138+
case 10:
139+
dr = 0;
140+
break;
141+
default:
142+
debugPrintLn("Invalid SF")
143+
break;
144+
}
145+
}
146+
147+
if (dr > -1)
148+
sendCommand("mac set dr " + String(dr));
149+
}
150+
151+
bool TheThingsNetwork::enableFsbChannels(int fsb) {
152+
int chLow = fsb > 0 ? (fsb - 1) * 8 : 0;
153+
int chHigh = fsb > 0 ? chLow + 7 : 71;
154+
155+
for (int i = 0; i < 72; i++)
156+
if (i == 70 || chLow <= i && i <= chHigh)
157+
sendCommand("mac set ch status " + String(i) + " on");
158+
else
159+
sendCommand("mac set ch status " + String(i) + " off");
160+
return true;
161+
}
162+
163+
bool TheThingsNetwork::personalize(const byte devAddr[4], const byte nwkSKey[16], const byte appSKey[16]) {
164+
sendCommand("mac set devaddr", devAddr, 4);
165+
sendCommand("mac set nwkskey", nwkSKey, 16);
166+
sendCommand("mac set appskey", appSKey, 16);
167+
sendCommand("mac join abp");
168+
169+
if (readLine() != "accepted") {
170+
debugPrintLn("Personalize not accepted");
171+
return false;
172+
}
173+
174+
debugPrintLn("Personalize accepted. Status: " + readValue("mac get status"));
175+
return true;
176+
}
177+
178+
bool TheThingsNetwork::join(const byte appEui[8], const byte appKey[16]) {
179+
String devEui = readValue("sys get hweui");
180+
sendCommand("mac set appeui", appEui, 8);
181+
sendCommand("mac set deveui " + devEui);
182+
sendCommand("mac set appkey", appKey, 16);
183+
sendCommand("mac join otaa");
184+
185+
String response = readLine(10000);
186+
if (response != "accepted") {
187+
debugPrintLn("Join not accepted");
188+
return false;
189+
}
190+
191+
debugPrintLn("Join accepted: " + response + ". Status: " + readValue("mac get status"));
192+
return true;
193+
}
194+
195+
void TheThingsNetwork::sendBytes(const byte* buffer, int length, int port, bool confirm) {
196+
if (!sendCommand("mac tx " + String(confirm ? "cnf" : "uncnf") + " " + String(port), buffer, length)) {
197+
debugPrintLn("Send command failed");
198+
return;
199+
}
200+
201+
String response = readLine(10000);
202+
if (response == "")
203+
debugPrintLn("Time-out")
204+
else if (response == "mac_tx_ok")
205+
debugPrintLn("Successful transmission")
206+
//else if (response starts with "mac_rx") // TODO: Handle downlink
207+
else
208+
debugPrintLn("Unexpected response: " + response);
209+
}
210+
211+
void TheThingsNetwork::sendString(String message, int port, bool confirm) {
212+
int l = message.length();
213+
byte buf[l + 1];
214+
message.getBytes(buf, l + 1);
215+
216+
return sendBytes(buf, l, port, confirm);
217+
}
218+
219+
void TheThingsNetwork::showStatus() {
220+
debugPrintLn("EUI: " + readValue("sys get hweui"));
221+
debugPrintLn("Battery: " + readValue("sys get vdd"));
222+
debugPrintLn("AppEUI: " + readValue("mac get appeui"));
223+
debugPrintLn("DevEUI: " + readValue("mac get deveui"));
224+
debugPrintLn("DevAddr: " + readValue("mac get devaddr"));
225+
226+
if (this->model == "RN2483") {
227+
debugPrintLn("Band: " + readValue("mac get band"));
228+
}
229+
230+
debugPrintLn("Data Rate: " + readValue("mac get dr"));
231+
debugPrintLn("RX Delay 1: " + readValue("mac get rxdelay1"));
232+
debugPrintLn("RX Delay 2: " + readValue("mac get rxdelay2"));
233+
}

0 commit comments

Comments
 (0)