3.3.7
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
# OpenThread CLI onReceive Callback Example
|
||||
|
||||
This example demonstrates how to use the OpenThread CLI callback mechanism to capture and process CLI responses asynchronously.\
|
||||
The application shows how to set up a callback function that processes CLI responses line by line, allowing non-blocking CLI interaction.
|
||||
|
||||
## 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()` which automatically starts a Thread network with default settings.
|
||||
|
||||
## Features
|
||||
|
||||
- CLI response callback using `OpenThreadCLI.onReceive()`
|
||||
- Asynchronous CLI response processing
|
||||
- Non-blocking CLI command execution
|
||||
- Demonstrates callback-based CLI interaction pattern
|
||||
- Automatic Thread network startup with default settings
|
||||
- Device role monitoring via CLI
|
||||
|
||||
## 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
|
||||
|
||||
No configuration is required before uploading the sketch. The example automatically starts a Thread network with default settings.
|
||||
|
||||
## Building and Flashing
|
||||
|
||||
1. Open the `onReceive.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:
|
||||
|
||||
```
|
||||
OpenThread CLI RESP===> disabled
|
||||
OpenThread CLI RESP===> disabled
|
||||
OpenThread CLI RESP===> detached
|
||||
OpenThread CLI RESP===> child
|
||||
OpenThread CLI RESP===> router
|
||||
OpenThread CLI RESP===> router
|
||||
...
|
||||
```
|
||||
|
||||
The callback function processes each line of CLI response, showing the device state transitions from "disabled" to "detached" to "child" to "router" (or "leader").
|
||||
|
||||
## Using the Device
|
||||
|
||||
### Callback Mechanism
|
||||
|
||||
The example demonstrates the callback-based CLI interaction pattern:
|
||||
|
||||
1. **Callback Registration**: `OThreadCLI.onReceive(otReceivedLine)` registers a callback function
|
||||
2. **Command Execution**: `OThreadCLI.println("state")` sends CLI commands
|
||||
3. **Response Processing**: The callback function `otReceivedLine()` processes responses asynchronously
|
||||
4. **Non-blocking**: The main loop continues while CLI responses are processed in the callback
|
||||
|
||||
### Device State Monitoring
|
||||
|
||||
The example continuously monitors the device state:
|
||||
- Sends "state" command every second
|
||||
- Callback processes the response
|
||||
- Shows state transitions as the device joins the Thread network
|
||||
|
||||
### Customizing the Callback
|
||||
|
||||
You can modify the `otReceivedLine()` function to:
|
||||
- Parse specific CLI responses
|
||||
- Extract data from CLI output
|
||||
- Trigger actions based on CLI responses
|
||||
- Filter or process specific response patterns
|
||||
|
||||
## Code Structure
|
||||
|
||||
The onReceive example consists of the following main components:
|
||||
|
||||
1. **`otReceivedLine()` callback function**:
|
||||
- Reads all available data from OpenThread CLI
|
||||
- Filters out empty lines (EOL sequences)
|
||||
- Prints non-empty lines with a prefix
|
||||
|
||||
2. **`setup()`**:
|
||||
- Initializes Serial communication
|
||||
- Starts OpenThread stack with `OpenThread.begin()` (auto-start)
|
||||
- Initializes OpenThread CLI
|
||||
- Registers the callback function using `OThreadCLI.onReceive(otReceivedLine)`
|
||||
|
||||
3. **`loop()`**:
|
||||
- Sends "state" CLI command every second using `OThreadCLI.println("state")`
|
||||
- The callback function processes the response asynchronously
|
||||
- Non-blocking operation allows other tasks to run
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **No callback responses**: Ensure the callback is registered in setup. Check that OpenThread CLI is initialized.
|
||||
- **Empty lines in output**: The callback filters empty lines, which is normal behavior
|
||||
- **State not changing**: Wait for the device to join the Thread network. First device becomes Leader, subsequent devices become Router or Child.
|
||||
- **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,52 @@
|
||||
// Copyright 2024 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() will automatically start a node in a Thread Network
|
||||
This will demonstrate how to capture the CLI response in a callback function
|
||||
The device state shall change from "disabled" to valid Thread states along time
|
||||
*/
|
||||
|
||||
#include "OThreadCLI.h"
|
||||
|
||||
// reads all the lines sent by CLI, one by one
|
||||
// ignores some lines that are just a sequence of \r\n
|
||||
void otReceivedLine() {
|
||||
String line = "";
|
||||
while (OThreadCLI.available() > 0) {
|
||||
char ch = OThreadCLI.read();
|
||||
if (ch != '\r' && ch != '\n') {
|
||||
line += ch;
|
||||
}
|
||||
}
|
||||
// ignores empty lines, usually EOL sequence
|
||||
if (line.length() > 0) {
|
||||
Serial.print("OpenThread CLI RESP===> ");
|
||||
Serial.println(line.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
OThread.begin(); // AutoStart
|
||||
OThreadCLI.begin();
|
||||
OThreadCLI.onReceive(otReceivedLine);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// sends the "state" command to the CLI every second
|
||||
// the onReceive() Callback Function will read and process the response
|
||||
OThreadCLI.println("state");
|
||||
delay(1000);
|
||||
}
|
||||
Reference in New Issue
Block a user