@@ -17,7 +17,6 @@ TheThingsNode *node;
17
17
enum port : byte
18
18
{
19
19
PORT_INTERVAL = 1 ,
20
- PORT_TEMPERATURE,
21
20
PORT_MOTION,
22
21
PORT_BUTTON
23
22
};
@@ -29,8 +28,6 @@ void setup() {
29
28
// Wait a maximum of 10s for Serial Monitor
30
29
while (!debugSerial && millis () < 10000 );
31
30
32
- ttn.onMessage (onMessage);
33
-
34
31
debugSerial.println (" -- TTN: STATUS" );
35
32
ttn.showStatus ();
36
33
@@ -40,18 +37,14 @@ void setup() {
40
37
node = TheThingsNode::setup ();
41
38
node->setColor (TTN_GREEN);
42
39
43
- // node->configUSB(true);
44
40
node->configLight (true );
45
- node->configInterval (true , 20000 );
41
+ node->configInterval (true , 60000 );
46
42
47
43
node->onWake (wake);
48
44
node->onInterval (interval);
49
45
node->onSleep (sleep);
50
46
51
- node->onTemperature (onTemperature);
52
47
node->onMotionStart (onMotionStart);
53
- node->onMotionStop (onMotionStop);
54
- node->onButtonPress (onButtonPress);
55
48
node->onButtonRelease (onButtonRelease);
56
49
57
50
debugSerial.println (" -- NODE: STATUS" );
@@ -63,50 +56,12 @@ void loop() {
63
56
}
64
57
65
58
void interval () {
66
- node->setColor (TTN_YELLOW );
59
+ node->setColor (TTN_BLUE );
67
60
68
61
debugSerial.println (" -- INTERVAL" );
69
62
node->showStatus ();
70
63
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);
110
65
}
111
66
112
67
void wake () {
@@ -117,88 +72,39 @@ void sleep() {
117
72
node->setColor (TTN_BLACK);
118
73
}
119
74
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
-
137
75
void onMotionStart () {
138
76
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" );
155
78
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);
162
80
}
163
81
164
82
void onButtonRelease (unsigned long duration) {
165
83
node->setColor (TTN_RED);
166
84
debugSerial.print (" -- BUTTON RELEASE: " );
167
85
debugSerial.println (duration);
168
86
87
+ sendData (PORT_BUTTON);
88
+ }
89
+
90
+ void sendData (port) {
169
91
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 ];
171
98
172
- bytes = (byte*) &duration;
173
- payload[0 ] = bytes[3 ];
174
- payload[1 ] = bytes[2 ];
99
+ uint16_t light = node->getLight ();
100
+ bytes = (byte*) &light;
175
101
payload[2 ] = bytes[1 ];
176
102
payload[3 ] = bytes[0 ];
177
103
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 ];
180
108
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);
204
110
}
0 commit comments