|
| 1 | +/* |
| 2 | + OpenThread.begin(true) will automatically start a node in a Thread Network |
| 3 | + Full scanning requires the thread node to be at least in Child state. |
| 4 | +
|
| 5 | + This will scan the IEEE 802.14.5 devices in the local area using CLI "scan" command |
| 6 | + As soon as this device turns into a Child, Router or Leader, it will be able to |
| 7 | + scan for Local Thread Networks as well. |
| 8 | +
|
| 9 | +*/ |
| 10 | + |
| 11 | +#include "OThreadCLI.h" |
| 12 | +#include "OThreadCLI_Util.h" |
| 13 | + |
| 14 | +bool otPrintRespCLI(const char *cmd, Stream &output) { |
| 15 | + char cliResp[256]; |
| 16 | + if (cmd == NULL) { |
| 17 | + return true; |
| 18 | + } |
| 19 | + OThreadCLI.println(cmd); |
| 20 | + while (1) { |
| 21 | + size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp)); |
| 22 | + if (len == 0) { |
| 23 | + return false; // timeout for reading a response from CLI |
| 24 | + } |
| 25 | + // clip it on EOL |
| 26 | + for (int i = 0; i < len; i++) { |
| 27 | + if (cliResp[i] == '\r' || cliResp[i] == '\n') { |
| 28 | + cliResp[i] = '\0'; |
| 29 | + } |
| 30 | + } |
| 31 | + if (!strncmp(cliResp, "Done", 4)) { |
| 32 | + return true; // finished with success |
| 33 | + } |
| 34 | + if (!strncmp(cliResp, "Error", 4)) { |
| 35 | + return false; // the CLI command or its arguments are not valid |
| 36 | + } |
| 37 | + output.println(cliResp); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void setup() { |
| 42 | + Serial.begin(115200); |
| 43 | + OThreadCLI.begin(true); // For scanning, AutoStart must be active, any setup |
| 44 | + OThreadCLI.setTimeout(10000); // 10 seconds for reading a line from CLI - scanning takes time |
| 45 | + Serial.println(); |
| 46 | + Serial.println("This sketch will continuosly scan the Thread Local Network and all devices IEEE 802.15.4 compatible"); |
| 47 | +} |
| 48 | + |
| 49 | +void loop() { |
| 50 | + Serial.println(); |
| 51 | + Serial.println("Scanning for near by IEEE 802.15.4 devices:"); |
| 52 | + // 802.15.4 Scan just need a previous OThreadCLI.begin() to tun on the 802.15.4 stack |
| 53 | + if (!otPrintRespCLI("scan", Serial)) { |
| 54 | + Serial.println("802.15.4 Scan Failed..."); |
| 55 | + } |
| 56 | + delay(5000); |
| 57 | + if (otGetDeviceRole() < OT_ROLE_CHILD) { |
| 58 | + Serial.println(); |
| 59 | + Serial.println("This device has not started Thread yet, bypassing Discovery Scan"); |
| 60 | + return; |
| 61 | + } |
| 62 | + Serial.println(); |
| 63 | + Serial.println("Scanning - MLE Discover:"); |
| 64 | + if (!otPrintRespCLI("discover", Serial)) { |
| 65 | + Serial.println("MLE Discover Failed..."); |
| 66 | + } |
| 67 | + delay(5000); |
| 68 | +} |
0 commit comments