Skip to content

Commit bbece1c

Browse files
committed
fix(openthread): Improves commentaries and code
1 parent 3687c5f commit bbece1c

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

libraries/OpenThread/examples/COAP/coap_lamp/coap_lamp.ino

+7-4
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,17 @@ void setupNode() {
9393
// this function is used by the Lamp mode to listen for CoAP frames from the Switch Node
9494
void otCOAPListen() {
9595
// waits for the client to send a CoAP request
96-
char cliResp[256];
96+
char cliResp[256] = {0};
9797
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
98-
cliResp[len] = '\0';
98+
cliResp[len - 1] = '\0';
9999
if (strlen(cliResp)) {
100100
String sResp(cliResp);
101+
// cliResp shall be something like:
102+
// "coap request from fd0c:94df:f1ae:b39a:ec47:ec6d:15e8:804a PUT with payload: 30"
103+
// payload may be 30 or 31 (HEX) '0' or '1' (ASCII)
101104
log_d("Msg[%s]", cliResp);
102105
if (sResp.startsWith("coap request from") && sResp.indexOf("PUT") > 0) {
103-
uint16_t payloadIdx = sResp.indexOf("payload: ") + 10; // 0x30 | 0x31
104-
char payload = sResp.charAt(payloadIdx);
106+
char payload = sResp.charAt(sResp.length() - 1); // last character in the payload
105107
log_i("CoAP PUT [%s]\r\n", payload == '0' ? "OFF" : "ON");
106108
if (payload == '0') {
107109
for (int16_t c = 248; c > 16; c -= 8) {
@@ -127,6 +129,7 @@ void setup() {
127129
OThreadCLI.begin(false); // No AutoStart is necessary
128130
OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response
129131
setupNode();
132+
// LED goes Green when all is ready and Red when failed.
130133
}
131134

132135
void loop() {

libraries/OpenThread/examples/COAP/coap_switch/coap_switch.ino

+9-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
6767
return false;
6868
}
6969
Serial.println("OpenThread setup done. Node is ready.");
70-
// all fine! LED goes Blue
70+
// all fine! LED goes and stays Blue
7171
neopixelWrite(RGB_BUILTIN, 0, 0, 64); // BLUE ... Swtich is ready!
7272
return true;
7373
}
@@ -96,23 +96,24 @@ bool otCoapPUT(bool lampState) {
9696
coapMsg += OT_COAP_RESOURCE_NAME;
9797
coapMsg += " con 0";
9898

99+
// final command is "coap put ff05::abcd Lamp con 1" or "coap put ff05::abcd Lamp con 0"
99100
if (lampState) {
100101
coapMsg[coapMsg.length() - 1] = '1';
101102
}
102103
OThreadCLI.println(coapMsg.c_str());
103104
log_d("Send CLI CMD:[%s]", coapMsg.c_str());
104105

105106
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
107108
// timeout is based on Stream::setTimeout()
109+
// Example of the expected confirmation response: "coap response from fdae:3289:1783:5c3f:fd84:c714:7e83:6122"
108110
uint8_t tries = 5;
109111
*cliResp = '\0';
110112
while (tries && !(gotDone && gotConfirmation)) {
111113
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
112-
cliResp[len] = '\0';
114+
cliResp[len - 1] = '\0';
113115
log_d("Try[%d]::MSG[%s]", tries, cliResp);
114116
if (strlen(cliResp)) {
115-
log_d("%s", cliResp);
116117
if (!strncmp(cliResp, "coap response from", 18)) {
117118
gotConfirmation = true;
118119
}
@@ -132,13 +133,12 @@ bool otCoapPUT(bool lampState) {
132133
void checkUserButton() {
133134
static long unsigned int lastPress = 0;
134135
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
136137

137138
pinMode(USER_BUTTON, INPUT_PULLUP); // C6/H2 User Button
138139
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
142142
// timeout from the CoAP PUT message... restart the node.
143143
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... something failed!
144144
Serial.println("Reseting the Node as Switch... wait.");
@@ -156,6 +156,7 @@ void setup() {
156156
OThreadCLI.begin(false); // No AutoStart is necessary
157157
OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response
158158
setupNode();
159+
// LED goes and keeps Blue when all is ready and Red when failed.
159160
}
160161

161162
void loop() {

libraries/OpenThread/examples/SimpleCLI/SimpleCLI.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* OpenThread.begin(false) will not start a node in a Thread Network
3-
* You will need to start it manually using the OpenThread CLI commands
2+
* OpenThread.begin(false) will not automatically start a node in a Thread Network
3+
* The user will need to start it manually using the OpenThread CLI commands
44
* Use the Serial Monitor to interact with the OpenThread CLI
55
*
66
* Type 'help' for a list of commands.

libraries/OpenThread/examples/SimpleThreadNetwork/LeaderNode/LeaderNode.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* OpenThread.begin(false) will not start a node in a Thread Network
3-
* A Leader node is the first device to start Thread that has a complete dataset
4-
* This is achieved by using the OpenThread CLI command "dataset init new"
2+
* OpenThread.begin(false) will not automatically start a node in a Thread Network
3+
* A Leader node is the first device, that has a complete dataset, to start Thread
4+
* A complete dataset is easily achieved by using the OpenThread CLI command "dataset init new"
55
*
66
* In order to allow other node to join the network,
77
* all of them shall use the same network master key

libraries/OpenThread/examples/SimpleThreadNetwork/RouterNode/RouterNode.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* OpenThread.begin(false) will not start a node in a Thread Network
3-
* A Router/Child node is the device that will join an existent Thread Network
2+
* OpenThread.begin(false) will not automatically start a node in a Thread Network
3+
* A Router/Child node is the device that will join an existing Thread Network
44
*
55
* In order to allow this node to join the network,
66
* it shall use the same network master key as used by the Leader Node

0 commit comments

Comments
 (0)