-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New OpenThread CLI Arduino Library for ESP32-C6 and ESP32-H2 #9908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
9e26803
feat(OThread): Add Library
SuGlider fb2a502
Merge branch 'master' into OpenThread
SuGlider 1a66bc1
fix(OpenThread): fixes file list in CMakeLists.txt
SuGlider 827b72c
Merge branch 'OpenThread' of https://github.com/SuGlider/arduino-esp3…
SuGlider 54f043a
fix(openthread): Fixes JSON CI Files
SuGlider 69f4fce
Merge branch 'master' into OpenThread
SuGlider 3e11343
fix(openthread): Fixes JSON CI Files
SuGlider 1b2530d
fix(openthread): Include Openthread guarding
SuGlider 739b77c
fix(openthread): COAP parametrization
SuGlider 3687c5f
fix(openthread): Include Openthread guarding
SuGlider bbece1c
fix(openthread): Improves commentaries and code
SuGlider 5c677be
fix(openthread): Improves code
SuGlider 436a7ff
fix(openthread): Includes StreamString.h
SuGlider 378f993
Merge branch 'master' into OpenThread
SuGlider ce46f4a
feat(openthread): New Scan Example
SuGlider 7fece86
feat(openthread): Improved Scan Example
SuGlider 001ae42
feat(openthread): README.md
SuGlider 198e6e8
feat(openthread): helper functions documentation
SuGlider 28d7c44
fix(openthread): begin end
SuGlider 3507a9d
feat(openthread): onReceice example
SuGlider f9d1e55
Merge branch 'OpenThread' of https://github.com/SuGlider/arduino-esp3…
SuGlider 176f92b
fix(openthread): tx queue error
SuGlider 6b0b9af
fix(doc): fixing documentation apresentation
SuGlider 4ec1397
fix(doc): documentation format
SuGlider 2aaa575
feat(openthread): commentary
SuGlider 46cf9aa
Merge branch 'master' into OpenThread
lucasssvaz eb7b019
fix(openthread): Typo, start/stop console
SuGlider 919bf79
Merge branch 'OpenThread' of https://github.com/SuGlider/arduino-esp3…
SuGlider b60a354
fix(openthread): library properties
SuGlider 11ec1d1
ci(pre-commit): Apply automatic fixes
lucasssvaz 55f9378
feat(openthread): formatting text
SuGlider 81d62e9
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"targets": { | ||
"esp32": false | ||
"esp32c2": false | ||
"esp32c3": false | ||
"esp32s2": false | ||
"esp32s3": false | ||
} | ||
} | ||
SuGlider marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
133 changes: 133 additions & 0 deletions
133
libraries/OpenThread/examples/COAP/coap_lamp/coap_lamp.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#include "OThreadCLI.h" | ||
#include "OThreadCLI_Util.h" | ||
|
||
#define OT_CHANNEL "24" | ||
#define OT_NETWORK_KEY "00112233445566778899aabbccddeeff" | ||
|
||
const char *otSetupLeader[] = { | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// clear/disable all | ||
"coap", "stop", | ||
"thread", "stop", | ||
"ifconfig", "down", | ||
"dataset", "clear", | ||
// set dataset | ||
"dataset", "init new", | ||
"dataset channel", OT_CHANNEL, | ||
"dataset networkkey", OT_NETWORK_KEY, | ||
"dataset", "commit active", | ||
// network start | ||
"ifconfig", "up", | ||
"thread", "start" | ||
}; | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const char *otCoapLamp[] = { | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// create a multicast IPv6 Address for this device | ||
"ipmaddr add", "ff05::abcd", | ||
// start and create a CoAP resource | ||
"coap", "start", | ||
"coap resource", "Lamp", | ||
"coap set", "0" | ||
}; | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoapCmds, uint8_t nCmds2, ot_device_role_t expectedRole) { | ||
Serial.println("Starting OpenThread."); | ||
Serial.println("Running as Lamp (RGB LED) - use the other C6/H2 as a Switch"); | ||
uint8_t i; | ||
for (i = 0; i < nCmds1; i++) { | ||
if (!otExecCommand(otSetupCmds[i * 2], otSetupCmds[i * 2 + 1])) { | ||
break; | ||
} | ||
} | ||
if (i != nCmds1) { | ||
log_e("Sorry, OpenThread Network setup failed!"); | ||
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! | ||
return false; | ||
} | ||
Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role."); | ||
// wait for the expected Device Role to start | ||
uint8_t tries = 24; // 24 x 2.5 sec = 1 min | ||
while (tries && getOtDeviceRole() != expectedRole) { | ||
Serial.print("."); | ||
delay(2500); | ||
tries--; | ||
} | ||
Serial.println(); | ||
if (!tries) { | ||
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", getStringOtDeviceRole()); | ||
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! | ||
return false; | ||
} | ||
Serial.printf("Device is %s.\r\n", getStringOtDeviceRole()); | ||
for (i = 0; i < nCmds2; i++) { | ||
if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) { | ||
break; | ||
} | ||
} | ||
if (i != nCmds2) { | ||
log_e("Sorry, OpenThread CoAP setup failed!"); | ||
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! | ||
return false; | ||
} | ||
Serial.println("OpenThread setup done. Node is ready."); | ||
// all fine! LED goes Green | ||
neopixelWrite(RGB_BUILTIN, 0, 64, 8); // GREEN ... Lamp is ready! | ||
return true; | ||
} | ||
|
||
void setupNode() { | ||
// tries to set the Thread Network node and only returns when succeded | ||
bool startedCorrectly = false; | ||
while (!startedCorrectly) { | ||
startedCorrectly |= otDeviceSetup(otSetupLeader, sizeof(otSetupLeader) / sizeof(char *) / 2, | ||
otCoapLamp, sizeof(otCoapLamp) / sizeof(char *) / 2, | ||
OT_ROLE_LEADER); | ||
if (!startedCorrectly) { | ||
Serial.println("Setup Failed...\r\nTrying again..."); | ||
} | ||
} | ||
|
||
} | ||
|
||
// 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]; | ||
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp)); | ||
cliResp[len] = '\0'; | ||
if (strlen(cliResp)) { | ||
String sResp(cliResp); | ||
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); | ||
log_i("CoAP PUT [%s]\r\n", payload == '0' ? "OFF" : "ON"); | ||
if (payload == '0') { | ||
for (int16_t c = 248; c > 16; c -= 8) { | ||
neopixelWrite(RGB_BUILTIN, c, c, c); // ramp down | ||
delay(5); | ||
} | ||
neopixelWrite(RGB_BUILTIN, 0, 0, 0); // Lamp Off | ||
} else { | ||
for (int16_t c = 16; c < 248; c += 8) { | ||
neopixelWrite(RGB_BUILTIN, c, c, c); // ramp up | ||
delay(5); | ||
} | ||
neopixelWrite(RGB_BUILTIN, 255, 255, 255); // Lamp On | ||
} | ||
} | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
// LED starts RED, indicating not connected to Thread network. | ||
neopixelWrite(RGB_BUILTIN, 64, 0, 0); | ||
OThreadCLI.begin(false); // No AutoStart is necessary | ||
OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response | ||
setupNode(); | ||
} | ||
|
||
void loop() { | ||
otCOAPListen(); | ||
delay(10); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"targets": { | ||
"esp32": false | ||
"esp32c2": false | ||
"esp32c3": false | ||
"esp32s2": false | ||
"esp32s3": false | ||
} | ||
} | ||
|
158 changes: 158 additions & 0 deletions
158
libraries/OpenThread/examples/COAP/coap_switch/coap_switch.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#include "OThreadCLI.h" | ||
#include "OThreadCLI_Util.h" | ||
|
||
#define USER_BUTTON 9 // C6/H2 Boot button | ||
#define OT_CHANNEL "24" | ||
#define OT_NETWORK_KEY "00112233445566778899aabbccddeeff" | ||
|
||
const char *otSetupChild[] = { | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// clear/disable all | ||
"coap", "stop", | ||
"thread", "stop", | ||
"ifconfig", "down", | ||
"dataset", "clear", | ||
// set dataset | ||
"dataset channel", OT_CHANNEL, | ||
"dataset networkkey", OT_NETWORK_KEY, | ||
"dataset", "commit active", | ||
// network start | ||
"ifconfig", "up", | ||
"thread", "start" | ||
}; | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const char *otCoapSwitch[] = { | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// start and create a CoAP resource | ||
"coap", "start", | ||
}; | ||
lucasssvaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoapCmds, uint8_t nCmds2, ot_device_role_t expectedRole1, ot_device_role_t expectedRole2) { | ||
Serial.println("Starting OpenThread."); | ||
Serial.println("Running as Switch - use the BOOT button to toggle the other C6/H2 as a Lamp"); | ||
uint8_t i; | ||
for (i = 0; i < nCmds1; i++) { | ||
if (!otExecCommand(otSetupCmds[i * 2], otSetupCmds[i * 2 + 1])) { | ||
break; | ||
} | ||
} | ||
if (i != nCmds1) { | ||
log_e("Sorry, OpenThread Network setup failed!"); | ||
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! | ||
return false; | ||
} | ||
Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role."); | ||
// wait for the expected Device Role to start | ||
uint8_t tries = 24; // 24 x 2.5 sec = 1 min | ||
while (tries && getOtDeviceRole() != expectedRole1 && getOtDeviceRole() != expectedRole2) { | ||
Serial.print("."); | ||
delay(2500); | ||
tries--; | ||
} | ||
Serial.println(); | ||
if (!tries) { | ||
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", getStringOtDeviceRole()); | ||
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! | ||
return false; | ||
} | ||
Serial.printf("Device is %s.\r\n", getStringOtDeviceRole()); | ||
for (i = 0; i < nCmds2; i++) { | ||
if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) { | ||
break; | ||
} | ||
} | ||
if (i != nCmds2) { | ||
log_e("Sorry, OpenThread CoAP setup failed!"); | ||
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! | ||
return false; | ||
} | ||
Serial.println("OpenThread setup done. Node is ready."); | ||
// all fine! LED goes Blue | ||
neopixelWrite(RGB_BUILTIN, 0, 0, 64); // BLUE ... Swtich is ready! | ||
return true; | ||
} | ||
|
||
|
||
void setupNode() { | ||
// tries to set the Thread Network node and only returns when succeded | ||
bool startedCorrectly = false; | ||
while (!startedCorrectly) { | ||
startedCorrectly |= otDeviceSetup(otSetupChild, sizeof(otSetupChild) / sizeof(char *) / 2, | ||
otCoapSwitch, sizeof(otCoapSwitch) / sizeof(char *) / 2, | ||
OT_ROLE_CHILD, OT_ROLE_ROUTER); | ||
if (!startedCorrectly) { | ||
Serial.println("Setup Failed...\r\nTrying again..."); | ||
} | ||
} | ||
|
||
} | ||
|
||
// Sends the CoAP frame to the Lamp node | ||
bool otCoapPUT(bool lampState) { | ||
bool gotDone = false, gotConfirmation = false; | ||
char coapMsg[] = "coap put ff05::abcd Lamp con 0"; | ||
|
||
if (lampState) { | ||
coapMsg[strlen(coapMsg) - 1] = '1'; | ||
} | ||
OThreadCLI.println(coapMsg); | ||
log_d("Send CLI CMD:[%s]", coapMsg); | ||
|
||
char cliResp[256]; | ||
// waits for the CoAP confirmation and Done message for about 5 seconds | ||
// timeout is based on Stream::setTimeout() | ||
uint8_t tries = 5; | ||
*cliResp = '\0'; | ||
while (tries && !(gotDone && gotConfirmation)) { | ||
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp)); | ||
cliResp[len] = '\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; | ||
} | ||
if (!strncmp(cliResp, "Done", 4)) { | ||
gotDone = true; | ||
} | ||
} | ||
tries--; | ||
} | ||
if (gotDone && gotConfirmation) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
// this fucntion is used by the Switch mode to check the BOOT Button and send the user action to the Lamp node | ||
void checkUserButton() { | ||
static long unsigned int lastPress = 0; | ||
const long unsigned int debounceTime = 500; | ||
static bool lastLampState = false; | ||
|
||
pinMode(USER_BUTTON, INPUT_PULLUP); // C6/H2 User Button | ||
if (millis() > lastPress + debounceTime && digitalRead(USER_BUTTON) == LOW) { | ||
if (otCoapPUT(!lastLampState)) { | ||
lastLampState = !lastLampState; | ||
} else { | ||
// 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."); | ||
// start over... | ||
setupNode(); | ||
} | ||
lastPress = millis(); | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
// LED starts RED, indicating not connected to Thread network. | ||
neopixelWrite(RGB_BUILTIN, 64, 0, 0); | ||
OThreadCLI.begin(false); // No AutoStart is necessary | ||
OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response | ||
setupNode(); | ||
} | ||
|
||
void loop() { | ||
checkUserButton(); | ||
delay(10); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* OpenThread.begin(false) will not start a node in a Thread Network | ||
* You 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. | ||
* Documentation: https://openthread.io/reference/cli/commands | ||
* | ||
*/ | ||
|
||
#include "OThreadCLI.h" | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
OThreadCLI.begin(false); // No AutoStart - fresh start | ||
Serial.println("OpenThread CLI started - type 'help' for a list of commands."); | ||
OThreadCLI.startOpenThreadConsole(Serial); | ||
} | ||
|
||
void loop() { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"targets": { | ||
"esp32": false | ||
"esp32c2": false | ||
"esp32c3": false | ||
"esp32s2": false | ||
"esp32s3": false | ||
} | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.