This commit is contained in:
2026-05-22 21:52:50 +03:00
commit be7c60e4dd
1854 changed files with 583428 additions and 0 deletions
@@ -0,0 +1,80 @@
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "OThreadCLI.h"
#include "OThreadCLI_Util.h"
// Leader node shall use the same Network Key and channel
#define CLI_NETWORK_KEY "00112233445566778899aabbccddeeff"
#define CLI_NETWORK_CHANNEL "24"
bool otStatus = true;
void setup() {
Serial.begin(115200);
OThread.begin(false); // No AutoStart - fresh start
OThreadCLI.begin();
Serial.println("Setting up OpenThread Node as Router/Child");
Serial.println("Make sure the Leader Node is already running");
otStatus &= otExecCommand("dataset", "clear");
otStatus &= otExecCommand("dataset networkkey", CLI_NETWORK_KEY);
otStatus &= otExecCommand("dataset channel", CLI_NETWORK_CHANNEL);
otStatus &= otExecCommand("dataset", "commit active");
otStatus &= otExecCommand("ifconfig", "up");
otStatus &= otExecCommand("thread", "start");
if (!otStatus) {
Serial.println("\r\n\t===> Failed starting Thread Network!");
return;
}
// wait for the node to enter in the router state
uint32_t timeout = millis() + 90000; // waits 90 seconds to
while (OThread.otGetDeviceRole() != OT_ROLE_CHILD && OThread.otGetDeviceRole() != OT_ROLE_ROUTER) {
Serial.print(".");
if (millis() > timeout) {
Serial.println("\r\n\t===> Timeout! Failed.");
otStatus = false;
break;
}
delay(500);
}
if (otStatus) {
// print the PanID using 2 methods
// CLI
char resp[256];
if (otGetRespCmd("panid", resp)) {
Serial.printf("\r\nPanID[using CLI]: %s\r\n", resp);
} else {
Serial.printf("\r\nPanID[using CLI]: FAILED!\r\n");
}
// OpenThread API
Serial.printf("PanID[using OT API]: 0x%x\r\n", (uint16_t)otLinkGetPanId(esp_openthread_get_instance()));
}
Serial.println("\r\n");
}
void loop() {
if (otStatus) {
Serial.println("Thread NetworkInformation: ");
Serial.println("---------------------------");
OThread.otPrintNetworkInformation(Serial);
Serial.println("---------------------------");
} else {
Serial.println("Some OpenThread operation has failed...");
}
delay(10000);
}
@@ -0,0 +1,163 @@
# OpenThread Extended Router Node Example (CLI)
This example demonstrates how to create an OpenThread Router or Child node that joins an existing Thread network, with extended functionality showing both CLI Helper Functions API and native OpenThread API usage.\
The example shows how to retrieve network information using both methods and demonstrates error handling.
## Supported Targets
| SoC | Thread | Status |
| --- | ------ | ------ |
| ESP32-H2 | ✅ | Fully supported |
| ESP32-C6 | ✅ | Fully supported |
| ESP32-C5 | ✅ | Fully supported |
### Note on Thread Support:
- Thread support must be enabled in the ESP-IDF configuration (`CONFIG_OPENTHREAD_ENABLED`). This is done automatically when using the ESP32 Arduino OpenThread library.
- This example uses `OpenThread.begin(false)` which does not automatically start a Thread network, allowing manual configuration.
- **Important:** A Leader node must be running before starting this Router/Child node.
## Features
- Manual Router/Child node configuration using CLI Helper Functions API
- Joins an existing Thread network created by a Leader node
- Demonstrates both CLI Helper Functions API (`otGetRespCmd()`) and native OpenThread API (`otLinkGetPanId()`) usage
- Network information display using both API methods
- Error handling and timeout management
- Comprehensive network status monitoring
## Hardware Requirements
- ESP32 compatible development board with Thread support (ESP32-H2, ESP32-C6, or ESP32-C5)
- USB cable for Serial communication
- A Leader node must be running on the same network
## Software Setup
### Prerequisites
1. Install the Arduino IDE (2.0 or newer recommended)
2. Install ESP32 Arduino Core with OpenThread support
3. ESP32 Arduino libraries:
- `OpenThread`
### Configuration
Before uploading the sketch, configure the network parameters to match the Leader node:
```cpp
#define CLI_NETWORK_KEY "00112233445566778899aabbccddeeff"
#define CLI_NETWORK_CHANNEL "24"
```
**Important:**
- The network key **must match** the Leader node's network key
- The channel **must match** the Leader node's channel
- The network key must be a 32-character hexadecimal string (16 bytes)
- The channel must be between 11 and 26 (IEEE 802.15.4 channels)
## Building and Flashing
1. **First, start the Leader node** using the LeaderNode example
2. Open the `ExtendedRouterNode.ino` sketch in the Arduino IDE.
3. Select your ESP32 board from the **Tools > Board** menu (ESP32-H2, ESP32-C6, or ESP32-C5).
4. Connect your ESP32 board to your computer via USB.
5. Click the **Upload** button to compile and flash the sketch.
## Expected Output
Once the sketch is running, open the Serial Monitor at a baud rate of **115200**. You should see output similar to the following:
```
Setting up OpenThread Node as Router/Child
Make sure the Leader Node is already running
PanID[using CLI]: 0x1234
PanID[using OT API]: 0x1234
Thread NetworkInformation:
---------------------------
Role: Router
RLOC16: 0xfc00
Network Name: OpenThread-ESP
Channel: 24
PAN ID: 0x1234
Extended PAN ID: dead00beef00cafe
Network Key: 00112233445566778899aabbccddeeff
Mesh Local EID: fd00:db8:a0:0:0:ff:fe00:fc00
Leader RLOC: fd00:db8:a0:0:0:ff:fe00:0
Node RLOC: fd00:db8:a0:0:0:ff:fe00:fc00
---------------------------
```
## Using the Device
### Extended Router/Child Node Setup
The Extended Router/Child node is automatically configured in `setup()` using the following sequence:
1. **Clear dataset**: Clears any existing dataset
2. **Set network key**: Configures the network security key (must match Leader)
3. **Set channel**: Configures the IEEE 802.15.4 channel (must match Leader)
4. **Commit dataset**: Applies the dataset to the active configuration
5. **Start interface**: Brings up the network interface
6. **Start Thread**: Starts the Thread network and joins the existing network
7. **Wait for role**: Waits up to 90 seconds for the device to become Router or Child
### Dual API Demonstration
This example demonstrates two ways to access OpenThread information:
1. **CLI Helper Functions API**: Uses `otGetRespCmd("panid", resp)` to get PAN ID via CLI
2. **Native OpenThread API**: Uses `otLinkGetPanId(esp_openthread_get_instance())` to get PAN ID directly
Both methods should return the same value, demonstrating API equivalence.
### Network Information
Once the device joins the network, the `loop()` function displays comprehensive network information using `OpenThread.otPrintNetworkInformation()`, including:
- Device role
- RLOC16
- Network name
- Channel
- PAN ID and Extended PAN ID
- Network key
- IPv6 addresses (Mesh Local EID, Leader RLOC, Node RLOC)
## Code Structure
The ExtendedRouterNode example consists of the following main components:
1. **`setup()`**:
- Initializes Serial communication
- Starts OpenThread stack with `OpenThread.begin(false)` (no auto-start)
- Initializes OpenThread CLI
- Configures the device to join an existing network using CLI Helper Functions:
- `otExecCommand()` - Executes CLI commands with error handling
- Commands: "dataset clear", "dataset networkkey", "dataset channel", "dataset commit active", "ifconfig up", "thread start"
- Waits for device to become Router or Child (with 90-second timeout)
- Demonstrates dual API usage for getting PAN ID
2. **`loop()`**:
- Displays comprehensive network information using `OpenThread.otPrintNetworkInformation()`
- Updates every 10 seconds
- Shows error message if setup failed
## Troubleshooting
- **Device not joining network**: Ensure the Leader node is running first. Verify network key and channel match the Leader exactly.
- **Timeout error**: The device waits 90 seconds to join. If timeout occurs, check network key and channel match the Leader.
- **Network key/channel mismatch**: Double-check that both Leader and Router/Child nodes use identical network key and channel values.
- **Setup failed message**: Check Serial Monitor for specific error messages. Verify Leader node is running and within range.
- **No serial output**: Check baudrate (115200) and USB connection
## Related Documentation
- [OpenThread CLI Helper Functions API](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread_cli.html)
- [OpenThread Core API](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread_core.html)
- [OpenThread Overview](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread.html)
## License
This example is licensed under the Apache License, Version 2.0.
@@ -0,0 +1,3 @@
requires:
- CONFIG_OPENTHREAD_ENABLED=y
- CONFIG_SOC_IEEE802154_SUPPORTED=y
@@ -0,0 +1,102 @@
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
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_CHANNEL "dataset channel 24"
otInstance *aInstance = NULL;
void setup() {
Serial.begin(115200);
OThread.begin(false); // No AutoStart - fresh start
OThreadCLI.begin();
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_CHANNEL);
OThreadCLI.println("dataset commit active");
OThreadCLI.println("ifconfig up");
OThreadCLI.println("thread start");
}
void loop() {
Serial.println("=============================================");
Serial.print("Thread Node State: ");
Serial.println(OThread.otGetStringDeviceRole());
// Native OpenThread API calls:
// wait until the node become Child or Router
if (OThread.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();
// Network 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);
}
@@ -0,0 +1,150 @@
# OpenThread Leader Node Example (CLI)
This example demonstrates how to create an OpenThread Leader node using the CLI Helper Functions API.\
The Leader node is the first device in a Thread network that manages the network and assigns router IDs. This example shows how to configure a Leader node manually using OpenThread CLI commands.
## Supported Targets
| SoC | Thread | Status |
| --- | ------ | ------ |
| ESP32-H2 | ✅ | Fully supported |
| ESP32-C6 | ✅ | Fully supported |
| ESP32-C5 | ✅ | Fully supported |
### Note on Thread Support:
- Thread support must be enabled in the ESP-IDF configuration (`CONFIG_OPENTHREAD_ENABLED`). This is done automatically when using the ESP32 Arduino OpenThread library.
- This example uses `OpenThread.begin(false)` which does not automatically start a Thread network, allowing manual configuration.
## Features
- Manual Leader node configuration using CLI Helper Functions API
- Complete dataset initialization with network key and channel
- Network information display using native OpenThread API calls
- Demonstrates both CLI Helper Functions and native OpenThread API usage
- IPv6 address listing (unicast and multicast)
## Hardware Requirements
- ESP32 compatible development board with Thread support (ESP32-H2, ESP32-C6, or ESP32-C5)
- USB cable for Serial communication
## Software Setup
### Prerequisites
1. Install the Arduino IDE (2.0 or newer recommended)
2. Install ESP32 Arduino Core with OpenThread support
3. ESP32 Arduino libraries:
- `OpenThread`
### Configuration
Before uploading the sketch, you can modify the network configuration:
```cpp
#define CLI_NETWORK_KEY "dataset networkkey 00112233445566778899aabbccddeeff"
#define CLI_NETWORK_CHANNEL "dataset channel 24"
```
**Important:**
- The network key must be a 32-character hexadecimal string (16 bytes)
- The channel must be between 11 and 26 (IEEE 802.15.4 channels)
- All devices in the same network must use the same network key and channel
## Building and Flashing
1. Open the `LeaderNode.ino` sketch in the Arduino IDE.
2. Select your ESP32 board from the **Tools > Board** menu (ESP32-H2, ESP32-C6, or ESP32-C5).
3. Connect your ESP32 board to your computer via USB.
4. Click the **Upload** button to compile and flash the sketch.
## Expected Output
Once the sketch is running, open the Serial Monitor at a baud rate of **115200**. You should see output similar to the following:
```
Setting up OpenThread Node as Leader
=============================================
Thread Node State: Leader
Network Name: OpenThread-ESP
Channel: 24
PanID: 0x1234
Extended PAN ID: dead00beef00cafe
Network Key: 00112233445566778899aabbccddeeff
IP Address: fd00:db8:a0:0:0:ff:fe00:0
Multicast IP Address: ff02::1
Multicast IP Address: ff03::1
...
```
## Using the Device
### Leader Node Setup
The Leader node is automatically configured in `setup()` using the following sequence:
1. **Initialize new dataset**: Creates a complete dataset with random values
2. **Set network key**: Configures the network security key
3. **Set channel**: Configures the IEEE 802.15.4 channel
4. **Commit dataset**: Applies the dataset to the active configuration
5. **Start interface**: Brings up the network interface
6. **Start Thread**: Starts the Thread network
### Network Information
Once the device becomes a Leader, the `loop()` function displays:
- Device role (Leader)
- Network name
- Channel
- PAN ID and Extended PAN ID
- Network key
- All IPv6 addresses (unicast and multicast)
### Joining Other Devices
To join other devices to this network:
1. Use the same network key and channel in the Router/Child node examples
2. Start the Leader node first
3. Then start the Router/Child nodes
## Code Structure
The LeaderNode example consists of the following main components:
1. **`setup()`**:
- Initializes Serial communication
- Starts OpenThread stack with `OpenThread.begin(false)` (no auto-start)
- Initializes OpenThread CLI
- Configures the device as a Leader using CLI Helper Functions:
- `OThreadCLI.println()` - Sends CLI commands
- Commands: "dataset init new", "dataset networkkey", "dataset channel", "dataset commit active", "ifconfig up", "thread start"
2. **`loop()`**:
- Checks if device role is Leader using `OpenThread.otGetDeviceRole()`
- Displays network information using native OpenThread API calls:
- `otThreadGetNetworkName()` - Network name
- `otLinkGetChannel()` - Channel
- `otLinkGetPanId()` - PAN ID
- `otThreadGetExtendedPanId()` - Extended PAN ID
- `otThreadGetNetworkKey()` - Network key
- `otIp6GetUnicastAddresses()` - Unicast IPv6 addresses
- `otIp6GetMulticastAddresses()` - Multicast IPv6 addresses
- Updates every 5 seconds
## Troubleshooting
- **Device not becoming Leader**: Ensure this is the first device started, or clear NVS to start fresh
- **Network key/channel mismatch**: Verify all devices use the same network key and channel
- **No network information**: Wait for the device to become Leader (may take a few seconds)
- **No serial output**: Check baudrate (115200) and USB connection
## Related Documentation
- [OpenThread CLI Helper Functions API](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread_cli.html)
- [OpenThread Core API](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread_core.html)
- [OpenThread Overview](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread.html)
## License
This example is licensed under the Apache License, Version 2.0.
@@ -0,0 +1,3 @@
requires:
- CONFIG_OPENTHREAD_ENABLED=y
- CONFIG_SOC_IEEE802154_SUPPORTED=y
@@ -0,0 +1,162 @@
# OpenThread Router/Child Node Example (CLI)
This example demonstrates how to create an OpenThread Router or Child node that joins an existing Thread network using the CLI Helper Functions API.\
The Router/Child node joins a network created by a Leader node and can route messages or operate as an end device.
## Supported Targets
| SoC | Thread | Status |
| --- | ------ | ------ |
| ESP32-H2 | ✅ | Fully supported |
| ESP32-C6 | ✅ | Fully supported |
| ESP32-C5 | ✅ | Fully supported |
### Note on Thread Support:
- Thread support must be enabled in the ESP-IDF configuration (`CONFIG_OPENTHREAD_ENABLED`). This is done automatically when using the ESP32 Arduino OpenThread library.
- This example uses `OpenThread.begin(false)` which does not automatically start a Thread network, allowing manual configuration.
- **Important:** A Leader node must be running before starting this Router/Child node.
## Features
- Manual Router/Child node configuration using CLI Helper Functions API
- Joins an existing Thread network created by a Leader node
- Network information display using native OpenThread API calls
- Demonstrates both CLI Helper Functions and native OpenThread API usage
- IPv6 address listing (unicast and multicast)
- Automatic role assignment (Router or Child based on network conditions)
## Hardware Requirements
- ESP32 compatible development board with Thread support (ESP32-H2, ESP32-C6, or ESP32-C5)
- USB cable for Serial communication
- A Leader node must be running on the same network
## Software Setup
### Prerequisites
1. Install the Arduino IDE (2.0 or newer recommended)
2. Install ESP32 Arduino Core with OpenThread support
3. ESP32 Arduino libraries:
- `OpenThread`
### Configuration
Before uploading the sketch, configure the network parameters to match the Leader node:
```cpp
#define CLI_NETWORK_KEY "dataset networkkey 00112233445566778899aabbccddeeff"
#define CLI_NETWORK_CHANNEL "dataset channel 24"
```
**Important:**
- The network key **must match** the Leader node's network key
- The channel **must match** the Leader node's channel
- The network key must be a 32-character hexadecimal string (16 bytes)
- The channel must be between 11 and 26 (IEEE 802.15.4 channels)
## Building and Flashing
1. **First, start the Leader node** using the LeaderNode example
2. Open the `RouterNode.ino` sketch in the Arduino IDE.
3. Select your ESP32 board from the **Tools > Board** menu (ESP32-H2, ESP32-C6, or ESP32-C5).
4. Connect your ESP32 board to your computer via USB.
5. Click the **Upload** button to compile and flash the sketch.
## Expected Output
Once the sketch is running, open the Serial Monitor at a baud rate of **115200**. You should see output similar to the following:
```
Setting up OpenThread Node as Router/Child
Make sure the Leader Node is already running
=============================================
Thread Node State: Router
Network Name: OpenThread-ESP
Channel: 24
PanID: 0x1234
Extended PAN ID: dead00beef00cafe
Network Key: 00112233445566778899aabbccddeeff
IP Address: fd00:db8:a0:0:0:ff:fe00:fc00
Multicast IP Address: ff02::1
Multicast IP Address: ff03::1
...
```
The device will join as either a **Router** (if network needs more routers) or **Child** (end device).
## Using the Device
### Router/Child Node Setup
The Router/Child node is automatically configured in `setup()` using the following sequence:
1. **Clear dataset**: Clears any existing dataset
2. **Set network key**: Configures the network security key (must match Leader)
3. **Set channel**: Configures the IEEE 802.15.4 channel (must match Leader)
4. **Commit dataset**: Applies the dataset to the active configuration
5. **Start interface**: Brings up the network interface
6. **Start Thread**: Starts the Thread network and joins the existing network
### Network Information
Once the device joins the network (as Router or Child), the `loop()` function displays:
- Device role (Router or Child)
- Network name (from the Leader)
- Channel
- PAN ID and Extended PAN ID
- Network key
- All IPv6 addresses (unicast and multicast)
### Multi-Device Network
To create a multi-device Thread network:
1. Start the Leader node first (using LeaderNode example)
2. Start Router/Child nodes (using this example)
3. All devices will form a mesh network
4. Routers extend network range and route messages
5. Children are end devices that can sleep
## Code Structure
The RouterNode example consists of the following main components:
1. **`setup()`**:
- Initializes Serial communication
- Starts OpenThread stack with `OpenThread.begin(false)` (no auto-start)
- Initializes OpenThread CLI
- Configures the device to join an existing network using CLI Helper Functions:
- `OThreadCLI.println()` - Sends CLI commands
- Commands: "dataset clear", "dataset networkkey", "dataset channel", "dataset commit active", "ifconfig up", "thread start"
2. **`loop()`**:
- Checks if device role is Router or Child using `OpenThread.otGetDeviceRole()`
- Displays network information using native OpenThread API calls:
- `otThreadGetNetworkName()` - Network name
- `otLinkGetChannel()` - Channel
- `otLinkGetPanId()` - PAN ID
- `otThreadGetExtendedPanId()` - Extended PAN ID
- `otThreadGetNetworkKey()` - Network key
- `otIp6GetUnicastAddresses()` - Unicast IPv6 addresses
- `otIp6GetMulticastAddresses()` - Multicast IPv6 addresses
- Updates every 5 seconds
## Troubleshooting
- **Device not joining network**: Ensure the Leader node is running first. Verify network key and channel match the Leader exactly.
- **Role stuck as "Detached"**: Wait a few seconds for the device to join. Check that network key and channel match the Leader.
- **Network key/channel mismatch**: Double-check that both Leader and Router/Child nodes use identical network key and channel values.
- **No network information**: Wait for the device to join the network (may take 10-30 seconds)
- **No serial output**: Check baudrate (115200) and USB connection
## Related Documentation
- [OpenThread CLI Helper Functions API](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread_cli.html)
- [OpenThread Core API](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread_core.html)
- [OpenThread Overview](https://docs.espressif.com/projects/arduino-esp32/en/latest/openthread/openthread.html)
## License
This example is licensed under the Apache License, Version 2.0.
@@ -0,0 +1,102 @@
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
OpenThread.begin(false) will not automatically start a node in a Thread Network
A Router/Child node is the device that will join an existing Thread Network
In order to allow this node to join the network,
it shall use the same network master key as used by the Leader Node
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_CHANNEL "dataset channel 24"
otInstance *aInstance = NULL;
void setup() {
Serial.begin(115200);
OThread.begin(false); // No AutoStart - fresh start
OThreadCLI.begin();
Serial.println();
Serial.println("Setting up OpenThread Node as Router/Child");
Serial.println("Make sure the Leader Node is already running");
aInstance = esp_openthread_get_instance();
OThreadCLI.println("dataset clear");
OThreadCLI.println(CLI_NETWORK_KEY);
OThreadCLI.println(CLI_NETWORK_CHANNEL);
OThreadCLI.println("dataset commit active");
OThreadCLI.println("ifconfig up");
OThreadCLI.println("thread start");
}
void loop() {
Serial.println("=============================================");
Serial.print("Thread Node State: ");
Serial.println(OThread.otGetStringDeviceRole());
// Native OpenThread API calls:
// wait until the node become Child or Router
if (OThread.otGetDeviceRole() == OT_ROLE_CHILD || OThread.otGetDeviceRole() == OT_ROLE_ROUTER) {
// 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();
// Network 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);
}
@@ -0,0 +1,3 @@
requires:
- CONFIG_OPENTHREAD_ENABLED=y
- CONFIG_SOC_IEEE802154_SUPPORTED=y