Skip to content

Commit bd11b90

Browse files
committed
simplify Node example
1 parent 2cf9846 commit bd11b90

File tree

2 files changed

+32
-171
lines changed

2 files changed

+32
-171
lines changed

examples/Node/Decoder.js

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,30 @@
11
/**
22
* Use as payload decoder function.
33
*
4-
* 01 12 8E 00 21 09 5A 03 04
4+
* 12 8E 00 21 09 5A
55
*
66
* {
7-
* "battery": 4750,
87
* "event": "interval",
9-
* "isButtonPressed": false,
10-
* "isMoving": false,
11-
* "isUSBConnected": true,
8+
* "battery": 4750,
129
* "light": 33,
1310
* "temperature": 23.94
1411
* }
15-
*
16-
* 02 08 99
17-
*
18-
* {
19-
* "event": "temperature",
20-
* "temperature": 22.01
21-
* }
22-
*
23-
* 03 00 00 01 FE
24-
*
25-
* {
26-
* "event": "motion",
27-
* "duration": 0.51
28-
* }
29-
*
30-
* 04 00 00 01 FE
31-
*
32-
* {
33-
* "event": "button",
34-
* "duration": 0.51
35-
* }
3612
*/
3713

3814
function Decoder(bytes, port) {
3915
var decoded = {};
4016

4117
var ports = {
42-
PORT_INTERVAL: 1,
43-
PORT_TEMPERATURE: 2,
44-
PORT_MOTION: 3,
45-
PORT_BUTTON: 4
18+
1: 'interval',
19+
2: 'motion',
20+
3: 'button'
4621
};
4722

48-
switch (port) {
49-
case ports.PORT_INTERVAL:
50-
decoded.event = 'interval';
51-
decoded.battery = (bytes[0] << 8) + bytes[1];
52-
decoded.light = (bytes[2] << 8) + bytes[3];
53-
decoded.temperature = ((bytes[4] << 8) + bytes[5]) / 100;
54-
decoded.isMoving = (bytes[6] === 1);
55-
var flags = bytes[7];
56-
decoded.isMoving = (flags & 1) !== 0;
57-
decoded.isButtonPressed = (flags & 2) !== 0;
58-
decoded.isUSBConnected = (flags & 4) !== 0;
59-
break;
60-
case ports.PORT_TEMPERATURE:
61-
decoded.event = 'temperature';
62-
decoded.temperature = ((bytes[0] << 8) + bytes[1]) / 100;
63-
break;
64-
case ports.PORT_MOTION:
65-
decoded.event = 'motion';
66-
decoded.duration = ((bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3]) / 1000;
67-
break;
68-
case ports.PORT_BUTTON:
69-
decoded.event = 'button';
70-
decoded.duration = ((bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3]) / 1000;
71-
break;
72-
}
23+
decoded.event = ports[port];
24+
25+
decoded.battery = (bytes[0] << 8) + bytes[1];
26+
decoded.light = (bytes[2] << 8) + bytes[3];
27+
decoded.temperature = ((bytes[4] << 8) + bytes[5]) / 100;
7328

7429
return decoded;
7530
}

examples/Node/Node.ino

Lines changed: 22 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ TheThingsNode *node;
1717
enum 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

6558
void 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

11267
void 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-
13775
void 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

16482
void 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

Comments
 (0)