-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommissionerNode.ino
More file actions
68 lines (58 loc) · 2.38 KB
/
Copy pathCommissionerNode.ino
File metadata and controls
68 lines (58 loc) · 2.38 KB
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
// CommissionerNode - one half of real Thread commissioning.
//
// This board forms a network and then acts as the COMMISSIONER: it admits
// new devices that present the right joining passphrase (PSKd). Flash
// JoinerNode (the other half) to a second board - the joiner needs NO
// network credentials in its sketch, just the PSKd.
//
// Commissioner serial: Joiner serial:
// role -> leader joining with PSKd "J01NME"...
// commissioner active join OK!
// (joiner appears in child table) role -> child
//
// The PSKd below must match the one in JoinerNode (6..32 chars, uppercase
// letters/digits, no I, O, Q or Z).
#include <NiusThread.h>
static const char *kPskd = "J01NME";
static const uint8_t kNetworkKey[16] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
};
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 3000) {}
Thread.begin();
Thread.setNetwork("ArduinoNRF", 25, 0xBEEF, kNetworkKey);
Thread.start();
Serial.println("CommissionerNode: forming network...");
}
void loop() {
static ThreadClass::Role lastRole = ThreadClass::ROLE_DISABLED;
static bool commissionerRequested = false;
static uint32_t renewAtMs = 0;
Thread.process();
if (Thread.role() != lastRole) {
lastRole = Thread.role();
Serial.print("role -> ");
Serial.println(Thread.roleString());
}
// Once attached, become the Commissioner; once active, open the joiner
// window so any device with our PSKd may join. The window has a 10-minute
// timeout, so renew it a minute early - this keeps the node accepting
// joiners indefinitely. For a one-shot window, drop the renewal.
if (!commissionerRequested && Thread.isAttached()) {
commissionerRequested = (Thread.commissionerStart() == ThreadClass::THREAD_OK);
if (commissionerRequested) {
Serial.println("petitioning to become commissioner...");
}
}
if (commissionerRequested && Thread.commissionerActive() &&
(renewAtMs == 0 || (int32_t)(millis() - renewAtMs) >= 0)) {
if (Thread.commissionerAddJoiner(kPskd, 600) == ThreadClass::THREAD_OK) {
renewAtMs = millis() + 9UL * 60UL * 1000UL; // renew after 9 min
Serial.print("joiner window open - PSKd \"");
Serial.print(kPskd);
Serial.println("\" accepted for the next 10 min (auto-renewing)");
}
}
}