11#include " OThreadCLI.h"
22#include " OThreadCLI_Util.h"
33
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"
77#define OT_COAP_RESOURCE_NAME " Lamp"
88
99const char *otSetupLeader[] = {
1010 // clear/disable all
11- " coap" , " stop" ,
12- " thread" , " stop" ,
13- " ifconfig" , " down" ,
14- " dataset" , " clear" ,
11+ " coap" , " stop" , " thread" , " stop" , " ifconfig" , " down" , " dataset" , " clear" ,
1512 // 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" ,
2014 // network start
21- " ifconfig" , " up" ,
22- " thread" , " start"
15+ " ifconfig" , " up" , " thread" , " start"
2316};
2417
2518const char *otCoapLamp[] = {
2619 // create a multicast IPv6 Address for this device
2720 " ipmaddr add" , OT_MCAST_ADDR,
2821 // 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"
3223};
3324
3425bool 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
4738 }
4839 Serial.println (" OpenThread started.\r\n Waiting for activating correct Device Role." );
4940 // 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
5142 while (tries && otGetDeviceRole () != expectedRole) {
5243 Serial.print (" ." );
5344 delay (2500 );
@@ -72,22 +63,20 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
7263 }
7364 Serial.println (" OpenThread setup done. Node is ready." );
7465 // 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!
7667 return true ;
7768}
7869
7970void setupNode () {
8071 // tries to set the Thread Network node and only returns when succeded
8172 bool startedCorrectly = false ;
8273 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);
8676 if (!startedCorrectly) {
8777 Serial.println (" Setup Failed...\r\n Trying again..." );
8878 }
8979 }
90-
9180}
9281
9382// this function is used by the Lamp mode to listen for CoAP frames from the Switch Node
@@ -107,16 +96,16 @@ void otCOAPListen() {
10796 log_i (" CoAP PUT [%s]\r\n " , payload == ' 0' ? " OFF" : " ON" );
10897 if (payload == ' 0' ) {
10998 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
111100 delay (5 );
112101 }
113- neopixelWrite (RGB_BUILTIN, 0 , 0 , 0 ); // Lamp Off
102+ neopixelWrite (RGB_BUILTIN, 0 , 0 , 0 ); // Lamp Off
114103 } else {
115104 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
117106 delay (5 );
118107 }
119- neopixelWrite (RGB_BUILTIN, 255 , 255 , 255 ); // Lamp On
108+ neopixelWrite (RGB_BUILTIN, 255 , 255 , 255 ); // Lamp On
120109 }
121110 }
122111 }
@@ -126,8 +115,8 @@ void setup() {
126115 Serial.begin (115200 );
127116 // LED starts RED, indicating not connected to Thread network.
128117 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
131120 setupNode ();
132121 // LED goes Green when all is ready and Red when failed.
133122}
0 commit comments