Skip to content

Commit 0c49edd

Browse files
authored
feat(openthread): improve the example
Update LeaderNode.ino example to add OpenThread Native calls.
1 parent 1b27f84 commit 0c49edd

File tree

1 file changed

+61
-11
lines changed
  • libraries/OpenThread/examples/SimpleThreadNetwork/LeaderNode

1 file changed

+61
-11
lines changed
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+
// Nettwork 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
}

0 commit comments

Comments
 (0)