-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
51 additions
and
868 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"targets": { | ||
"esp32": false, | ||
"esp32c2": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
"targets": { | ||
"esp32": false, | ||
"esp32c2": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,160 +1,9 @@ | ||
#include "OThreadCLI.h" | ||
#include "OThreadCLI_Util.h" | ||
|
||
#define USER_BUTTON 9 // C6/H2 Boot button | ||
#define OT_CHANNEL "24" | ||
#define OT_NETWORK_KEY "00112233445566778899aabbccddeeff" | ||
#define OT_MCAST_ADDR "ff05::abcd" | ||
#define OT_COAP_RESOURCE_NAME "Lamp" | ||
|
||
const char *otSetupChild[] = { | ||
// 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" | ||
}; | ||
|
||
const char *otCoapSwitch[] = { | ||
// start and create a CoAP resource | ||
"coap", | ||
"start", | ||
}; | ||
|
||
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 && otGetDeviceRole() != expectedRole1 && otGetDeviceRole() != expectedRole2) { | ||
Serial.print("."); | ||
delay(2500); | ||
tries--; | ||
} | ||
Serial.println(); | ||
if (!tries) { | ||
log_e("Sorry, Device Role failed by timeout! Current Role: %s.", otGetStringDeviceRole()); | ||
neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! | ||
return false; | ||
} | ||
Serial.printf("Device is %s.\r\n", otGetStringDeviceRole()); | ||
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 and stays 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; | ||
String coapMsg = "coap put "; | ||
coapMsg += OT_MCAST_ADDR; | ||
coapMsg += " "; | ||
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 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 - 1] = '\0'; | ||
log_d("Try[%d]::MSG[%s]", tries, cliResp); | ||
if (strlen(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 = 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) { | ||
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."); | ||
// 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(); | ||
// LED goes and keeps Blue when all is ready and Red when failed. | ||
} | ||
|
||
void loop() { | ||
checkUserButton(); | ||
delay(10); | ||
} | ||
{ | ||
"targets": { | ||
"esp32": false, | ||
"esp32c2": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,37 +1,9 @@ | ||
/* | ||
* 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 | ||
* The network master key is a 16-byte key that is used to secure the network | ||
* | ||
* Using the same channel will make the process faster | ||
* | ||
*/ | ||
|
||
#include "OThreadCLI.h" | ||
#include "OThreadCLI_Util.h" | ||
|
||
#define CLI_NETWORK_KEY "dataset networkkey 00112233445566778899aabbccddeeff" | ||
#define CLI_NETWORK_CHANEL "dataset channel 24" | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
OThreadCLI.begin(false); // No AutoStart - fresh start | ||
Serial.println("Setting up OpenThread Node as Leader"); | ||
|
||
OThreadCLI.println("dataset init new"); | ||
OThreadCLI.println(CLI_NETWORK_KEY); | ||
OThreadCLI.println(CLI_NETWORK_CHANEL); | ||
OThreadCLI.println("dataset commit active"); | ||
OThreadCLI.println("ifconfig up"); | ||
OThreadCLI.println("thread start"); | ||
} | ||
|
||
void loop() { | ||
Serial.print("Thread Node State: "); | ||
Serial.println(otGetStringDeviceRole()); | ||
delay(5000); | ||
} | ||
{ | ||
"targets": { | ||
"esp32": false, | ||
"esp32c2": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,41 +1,9 @@ | ||
/* | ||
OpenThread.begin(true) will automatically start a node in a Thread Network | ||
Full scanning requires the thread node to be at least in Child state. | ||
|
||
This will scan the IEEE 802.14.5 devices in the local area using CLI "scan" command | ||
As soon as this device turns into a Child, Router or Leader, it will be able to | ||
scan for Local Thread Networks as well. | ||
|
||
*/ | ||
|
||
#include "OThreadCLI.h" | ||
#include "OThreadCLI_Util.h" | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
OThreadCLI.begin(true); // For scanning, AutoStart must be active, any setup | ||
OThreadCLI.setTimeout(100); // Set a timeout for the CLI response | ||
Serial.println(); | ||
Serial.println("This sketch will continuously scan the Thread Local Network and all devices IEEE 802.15.4 compatible"); | ||
} | ||
|
||
void loop() { | ||
Serial.println(); | ||
Serial.println("Scanning for nearby IEEE 802.15.4 devices:"); | ||
// 802.15.4 Scan just needs a previous OThreadCLI.begin() | ||
if (!otPrintRespCLI("scan", Serial, 3000)) { | ||
Serial.println("Scan Failed..."); | ||
{ | ||
"targets": { | ||
"esp32": false, | ||
"esp32c2": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
delay(5000); | ||
if (otGetDeviceRole() < OT_ROLE_CHILD) { | ||
Serial.println(); | ||
Serial.println("This device has not started Thread yet, bypassing Discovery Scan"); | ||
return; | ||
} | ||
Serial.println(); | ||
Serial.println("Scanning MLE Discover:"); | ||
if (!otPrintRespCLI("discover", Serial, 3000)) { | ||
Serial.println("Discover Failed..."); | ||
} | ||
delay(5000); | ||
} | ||
} |
Oops, something went wrong.