|
| 1 | +#include "OThreadCLI.h" |
| 2 | +#include "OThreadCLI_Util.h" |
| 3 | + |
| 4 | +// Leader node shall use the same Network Key and channel |
| 5 | +#define CLI_NETWORK_KEY "00112233445566778899aabbccddeeff" |
| 6 | +#define CLI_NETWORK_CHANEL "24" |
| 7 | +bool otStatus = true; |
| 8 | + |
| 9 | +void setup() { |
| 10 | + Serial.begin(115200); |
| 11 | + OThreadCLI.begin(false); // No AutoStart - fresh start |
| 12 | + Serial.println("Setting up OpenThread Node as Router/Child"); |
| 13 | + Serial.println("Make sure the Leader Node is already running"); |
| 14 | + |
| 15 | + otStatus &= otExecCommand("dataset", "clear"); |
| 16 | + otStatus &= otExecCommand("dataset networkkey", CLI_NETWORK_KEY); |
| 17 | + otStatus &= otExecCommand("dataset channel", CLI_NETWORK_CHANEL); |
| 18 | + otStatus &= otExecCommand("dataset", "commit active"); |
| 19 | + otStatus &= otExecCommand("ifconfig", "up"); |
| 20 | + otStatus &= otExecCommand("thread", "start"); |
| 21 | + |
| 22 | + if (!otStatus) { |
| 23 | + Serial.println("\r\n\t===> Failed starting Thread Network!"); |
| 24 | + return; |
| 25 | + } |
| 26 | + // wait for the node to enter in the router state |
| 27 | + uint32_t timeout = millis() + 90000; // waits 90 seconds to |
| 28 | + while (otGetDeviceRole() != OT_ROLE_CHILD && otGetDeviceRole() != OT_ROLE_ROUTER) { |
| 29 | + Serial.print("."); |
| 30 | + if (millis() > timeout) { |
| 31 | + Serial.println("\r\n\t===> Timeout! Failed."); |
| 32 | + otStatus = false; |
| 33 | + break; |
| 34 | + } |
| 35 | + delay(500); |
| 36 | + } |
| 37 | + |
| 38 | + if (otStatus) { |
| 39 | + // print the PanID using 2 methods |
| 40 | + |
| 41 | + // CLI |
| 42 | + char resp[256]; |
| 43 | + if (otGetRespCmd("panid", resp)) { |
| 44 | + Serial.printf("\r\nPanID[using CLI]: %s\r\n", resp); |
| 45 | + } else { |
| 46 | + Serial.printf("\r\nPanID[using CLI]: FAILED!\r\n"); |
| 47 | + } |
| 48 | + |
| 49 | + // OpenThread API |
| 50 | + Serial.printf("PanID[using OT API]: 0x%x\r\n", (uint16_t) otLinkGetPanId(esp_openthread_get_instance())); |
| 51 | + } |
| 52 | + Serial.println("\r\n"); |
| 53 | +} |
| 54 | + |
| 55 | +void loop() { |
| 56 | + if (otStatus) { |
| 57 | + Serial.println("Thread NetworkInformation: "); |
| 58 | + Serial.println("---------------------------"); |
| 59 | + otPrintNetworkInformation(Serial); |
| 60 | + Serial.println("---------------------------"); |
| 61 | + } else { |
| 62 | + Serial.println("Some OpenThread operation has failed..."); |
| 63 | + } |
| 64 | + delay(10000); |
| 65 | +} |
0 commit comments