Skip to content

Commit cbe0f2f

Browse files
OpenThread Example Improvement (#10299)
* feat(openthread): add extended example Creates a new example that mixes different APIs * feat(openthread): create cj.json file Adds neessary CI file * feat(openthread): improve the example Update ExtendedRoterNode.ino with more use of API * feat(openthread): improve the example Adds OpenThread Native calls to the example * feat(openthread): improve the example Update LeaderNode.ino example to add OpenThread Native calls. * fix(openthread): bad formatting using space Update keywords.txt to use TAB instead of SPACE in order to recognize correctly the keywords. * fix(openthread): bad example file name - typo Changed ExtendedRoterNode to ExtendedRouterNode - Typo error. * feat(openthread): add extended example ci.json file Added CI file to the example. * fix(openthread): deleted bad file names in the example Delete libraries/OpenThread/examples/SimpleThreadNetwork/ExtendedRoterNode directory * fix(openthread): typo in commentaries * fix(openthread): typo in commentaries * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 532d5f2 commit cbe0f2f

File tree

5 files changed

+198
-24
lines changed

5 files changed

+198
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"targets": {
3+
"esp32": false,
4+
"esp32c2": false,
5+
"esp32c3": false,
6+
"esp32s2": false,
7+
"esp32s3": false
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
/*
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"
5-
*
6-
* In order to allow other node to join the network,
7-
* all of them shall use the same network master key
8-
* The network master key is a 16-byte key that is used to secure the network
9-
*
10-
* Using the same channel will make the process faster
11-
*
12-
*/
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"
5+
6+
In order to allow other node to join the network,
7+
all of them shall use the same network master key
8+
The network master key is a 16-byte key that is used to secure the network
9+
10+
Using the same channel will make the process faster
11+
12+
*/
1313

1414
#include "OThreadCLI.h"
1515
#include "OThreadCLI_Util.h"
1616

1717
#define CLI_NETWORK_KEY "dataset networkkey 00112233445566778899aabbccddeeff"
1818
#define CLI_NETWORK_CHANEL "dataset channel 24"
1919

20+
otInstance *aInstance = NULL;
21+
2022
void setup() {
2123
Serial.begin(115200);
2224
OThreadCLI.begin(false); // No AutoStart - fresh start
25+
Serial.println();
2326
Serial.println("Setting up OpenThread Node as Leader");
27+
aInstance = esp_openthread_get_instance();
2428

2529
OThreadCLI.println("dataset init new");
2630
OThreadCLI.println(CLI_NETWORK_KEY);
@@ -31,7 +35,53 @@ void setup() {
3135
}
3236

3337
void loop() {
38+
Serial.println("=============================================");
3439
Serial.print("Thread Node State: ");
3540
Serial.println(otGetStringDeviceRole());
41+
42+
// Native OpenThread API calls:
43+
// wait until the node become Child or Router
44+
if (otGetDeviceRole() == OT_ROLE_LEADER) {
45+
// Network Name
46+
const char *networkName = otThreadGetNetworkName(aInstance);
47+
Serial.printf("Network Name: %s\r\n", networkName);
48+
// Channel
49+
uint8_t channel = otLinkGetChannel(aInstance);
50+
Serial.printf("Channel: %d\r\n", channel);
51+
// PAN ID
52+
uint16_t panId = otLinkGetPanId(aInstance);
53+
Serial.printf("PanID: 0x%04x\r\n", panId);
54+
// Extended PAN ID
55+
const otExtendedPanId *extPanId = otThreadGetExtendedPanId(aInstance);
56+
Serial.printf("Extended PAN ID: ");
57+
for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) {
58+
Serial.printf("%02x", extPanId->m8[i]);
59+
}
60+
Serial.println();
61+
// Network Key
62+
otNetworkKey networkKey;
63+
otThreadGetNetworkKey(aInstance, &networkKey);
64+
Serial.printf("Network Key: ");
65+
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) {
66+
Serial.printf("%02x", networkKey.m8[i]);
67+
}
68+
Serial.println();
69+
// IP Addresses
70+
char buf[OT_IP6_ADDRESS_STRING_SIZE];
71+
const otNetifAddress *address = otIp6GetUnicastAddresses(aInstance);
72+
while (address != NULL) {
73+
otIp6AddressToString(&address->mAddress, buf, sizeof(buf));
74+
Serial.printf("IP Address: %s\r\n", buf);
75+
address = address->mNext;
76+
}
77+
// Multicast IP Addresses
78+
const otNetifMulticastAddress *mAddress = otIp6GetMulticastAddresses(aInstance);
79+
while (mAddress != NULL) {
80+
otIp6AddressToString(&mAddress->mAddress, buf, sizeof(buf));
81+
printf("Multicast IP Address: %s\n", buf);
82+
mAddress = mAddress->mNext;
83+
}
84+
}
85+
3686
delay(5000);
3787
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
/*
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
4-
*
5-
* In order to allow this node to join the network,
6-
* it shall use the same network master key as used by the Leader Node
7-
* The network master key is a 16-byte key that is used to secure the network
8-
*
9-
* Using the same channel will make the process faster
10-
*
11-
*/
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
4+
5+
In order to allow this node to join the network,
6+
it shall use the same network master key as used by the Leader Node
7+
The network master key is a 16-byte key that is used to secure the network
8+
9+
Using the same channel will make the process faster
10+
11+
*/
1212

1313
#include "OThreadCLI.h"
1414
#include "OThreadCLI_Util.h"
1515

1616
#define CLI_NETWORK_KEY "dataset networkkey 00112233445566778899aabbccddeeff"
1717
#define CLI_NETWORK_CHANEL "dataset channel 24"
1818

19+
otInstance *aInstance = NULL;
20+
1921
void setup() {
2022
Serial.begin(115200);
2123
OThreadCLI.begin(false); // No AutoStart - fresh start
24+
Serial.println();
2225
Serial.println("Setting up OpenThread Node as Router/Child");
2326
Serial.println("Make sure the Leader Node is already running");
27+
aInstance = esp_openthread_get_instance();
2428

2529
OThreadCLI.println("dataset clear");
2630
OThreadCLI.println(CLI_NETWORK_KEY);
@@ -31,7 +35,53 @@ void setup() {
3135
}
3236

3337
void loop() {
38+
Serial.println("=============================================");
3439
Serial.print("Thread Node State: ");
3540
Serial.println(otGetStringDeviceRole());
41+
42+
// Native OpenThread API calls:
43+
// wait until the node become Child or Router
44+
if (otGetDeviceRole() == OT_ROLE_CHILD || otGetDeviceRole() == OT_ROLE_ROUTER) {
45+
// Network Name
46+
const char *networkName = otThreadGetNetworkName(aInstance);
47+
Serial.printf("Network Name: %s\r\n", networkName);
48+
// Channel
49+
uint8_t channel = otLinkGetChannel(aInstance);
50+
Serial.printf("Channel: %d\r\n", channel);
51+
// PAN ID
52+
uint16_t panId = otLinkGetPanId(aInstance);
53+
Serial.printf("PanID: 0x%04x\r\n", panId);
54+
// Extended PAN ID
55+
const otExtendedPanId *extPanId = otThreadGetExtendedPanId(aInstance);
56+
Serial.printf("Extended PAN ID: ");
57+
for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) {
58+
Serial.printf("%02x", extPanId->m8[i]);
59+
}
60+
Serial.println();
61+
// Network Key
62+
otNetworkKey networkKey;
63+
otThreadGetNetworkKey(aInstance, &networkKey);
64+
Serial.printf("Network Key: ");
65+
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) {
66+
Serial.printf("%02x", networkKey.m8[i]);
67+
}
68+
Serial.println();
69+
// IP Addresses
70+
char buf[OT_IP6_ADDRESS_STRING_SIZE];
71+
const otNetifAddress *address = otIp6GetUnicastAddresses(aInstance);
72+
while (address != NULL) {
73+
otIp6AddressToString(&address->mAddress, buf, sizeof(buf));
74+
Serial.printf("IP Address: %s\r\n", buf);
75+
address = address->mNext;
76+
}
77+
// Multicast IP Addresses
78+
const otNetifMulticastAddress *mAddress = otIp6GetMulticastAddresses(aInstance);
79+
while (mAddress != NULL) {
80+
otIp6AddressToString(&mAddress->mAddress, buf, sizeof(buf));
81+
printf("Multicast IP Address: %s\n", buf);
82+
mAddress = mAddress->mNext;
83+
}
84+
}
85+
3686
delay(5000);
3787
}

libraries/OpenThread/keywords.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ peek KEYWORD2
3131
flush KEYWORD2
3232
otGetDeviceRole KEYWORD2
3333
otGetStringDeviceRole KEYWORD2
34-
otGetRespCmd KEYWORD2
34+
otGetRespCmd KEYWORD2
3535
otExecCommand KEYWORD2
36-
otPrintRespCLI KEYWORD2
37-
otPrintNetworkInformation KEYWORD2
36+
otPrintRespCLI KEYWORD2
37+
otPrintNetworkInformation KEYWORD2
3838

3939
#######################################
4040
# Constants (LITERAL1)

0 commit comments

Comments
 (0)