1
1
#include < TheThingsNetwork.h>
2
2
#include < TheThingsNode.h>
3
3
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" ;
7
8
8
9
#define loraSerial Serial1
9
10
#define debugSerial Serial
@@ -14,43 +15,45 @@ const char *appKey = "00000000000000000000000000000000";
14
15
TheThingsNetwork ttn (loraSerial, debugSerial, freqPlan);
15
16
TheThingsNode *node;
16
17
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
24
22
25
23
void setup () {
26
24
loraSerial.begin (57600 );
27
25
debugSerial.begin (9600 );
28
26
29
- // Wait a maximum of 10s for Serial Monitor
30
- while (!debugSerial && millis () < 10000 );
31
-
32
- debugSerial.println (" -- TTN: STATUS" );
27
+ // Test LoRa module
33
28
ttn.showStatus ();
34
29
35
- debugSerial.println (" -- TTN: JOIN" );
36
- ttn.join (appEui, appKey);
37
-
30
+ // Config Node
38
31
node = TheThingsNode::setup ();
39
- node->setColor (TTN_GREEN);
40
-
41
32
node->configLight (true );
42
33
node->configInterval (true , 60000 );
43
-
44
34
node->onWake (wake);
45
35
node->onInterval (interval);
46
36
node->onSleep (sleep);
47
-
48
37
node->onMotionStart (onMotionStart);
49
38
node->onButtonRelease (onButtonRelease);
50
39
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
+
51
47
debugSerial.println (" -- NODE: STATUS" );
52
48
node->showStatus ();
53
49
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" );
54
57
sendData (PORT_SETUP);
55
58
}
56
59
@@ -61,9 +64,7 @@ void loop() {
61
64
void interval () {
62
65
node->setColor (TTN_BLUE);
63
66
64
- debugSerial.println (" -- INTERVAL" );
65
- node->showStatus ();
66
-
67
+ debugSerial.println (" -- SEND: INTERVAL" );
67
68
sendData (PORT_INTERVAL);
68
69
}
69
70
@@ -76,21 +77,25 @@ void sleep() {
76
77
}
77
78
78
79
void onMotionStart () {
79
- node->setColor (TTN_RED);
80
- debugSerial.print (" -- MOTION STOP" );
80
+ node->setColor (TTN_BLUE);
81
81
82
+ debugSerial.print (" -- SEND: MOTION" );
82
83
sendData (PORT_MOTION);
83
84
}
84
85
85
86
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" );
88
90
debugSerial.println (duration);
89
91
90
92
sendData (PORT_BUTTON);
91
93
}
92
94
93
- void sendData (port) {
95
+ void sendData (uint8_t port) {
96
+ ttn.showStatus ();
97
+ node->showStatus ();
98
+
94
99
byte* bytes;
95
100
byte payload[9 ];
96
101
@@ -109,5 +114,5 @@ void sendData(port) {
109
114
payload[4 ] = bytes[1 ];
110
115
payload[5 ] = bytes[0 ];
111
116
112
- ttn.sendBytes (payload, sizeof (payload), PORT_INTERVAL );
117
+ ttn.sendBytes (payload, sizeof (payload), port );
113
118
}
0 commit comments