Skip to content

Commit

Permalink
fix(openthread): Improves commentaries and code
Browse files Browse the repository at this point in the history
  • Loading branch information
SuGlider committed Jun 20, 2024
1 parent 3687c5f commit bbece1c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
11 changes: 7 additions & 4 deletions libraries/OpenThread/examples/COAP/coap_lamp/coap_lamp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ void setupNode() {
// this function is used by the Lamp mode to listen for CoAP frames from the Switch Node
void otCOAPListen() {
// waits for the client to send a CoAP request
char cliResp[256];
char cliResp[256] = {0};
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
cliResp[len] = '\0';
cliResp[len - 1] = '\0';
if (strlen(cliResp)) {
String sResp(cliResp);
// cliResp shall be something like:
// "coap request from fd0c:94df:f1ae:b39a:ec47:ec6d:15e8:804a PUT with payload: 30"
// payload may be 30 or 31 (HEX) '0' or '1' (ASCII)
log_d("Msg[%s]", cliResp);
if (sResp.startsWith("coap request from") && sResp.indexOf("PUT") > 0) {
uint16_t payloadIdx = sResp.indexOf("payload: ") + 10; // 0x30 | 0x31
char payload = sResp.charAt(payloadIdx);
char payload = sResp.charAt(sResp.length() - 1); // last character in the payload
log_i("CoAP PUT [%s]\r\n", payload == '0' ? "OFF" : "ON");
if (payload == '0') {
for (int16_t c = 248; c > 16; c -= 8) {
Expand All @@ -127,6 +129,7 @@ void setup() {
OThreadCLI.begin(false); // No AutoStart is necessary
OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response
setupNode();
// LED goes Green when all is ready and Red when failed.
}

void loop() {
Expand Down
17 changes: 9 additions & 8 deletions libraries/OpenThread/examples/COAP/coap_switch/coap_switch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoap
return false;
}
Serial.println("OpenThread setup done. Node is ready.");
// all fine! LED goes Blue
// all fine! LED goes and stays Blue
neopixelWrite(RGB_BUILTIN, 0, 0, 64); // BLUE ... Swtich is ready!
return true;
}
Expand Down Expand Up @@ -96,23 +96,24 @@ bool otCoapPUT(bool lampState) {
coapMsg += OT_COAP_RESOURCE_NAME;
coapMsg += " con 0";

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

char cliResp[256];
// waits for the CoAP confirmation and Done message for about 5 seconds
// waits for the CoAP confirmation and Done message for about 1.25 seconds
// timeout is based on Stream::setTimeout()
// Example of the expected confirmation response: "coap response from fdae:3289:1783:5c3f:fd84:c714:7e83:6122"
uint8_t tries = 5;
*cliResp = '\0';
while (tries && !(gotDone && gotConfirmation)) {
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
cliResp[len] = '\0';
cliResp[len - 1] = '\0';
log_d("Try[%d]::MSG[%s]", tries, cliResp);
if (strlen(cliResp)) {
log_d("%s", cliResp);
if (!strncmp(cliResp, "coap response from", 18)) {
gotConfirmation = true;
}
Expand All @@ -132,13 +133,12 @@ bool otCoapPUT(bool lampState) {
void checkUserButton() {
static long unsigned int lastPress = 0;
const long unsigned int debounceTime = 500;
static bool lastLampState = false;
static bool lastLampState = true; // first button press will turn the Lamp OFF from inital Green

pinMode(USER_BUTTON, INPUT_PULLUP); // C6/H2 User Button
if (millis() > lastPress + debounceTime && digitalRead(USER_BUTTON) == LOW) {
if (otCoapPUT(!lastLampState)) {
lastLampState = !lastLampState;
} else {
lastLampState = !lastLampState;
if (!otCoapPUT(lastLampState)) { // failed: Lamp Node is not responding due to be off or unreachable
// timeout from the CoAP PUT message... restart the node.
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... something failed!
Serial.println("Reseting the Node as Switch... wait.");
Expand All @@ -156,6 +156,7 @@ void setup() {
OThreadCLI.begin(false); // No AutoStart is necessary
OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response
setupNode();
// LED goes and keeps Blue when all is ready and Red when failed.
}

void loop() {
Expand Down
4 changes: 2 additions & 2 deletions libraries/OpenThread/examples/SimpleCLI/SimpleCLI.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* OpenThread.begin(false) will not start a node in a Thread Network
* You will need to start it manually using the OpenThread CLI commands
* OpenThread.begin(false) will not automatically start a node in a Thread Network
* The user will need to start it manually using the OpenThread CLI commands
* Use the Serial Monitor to interact with the OpenThread CLI
*
* Type 'help' for a list of commands.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* OpenThread.begin(false) will not start a node in a Thread Network
* A Leader node is the first device to start Thread that has a complete dataset
* This is achieved by using the OpenThread CLI command "dataset init new"
* OpenThread.begin(false) will not automatically start a node in a Thread Network
* A Leader node is the first device, that has a complete dataset, to start Thread
* A complete dataset is easily achieved by using the OpenThread CLI command "dataset init new"
*
* In order to allow other node to join the network,
* all of them shall use the same network master key
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* OpenThread.begin(false) will not start a node in a Thread Network
* A Router/Child node is the device that will join an existent Thread Network
* OpenThread.begin(false) will not automatically start a node in a Thread Network
* A Router/Child node is the device that will join an existing Thread Network
*
* In order to allow this node to join the network,
* it shall use the same network master key as used by the Leader Node
Expand Down

0 comments on commit bbece1c

Please sign in to comment.