@@ -67,7 +67,7 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
67
67
return false ;
68
68
}
69
69
Serial.println (" OpenThread setup done. Node is ready." );
70
- // all fine! LED goes Blue
70
+ // all fine! LED goes and stays Blue
71
71
neopixelWrite (RGB_BUILTIN, 0 , 0 , 64 ); // BLUE ... Swtich is ready!
72
72
return true ;
73
73
}
@@ -96,23 +96,24 @@ bool otCoapPUT(bool lampState) {
96
96
coapMsg += OT_COAP_RESOURCE_NAME;
97
97
coapMsg += " con 0" ;
98
98
99
+ // final command is "coap put ff05::abcd Lamp con 1" or "coap put ff05::abcd Lamp con 0"
99
100
if (lampState) {
100
101
coapMsg[coapMsg.length () - 1 ] = ' 1' ;
101
102
}
102
103
OThreadCLI.println (coapMsg.c_str ());
103
104
log_d (" Send CLI CMD:[%s]" , coapMsg.c_str ());
104
105
105
106
char cliResp[256 ];
106
- // waits for the CoAP confirmation and Done message for about 5 seconds
107
+ // waits for the CoAP confirmation and Done message for about 1.25 seconds
107
108
// timeout is based on Stream::setTimeout()
109
+ // Example of the expected confirmation response: "coap response from fdae:3289:1783:5c3f:fd84:c714:7e83:6122"
108
110
uint8_t tries = 5 ;
109
111
*cliResp = ' \0 ' ;
110
112
while (tries && !(gotDone && gotConfirmation)) {
111
113
size_t len = OThreadCLI.readBytesUntil (' \n ' , cliResp, sizeof (cliResp));
112
- cliResp[len] = ' \0 ' ;
114
+ cliResp[len - 1 ] = ' \0 ' ;
113
115
log_d (" Try[%d]::MSG[%s]" , tries, cliResp);
114
116
if (strlen (cliResp)) {
115
- log_d (" %s" , cliResp);
116
117
if (!strncmp (cliResp, " coap response from" , 18 )) {
117
118
gotConfirmation = true ;
118
119
}
@@ -132,13 +133,12 @@ bool otCoapPUT(bool lampState) {
132
133
void checkUserButton () {
133
134
static long unsigned int lastPress = 0 ;
134
135
const long unsigned int debounceTime = 500 ;
135
- static bool lastLampState = false ;
136
+ static bool lastLampState = true ; // first button press will turn the Lamp OFF from inital Green
136
137
137
138
pinMode (USER_BUTTON, INPUT_PULLUP); // C6/H2 User Button
138
139
if (millis () > lastPress + debounceTime && digitalRead (USER_BUTTON) == LOW) {
139
- if (otCoapPUT (!lastLampState)) {
140
- lastLampState = !lastLampState;
141
- } else {
140
+ lastLampState = !lastLampState;
141
+ if (!otCoapPUT (lastLampState)) { // failed: Lamp Node is not responding due to be off or unreachable
142
142
// timeout from the CoAP PUT message... restart the node.
143
143
neopixelWrite (RGB_BUILTIN, 255 , 0 , 0 ); // RED ... something failed!
144
144
Serial.println (" Reseting the Node as Switch... wait." );
@@ -156,6 +156,7 @@ void setup() {
156
156
OThreadCLI.begin (false ); // No AutoStart is necessary
157
157
OThreadCLI.setTimeout (250 ); // waits 250ms for the OpenThread CLI response
158
158
setupNode ();
159
+ // LED goes and keeps Blue when all is ready and Red when failed.
159
160
}
160
161
161
162
void loop () {
0 commit comments