1
1
/*
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
+ */
13
13
14
14
#include " OThreadCLI.h"
15
15
#include " OThreadCLI_Util.h"
16
16
17
17
#define CLI_NETWORK_KEY " dataset networkkey 00112233445566778899aabbccddeeff"
18
18
#define CLI_NETWORK_CHANEL " dataset channel 24"
19
19
20
+ otInstance *aInstance = NULL ;
21
+
20
22
void setup () {
21
23
Serial.begin (115200 );
22
24
OThreadCLI.begin (false ); // No AutoStart - fresh start
25
+ Serial.println ();
23
26
Serial.println (" Setting up OpenThread Node as Leader" );
27
+ aInstance = esp_openthread_get_instance ();
24
28
25
29
OThreadCLI.println (" dataset init new" );
26
30
OThreadCLI.println (CLI_NETWORK_KEY);
@@ -31,7 +35,53 @@ void setup() {
31
35
}
32
36
33
37
void loop () {
38
+ Serial.println (" =============================================" );
34
39
Serial.print (" Thread Node State: " );
35
40
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
+
36
86
delay (5000 );
37
87
}
0 commit comments