-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathLeaderNode.ino
87 lines (76 loc) · 2.95 KB
/
LeaderNode.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
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"
otInstance *aInstance = NULL;
void setup() {
Serial.begin(115200);
OThreadCLI.begin(false); // No AutoStart - fresh start
Serial.println();
Serial.println("Setting up OpenThread Node as Leader");
aInstance = esp_openthread_get_instance();
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.println("=============================================");
Serial.print("Thread Node State: ");
Serial.println(otGetStringDeviceRole());
// Native OpenThread API calls:
// wait until the node become Child or Router
if (otGetDeviceRole() == OT_ROLE_LEADER) {
// Network Name
const char *networkName = otThreadGetNetworkName(aInstance);
Serial.printf("Network Name: %s\r\n", networkName);
// Channel
uint8_t channel = otLinkGetChannel(aInstance);
Serial.printf("Channel: %d\r\n", channel);
// PAN ID
uint16_t panId = otLinkGetPanId(aInstance);
Serial.printf("PanID: 0x%04x\r\n", panId);
// Extended PAN ID
const otExtendedPanId *extPanId = otThreadGetExtendedPanId(aInstance);
Serial.printf("Extended PAN ID: ");
for (int i = 0; i < OT_EXT_PAN_ID_SIZE; i++) {
Serial.printf("%02x", extPanId->m8[i]);
}
Serial.println();
// Nettwork Key
otNetworkKey networkKey;
otThreadGetNetworkKey(aInstance, &networkKey);
Serial.printf("Network Key: ");
for (int i = 0; i < OT_NETWORK_KEY_SIZE; i++) {
Serial.printf("%02x", networkKey.m8[i]);
}
Serial.println();
// IP Addresses
char buf[OT_IP6_ADDRESS_STRING_SIZE];
const otNetifAddress *address = otIp6GetUnicastAddresses(aInstance);
while (address != NULL) {
otIp6AddressToString(&address->mAddress, buf, sizeof(buf));
Serial.printf("IP Address: %s\r\n", buf);
address = address->mNext;
}
// Multicast IP Addresses
const otNetifMulticastAddress *mAddress = otIp6GetMulticastAddresses(aInstance);
while (mAddress != NULL) {
otIp6AddressToString(&mAddress->mAddress, buf, sizeof(buf));
printf("Multicast IP Address: %s\n", buf);
mAddress = mAddress->mNext;
}
}
delay(5000);
}