@@ -17,7 +17,6 @@ TheThingsNode *node;
1717enum port : byte
1818{
1919 PORT_INTERVAL = 1 ,
20- PORT_TEMPERATURE,
2120 PORT_MOTION,
2221 PORT_BUTTON
2322};
@@ -29,8 +28,6 @@ void setup() {
2928 // Wait a maximum of 10s for Serial Monitor
3029 while (!debugSerial && millis () < 10000 );
3130
32- ttn.onMessage (onMessage);
33-
3431 debugSerial.println (" -- TTN: STATUS" );
3532 ttn.showStatus ();
3633
@@ -40,18 +37,14 @@ void setup() {
4037 node = TheThingsNode::setup ();
4138 node->setColor (TTN_GREEN);
4239
43- // node->configUSB(true);
4440 node->configLight (true );
45- node->configInterval (true , 20000 );
41+ node->configInterval (true , 60000 );
4642
4743 node->onWake (wake);
4844 node->onInterval (interval);
4945 node->onSleep (sleep);
5046
51- node->onTemperature (onTemperature);
5247 node->onMotionStart (onMotionStart);
53- node->onMotionStop (onMotionStop);
54- node->onButtonPress (onButtonPress);
5548 node->onButtonRelease (onButtonRelease);
5649
5750 debugSerial.println (" -- NODE: STATUS" );
@@ -63,50 +56,12 @@ void loop() {
6356}
6457
6558void interval () {
66- node->setColor (TTN_YELLOW );
59+ node->setColor (TTN_BLUE );
6760
6861 debugSerial.println (" -- INTERVAL" );
6962 node->showStatus ();
7063
71- byte* bytes;
72- byte payload[9 ];
73-
74- payload[0 ] = PORT_INTERVAL;
75-
76- uint16_t battery = node->getBattery ();
77- bytes = (byte*) &battery;
78- payload[1 ] = bytes[1 ];
79- payload[2 ] = bytes[0 ];
80-
81- uint16_t light = node->getLight ();
82- bytes = (byte*) &light;
83- payload[3 ] = bytes[1 ];
84- payload[4 ] = bytes[0 ];
85-
86- int16_t temperature = round (node->getTemperatureAsFloat () * 100 );
87- bytes = (byte*) &temperature;
88- payload[5 ] = bytes[1 ];
89- payload[6 ] = bytes[0 ];
90-
91- payload[7 ] = node->getColor ();
92-
93- byte flags = B00000000;
94-
95- if (node->isMoving ()) {
96- flags = flags | 1 ;
97- }
98-
99- if (node->isButtonPressed ()) {
100- flags = flags | 2 ;
101- }
102-
103- if (node->isUSBConnected ()) {
104- flags = flags | 4 ;
105- }
106-
107- payload[8 ] = flags;
108-
109- ttn.sendBytes (payload, sizeof (payload), PORT_INTERVAL);
64+ sendData (PORT_INTERVAL);
11065}
11166
11267void wake () {
@@ -117,88 +72,39 @@ void sleep() {
11772 node->setColor (TTN_BLACK);
11873}
11974
120- void onTemperature () {
121- float temperature = node->getTemperatureAsFloat ();
122-
123- node->setColor (TTN_RED);
124- debugSerial.println (" -- TEMPERATURE: " + String (temperature));
125-
126- byte* bytes;
127- byte payload[2 ];
128-
129- int16_t rounded = round (temperature * 100 );
130- bytes = (byte*) &rounded;
131- payload[0 ] = bytes[1 ];
132- payload[1 ] = bytes[0 ];
133-
134- ttn.sendBytes (payload, sizeof (payload), PORT_TEMPERATURE);
135- }
136-
13775void onMotionStart () {
13876 node->setColor (TTN_RED);
139- debugSerial.println (" -- MOTION START" );
140- }
141-
142- void onMotionStop (unsigned long duration) {
143- node->setColor (TTN_RED);
144- debugSerial.print (" -- MOTION STOP: " );
145- debugSerial.println (duration);
146-
147- byte* bytes;
148- byte payload[4 ];
149-
150- bytes = (byte*) &duration;
151- payload[0 ] = bytes[3 ];
152- payload[1 ] = bytes[2 ];
153- payload[2 ] = bytes[1 ];
154- payload[3 ] = bytes[0 ];
77+ debugSerial.print (" -- MOTION STOP" );
15578
156- ttn.sendBytes (payload, sizeof (payload), PORT_MOTION);
157- }
158-
159- void onButtonPress () {
160- node->setColor (TTN_RED);
161- debugSerial.println (" -- BUTTON PRESS" );
79+ sendData (PORT_MOTION);
16280}
16381
16482void onButtonRelease (unsigned long duration) {
16583 node->setColor (TTN_RED);
16684 debugSerial.print (" -- BUTTON RELEASE: " );
16785 debugSerial.println (duration);
16886
87+ sendData (PORT_BUTTON);
88+ }
89+
90+ void sendData (port) {
16991 byte* bytes;
170- byte payload[4 ];
92+ byte payload[9 ];
93+
94+ uint16_t battery = node->getBattery ();
95+ bytes = (byte*) &battery;
96+ payload[0 ] = bytes[1 ];
97+ payload[1 ] = bytes[0 ];
17198
172- bytes = (byte*) &duration;
173- payload[0 ] = bytes[3 ];
174- payload[1 ] = bytes[2 ];
99+ uint16_t light = node->getLight ();
100+ bytes = (byte*) &light;
175101 payload[2 ] = bytes[1 ];
176102 payload[3 ] = bytes[0 ];
177103
178- ttn.sendBytes (payload, sizeof (payload), PORT_BUTTON);
179- }
104+ int16_t temperature = round (node->getTemperatureAsFloat () * 100 );
105+ bytes = (byte*) &temperature;
106+ payload[4 ] = bytes[1 ];
107+ payload[5 ] = bytes[0 ];
180108
181- void onMessage (const byte* payload, size_t length, port_t port) {
182- debugSerial.println (" -- ON MESSAGE" );
183-
184- uint32_t interval;
185-
186- switch (port) {
187- case 2 :
188- interval = ((payload[1 ] << 8 ) + payload[2 ]) * 1000 ;
189- node->configInterval (true , interval);
190- debugSerial.println (" Loop delay changed: " + String (interval));
191- loop ();
192- break ;
193- case 3 :
194- bool enabled = (payload[0 ] == 1 );
195- uint8_t lower = (payload[1 ] << 8 ) + payload[2 ];
196- uint8_t upper = (payload[3 ] << 8 ) + payload[4 ];
197- uint8_t critical = (payload[5 ] << 8 ) + payload[6 ];
198- // node.configTemperature(enabled, lower, upper, critical);
199- debugSerial.print (" Temperature alert changed, on: " );
200- debugSerial.print (enabled ? " yes" : " no" );
201- debugSerial.println (" , lower: " + String (lower) + " , upper: " + String (upper) + " , critical: " + String (critical));
202- break ;
203- }
109+ ttn.sendBytes (payload, sizeof (payload), PORT_INTERVAL);
204110}
0 commit comments