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";
1415TheThingsNetwork ttn (loraSerial, debugSerial, freqPlan);
1516TheThingsNode *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
2523void 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() {
6164void 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
7879void 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
8586void 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