|
| 1 | +#include "OThreadCLI.h" |
| 2 | +#include "OThreadCLI_Util.h" |
| 3 | + |
| 4 | +#define OT_CHANNEL "24" |
| 5 | +#define OT_NETWORK_KEY "00112233445566778899aabbccddeeff" |
| 6 | +#define OT_MCAST_ADDR "ff05::abcd" |
| 7 | +#define OT_COAP_RESOURCE_NAME "Lamp" |
| 8 | + |
| 9 | +const char *otSetupLeader[] = { |
| 10 | + // -- clear/disable all |
| 11 | + // stop CoAP |
| 12 | + "coap", "stop", |
| 13 | + // stop Thread |
| 14 | + "thread", "stop", |
| 15 | + // stop the interface |
| 16 | + "ifconfig", "down", |
| 17 | + // clear the dataset |
| 18 | + "dataset", "clear", |
| 19 | + // -- set dataset |
| 20 | + // create a new complete dataset with random data |
| 21 | + "dataset", "init new", |
| 22 | + // set the channel |
| 23 | + "dataset channel", OT_CHANNEL, |
| 24 | + // set the network key |
| 25 | + "dataset networkkey", OT_NETWORK_KEY, |
| 26 | + // commit the dataset |
| 27 | + "dataset", "commit active", |
| 28 | + // -- network start |
| 29 | + // start the interface |
| 30 | + "ifconfig", "up", |
| 31 | + // start the Thread network |
| 32 | + "thread", "start" |
| 33 | +}; |
| 34 | + |
| 35 | +const char *otCoapLamp[] = { |
| 36 | + // -- create a multicast IPv6 Address for this device |
| 37 | + "ipmaddr add", OT_MCAST_ADDR, |
| 38 | + // -- start and create a CoAP resource |
| 39 | + // start CoAP as server |
| 40 | + "coap", "start", |
| 41 | + // create a CoAP resource |
| 42 | + "coap resource", OT_COAP_RESOURCE_NAME, |
| 43 | + // set the CoAP resource initial value |
| 44 | + "coap set", "0" |
| 45 | +}; |
| 46 | + |
| 47 | +bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoapCmds, uint8_t nCmds2, ot_device_role_t expectedRole) { |
| 48 | + Serial.println("Starting OpenThread."); |
| 49 | + Serial.println("Running as Lamp (RGB LED) - use the other C6/H2 as a Switch"); |
| 50 | + uint8_t i; |
| 51 | + for (i = 0; i < nCmds1; i++) { |
| 52 | + if (!otExecCommand(otSetupCmds[i * 2], otSetupCmds[i * 2 + 1])) { |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + if (i != nCmds1) { |
| 57 | + log_e("Sorry, OpenThread Network setup failed!"); |
| 58 | + neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! |
| 59 | + return false; |
| 60 | + } |
| 61 | + Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role."); |
| 62 | + // wait for the expected Device Role to start |
| 63 | + uint8_t tries = 24; // 24 x 2.5 sec = 1 min |
| 64 | + while (tries && otGetDeviceRole() != expectedRole) { |
| 65 | + Serial.print("."); |
| 66 | + delay(2500); |
| 67 | + tries--; |
| 68 | + } |
| 69 | + Serial.println(); |
| 70 | + if (!tries) { |
| 71 | + log_e("Sorry, Device Role failed by timeout! Current Role: %s.", otGetStringDeviceRole()); |
| 72 | + neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! |
| 73 | + return false; |
| 74 | + } |
| 75 | + Serial.printf("Device is %s.\r\n", otGetStringDeviceRole()); |
| 76 | + for (i = 0; i < nCmds2; i++) { |
| 77 | + if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) { |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + if (i != nCmds2) { |
| 82 | + log_e("Sorry, OpenThread CoAP setup failed!"); |
| 83 | + neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! |
| 84 | + return false; |
| 85 | + } |
| 86 | + Serial.println("OpenThread setup done. Node is ready."); |
| 87 | + // all fine! LED goes Green |
| 88 | + neopixelWrite(RGB_BUILTIN, 0, 64, 8); // GREEN ... Lamp is ready! |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +void setupNode() { |
| 93 | + // tries to set the Thread Network node and only returns when succeded |
| 94 | + bool startedCorrectly = false; |
| 95 | + while (!startedCorrectly) { |
| 96 | + startedCorrectly |= |
| 97 | + otDeviceSetup(otSetupLeader, sizeof(otSetupLeader) / sizeof(char *) / 2, otCoapLamp, sizeof(otCoapLamp) / sizeof(char *) / 2, OT_ROLE_LEADER); |
| 98 | + if (!startedCorrectly) { |
| 99 | + Serial.println("Setup Failed...\r\nTrying again..."); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// this function is used by the Lamp mode to listen for CoAP frames from the Switch Node |
| 105 | +void otCOAPListen() { |
| 106 | + // waits for the client to send a CoAP request |
| 107 | + char cliResp[256] = {0}; |
| 108 | + size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp)); |
| 109 | + cliResp[len - 1] = '\0'; |
| 110 | + if (strlen(cliResp)) { |
| 111 | + String sResp(cliResp); |
| 112 | + // cliResp shall be something like: |
| 113 | + // "coap request from fd0c:94df:f1ae:b39a:ec47:ec6d:15e8:804a PUT with payload: 30" |
| 114 | + // payload may be 30 or 31 (HEX) '0' or '1' (ASCII) |
| 115 | + log_d("Msg[%s]", cliResp); |
| 116 | + if (sResp.startsWith("coap request from") && sResp.indexOf("PUT") > 0) { |
| 117 | + char payload = sResp.charAt(sResp.length() - 1); // last character in the payload |
| 118 | + log_i("CoAP PUT [%s]\r\n", payload == '0' ? "OFF" : "ON"); |
| 119 | + if (payload == '0') { |
| 120 | + for (int16_t c = 248; c > 16; c -= 8) { |
| 121 | + neopixelWrite(RGB_BUILTIN, c, c, c); // ramp down |
| 122 | + delay(5); |
| 123 | + } |
| 124 | + neopixelWrite(RGB_BUILTIN, 0, 0, 0); // Lamp Off |
| 125 | + } else { |
| 126 | + for (int16_t c = 16; c < 248; c += 8) { |
| 127 | + neopixelWrite(RGB_BUILTIN, c, c, c); // ramp up |
| 128 | + delay(5); |
| 129 | + } |
| 130 | + neopixelWrite(RGB_BUILTIN, 255, 255, 255); // Lamp On |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void setup() { |
| 137 | + Serial.begin(115200); |
| 138 | + // LED starts RED, indicating not connected to Thread network. |
| 139 | + neopixelWrite(RGB_BUILTIN, 64, 0, 0); |
| 140 | + OThreadCLI.begin(false); // No AutoStart is necessary |
| 141 | + OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response |
| 142 | + setupNode(); |
| 143 | + // LED goes Green when all is ready and Red when failed. |
| 144 | +} |
| 145 | + |
| 146 | +void loop() { |
| 147 | + otCOAPListen(); |
| 148 | + delay(10); |
| 149 | +} |
0 commit comments