Skip to content

Commit 97af939

Browse files
Merge pull request TheThingsNetwork#175 from TheThingsNetwork/landing
Update examples for landing page
2 parents 2797bc0 + d5c9445 commit 97af939

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

examples/Node/Decoder.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* {
77
* "event": "interval",
8+
* "device": "node",
89
* "battery": 4750,
910
* "light": 33,
1011
* "temperature": 23.94
@@ -14,18 +15,24 @@
1415
function Decoder(bytes, port) {
1516
var decoded = {};
1617

17-
var ports = {
18+
var events = {
1819
1: 'setup',
1920
2: 'interval',
2021
3: 'motion',
2122
4: 'button'
2223
};
2324

24-
decoded.event = ports[port];
25+
if (port > 10) {
26+
port = port - 10;
27+
decoded.device = 'uno';
28+
} else {
29+
decoded.device = 'node';
30+
decoded.battery = (bytes[0] << 8) + bytes[1];
31+
decoded.light = (bytes[2] << 8) + bytes[3];
32+
decoded.temperature = ((bytes[4] << 8) + bytes[5]) / 100;
33+
}
2534

26-
decoded.battery = (bytes[0] << 8) + bytes[1];
27-
decoded.light = (bytes[2] << 8) + bytes[3];
28-
decoded.temperature = ((bytes[4] << 8) + bytes[5]) / 100;
35+
decoded.event = events[port];
2936

3037
return decoded;
3138
}

examples/Node/Node.ino

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <TheThingsNetwork.h>
22
#include <TheThingsNode.h>
33

4-
// Set your AppEUI and AppKey
5-
const char *appEui = "0000000000000000";
6-
const char *appKey = "00000000000000000000000000000000";
4+
// These keys are for https://ttn.fyi/activate
5+
// Replace them if you want to use your own app
6+
const char *appEui = "70B3D57EF0001CEE";
7+
const char *appKey = "F2E5C891560FF9CE24AD56E1A69B85DF";
78

89
#define loraSerial Serial1
910
#define debugSerial Serial
@@ -14,43 +15,45 @@ const char *appKey = "00000000000000000000000000000000";
1415
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
1516
TheThingsNode *node;
1617

17-
enum port : byte
18-
{
19-
PORT_SETUP = 1,
20-
PORT_INTERVAL,
21-
PORT_MOTION,
22-
PORT_BUTTON
23-
};
18+
#define PORT_SETUP 1
19+
#define PORT_INTERVAL 2
20+
#define PORT_MOTION 3
21+
#define PORT_BUTTON 4
2422

2523
void setup() {
2624
loraSerial.begin(57600);
2725
debugSerial.begin(9600);
2826

29-
// Wait a maximum of 10s for Serial Monitor
30-
while (!debugSerial && millis() < 10000);
31-
32-
debugSerial.println("-- TTN: STATUS");
27+
// Test LoRa module
3328
ttn.showStatus();
3429

35-
debugSerial.println("-- TTN: JOIN");
36-
ttn.join(appEui, appKey);
37-
30+
// Config Node
3831
node = TheThingsNode::setup();
39-
node->setColor(TTN_GREEN);
40-
4132
node->configLight(true);
4233
node->configInterval(true, 60000);
43-
4434
node->onWake(wake);
4535
node->onInterval(interval);
4636
node->onSleep(sleep);
47-
4837
node->onMotionStart(onMotionStart);
4938
node->onButtonRelease(onButtonRelease);
5039

40+
// Test sensors and set LED to GREEN if it works
41+
node->showStatus();
42+
node->setColor(TTN_GREEN);
43+
44+
// Wait a maximum of 10s for Serial Monitor
45+
while (!debugSerial && millis() < 10000);
46+
5147
debugSerial.println("-- NODE: STATUS");
5248
node->showStatus();
5349

50+
debugSerial.println("-- TTN: STATUS");
51+
ttn.showStatus();
52+
53+
debugSerial.println("-- TTN: JOIN");
54+
ttn.join(appEui, appKey);
55+
56+
debugSerial.println("-- SEND: SETUP");
5457
sendData(PORT_SETUP);
5558
}
5659

@@ -61,9 +64,7 @@ void loop() {
6164
void interval() {
6265
node->setColor(TTN_BLUE);
6366

64-
debugSerial.println("-- INTERVAL");
65-
node->showStatus();
66-
67+
debugSerial.println("-- SEND: INTERVAL");
6768
sendData(PORT_INTERVAL);
6869
}
6970

@@ -76,21 +77,25 @@ void sleep() {
7677
}
7778

7879
void onMotionStart() {
79-
node->setColor(TTN_RED);
80-
debugSerial.print("-- MOTION STOP");
80+
node->setColor(TTN_BLUE);
8181

82+
debugSerial.print("-- SEND: MOTION");
8283
sendData(PORT_MOTION);
8384
}
8485

8586
void onButtonRelease(unsigned long duration) {
86-
node->setColor(TTN_RED);
87-
debugSerial.print("-- BUTTON RELEASE: ");
87+
node->setColor(TTN_BLUE);
88+
89+
debugSerial.print("-- SEND: BUTTON");
8890
debugSerial.println(duration);
8991

9092
sendData(PORT_BUTTON);
9193
}
9294

93-
void sendData(port) {
95+
void sendData(uint8_t port) {
96+
ttn.showStatus();
97+
node->showStatus();
98+
9499
byte* bytes;
95100
byte payload[9];
96101

@@ -109,5 +114,5 @@ void sendData(port) {
109114
payload[4] = bytes[1];
110115
payload[5] = bytes[0];
111116

112-
ttn.sendBytes(payload, sizeof(payload), PORT_INTERVAL);
117+
ttn.sendBytes(payload, sizeof(payload), port);
113118
}

0 commit comments

Comments
 (0)