1
1
#include " OThreadCLI.h"
2
2
#include " OThreadCLI_Util.h"
3
3
4
- #define OT_CHANNEL " 24"
5
- #define OT_NETWORK_KEY " 00112233445566778899aabbccddeeff"
6
- #define OT_MCAST_ADDR " ff05::abcd"
4
+ #define OT_CHANNEL " 24"
5
+ #define OT_NETWORK_KEY " 00112233445566778899aabbccddeeff"
6
+ #define OT_MCAST_ADDR " ff05::abcd"
7
7
#define OT_COAP_RESOURCE_NAME " Lamp"
8
8
9
9
const char *otSetupLeader[] = {
10
10
// clear/disable all
11
- " coap" , " stop" ,
12
- " thread" , " stop" ,
13
- " ifconfig" , " down" ,
14
- " dataset" , " clear" ,
11
+ " coap" , " stop" , " thread" , " stop" , " ifconfig" , " down" , " dataset" , " clear" ,
15
12
// set dataset
16
- " dataset" , " init new" ,
17
- " dataset channel" , OT_CHANNEL,
18
- " dataset networkkey" , OT_NETWORK_KEY,
19
- " dataset" , " commit active" ,
13
+ " dataset" , " init new" , " dataset channel" , OT_CHANNEL, " dataset networkkey" , OT_NETWORK_KEY, " dataset" , " commit active" ,
20
14
// network start
21
- " ifconfig" , " up" ,
22
- " thread" , " start"
15
+ " ifconfig" , " up" , " thread" , " start"
23
16
};
24
17
25
18
const char *otCoapLamp[] = {
26
19
// create a multicast IPv6 Address for this device
27
20
" ipmaddr add" , OT_MCAST_ADDR,
28
21
// start and create a CoAP resource
29
- " coap" , " start" ,
30
- " coap resource" , OT_COAP_RESOURCE_NAME,
31
- " coap set" , " 0"
22
+ " coap" , " start" , " coap resource" , OT_COAP_RESOURCE_NAME, " coap set" , " 0"
32
23
};
33
24
34
25
bool otDeviceSetup (const char **otSetupCmds, uint8_t nCmds1, const char **otCoapCmds, uint8_t nCmds2, ot_device_role_t expectedRole) {
@@ -47,7 +38,7 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
47
38
}
48
39
Serial.println (" OpenThread started.\r\n Waiting for activating correct Device Role." );
49
40
// wait for the expected Device Role to start
50
- uint8_t tries = 24 ; // 24 x 2.5 sec = 1 min
41
+ uint8_t tries = 24 ; // 24 x 2.5 sec = 1 min
51
42
while (tries && otGetDeviceRole () != expectedRole) {
52
43
Serial.print (" ." );
53
44
delay (2500 );
@@ -72,22 +63,20 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
72
63
}
73
64
Serial.println (" OpenThread setup done. Node is ready." );
74
65
// all fine! LED goes Green
75
- neopixelWrite (RGB_BUILTIN, 0 , 64 , 8 ); // GREEN ... Lamp is ready!
66
+ neopixelWrite (RGB_BUILTIN, 0 , 64 , 8 ); // GREEN ... Lamp is ready!
76
67
return true ;
77
68
}
78
69
79
70
void setupNode () {
80
71
// tries to set the Thread Network node and only returns when succeded
81
72
bool startedCorrectly = false ;
82
73
while (!startedCorrectly) {
83
- startedCorrectly |= otDeviceSetup (otSetupLeader, sizeof (otSetupLeader) / sizeof (char *) / 2 ,
84
- otCoapLamp, sizeof (otCoapLamp) / sizeof (char *) / 2 ,
85
- OT_ROLE_LEADER);
74
+ startedCorrectly |=
75
+ otDeviceSetup (otSetupLeader, sizeof (otSetupLeader) / sizeof (char *) / 2 , otCoapLamp, sizeof (otCoapLamp) / sizeof (char *) / 2 , OT_ROLE_LEADER);
86
76
if (!startedCorrectly) {
87
77
Serial.println (" Setup Failed...\r\n Trying again..." );
88
78
}
89
79
}
90
-
91
80
}
92
81
93
82
// this function is used by the Lamp mode to listen for CoAP frames from the Switch Node
@@ -107,16 +96,16 @@ void otCOAPListen() {
107
96
log_i (" CoAP PUT [%s]\r\n " , payload == ' 0' ? " OFF" : " ON" );
108
97
if (payload == ' 0' ) {
109
98
for (int16_t c = 248 ; c > 16 ; c -= 8 ) {
110
- neopixelWrite (RGB_BUILTIN, c, c, c); // ramp down
99
+ neopixelWrite (RGB_BUILTIN, c, c, c); // ramp down
111
100
delay (5 );
112
101
}
113
- neopixelWrite (RGB_BUILTIN, 0 , 0 , 0 ); // Lamp Off
102
+ neopixelWrite (RGB_BUILTIN, 0 , 0 , 0 ); // Lamp Off
114
103
} else {
115
104
for (int16_t c = 16 ; c < 248 ; c += 8 ) {
116
- neopixelWrite (RGB_BUILTIN, c, c, c); // ramp up
105
+ neopixelWrite (RGB_BUILTIN, c, c, c); // ramp up
117
106
delay (5 );
118
107
}
119
- neopixelWrite (RGB_BUILTIN, 255 , 255 , 255 ); // Lamp On
108
+ neopixelWrite (RGB_BUILTIN, 255 , 255 , 255 ); // Lamp On
120
109
}
121
110
}
122
111
}
@@ -126,8 +115,8 @@ void setup() {
126
115
Serial.begin (115200 );
127
116
// LED starts RED, indicating not connected to Thread network.
128
117
neopixelWrite (RGB_BUILTIN, 64 , 0 , 0 );
129
- OThreadCLI.begin (false ); // No AutoStart is necessary
130
- OThreadCLI.setTimeout (250 ); // waits 250ms for the OpenThread CLI response
118
+ OThreadCLI.begin (false ); // No AutoStart is necessary
119
+ OThreadCLI.setTimeout (250 ); // waits 250ms for the OpenThread CLI response
131
120
setupNode ();
132
121
// LED goes Green when all is ready and Red when failed.
133
122
}
0 commit comments