|
| 1 | +#include "OThreadCLI.h" |
| 2 | +#include "OThreadCLI_Util.h" |
| 3 | + |
| 4 | +#define USER_BUTTON 9 // C6/H2 Boot button |
| 5 | +#define OT_CHANNEL "24" |
| 6 | +#define OT_NETWORK_KEY "00112233445566778899aabbccddeeff" |
| 7 | + |
| 8 | +const char *otSetupChild[] = { |
| 9 | + // clear/disable all |
| 10 | + "coap", "stop", |
| 11 | + "thread", "stop", |
| 12 | + "ifconfig", "down", |
| 13 | + "dataset", "clear", |
| 14 | + // set dataset |
| 15 | + "dataset channel", OT_CHANNEL, |
| 16 | + "dataset networkkey", OT_NETWORK_KEY, |
| 17 | + "dataset", "commit active", |
| 18 | + // network start |
| 19 | + "ifconfig", "up", |
| 20 | + "thread", "start" |
| 21 | +}; |
| 22 | + |
| 23 | +const char *otCoapSwitch[] = { |
| 24 | + // start and create a CoAP resource |
| 25 | + "coap", "start", |
| 26 | +}; |
| 27 | + |
| 28 | +bool otDeviceSetup(const char **otSetupCmds, uint8_t nCmds1, const char **otCoapCmds, uint8_t nCmds2, ot_device_role_t expectedRole1, ot_device_role_t expectedRole2) { |
| 29 | + Serial.println("Starting OpenThread."); |
| 30 | + Serial.println("Running as Switch - use the BOOT button to toggle the other C6/H2 as a Lamp"); |
| 31 | + uint8_t i; |
| 32 | + for (i = 0; i < nCmds1; i++) { |
| 33 | + if (!otExecCommand(otSetupCmds[i * 2], otSetupCmds[i * 2 + 1])) { |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + if (i != nCmds1) { |
| 38 | + log_e("Sorry, OpenThread Network setup failed!"); |
| 39 | + neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! |
| 40 | + return false; |
| 41 | + } |
| 42 | + Serial.println("OpenThread started.\r\nWaiting for activating correct Device Role."); |
| 43 | + // wait for the expected Device Role to start |
| 44 | + uint8_t tries = 24; // 24 x 2.5 sec = 1 min |
| 45 | + while (tries && getOtDeviceRole() != expectedRole1 && getOtDeviceRole() != expectedRole2) { |
| 46 | + Serial.print("."); |
| 47 | + delay(2500); |
| 48 | + tries--; |
| 49 | + } |
| 50 | + Serial.println(); |
| 51 | + if (!tries) { |
| 52 | + log_e("Sorry, Device Role failed by timeout! Current Role: %s.", getStringOtDeviceRole()); |
| 53 | + neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! |
| 54 | + return false; |
| 55 | + } |
| 56 | + Serial.printf("Device is %s.\r\n", getStringOtDeviceRole()); |
| 57 | + for (i = 0; i < nCmds2; i++) { |
| 58 | + if (!otExecCommand(otCoapCmds[i * 2], otCoapCmds[i * 2 + 1])) { |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + if (i != nCmds2) { |
| 63 | + log_e("Sorry, OpenThread CoAP setup failed!"); |
| 64 | + neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... failed! |
| 65 | + return false; |
| 66 | + } |
| 67 | + Serial.println("OpenThread setup done. Node is ready."); |
| 68 | + // all fine! LED goes Blue |
| 69 | + neopixelWrite(RGB_BUILTIN, 0, 0, 64); // BLUE ... Swtich is ready! |
| 70 | + return true; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +void setupNode() { |
| 75 | + // tries to set the Thread Network node and only returns when succeded |
| 76 | + bool startedCorrectly = false; |
| 77 | + while (!startedCorrectly) { |
| 78 | + startedCorrectly |= otDeviceSetup(otSetupChild, sizeof(otSetupChild) / sizeof(char *) / 2, |
| 79 | + otCoapSwitch, sizeof(otCoapSwitch) / sizeof(char *) / 2, |
| 80 | + OT_ROLE_CHILD, OT_ROLE_ROUTER); |
| 81 | + if (!startedCorrectly) { |
| 82 | + Serial.println("Setup Failed...\r\nTrying again..."); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +// Sends the CoAP frame to the Lamp node |
| 89 | +bool otCoapPUT(bool lampState) { |
| 90 | + bool gotDone = false, gotConfirmation = false; |
| 91 | + char coapMsg[] = "coap put ff05::abcd Lamp con 0"; |
| 92 | + |
| 93 | + if (lampState) { |
| 94 | + coapMsg[strlen(coapMsg) - 1] = '1'; |
| 95 | + } |
| 96 | + OThreadCLI.println(coapMsg); |
| 97 | + log_d("Send CLI CMD:[%s]", coapMsg); |
| 98 | + |
| 99 | + char cliResp[256]; |
| 100 | + // waits for the CoAP confirmation and Done message for about 5 seconds |
| 101 | + // timeout is based on Stream::setTimeout() |
| 102 | + uint8_t tries = 5; |
| 103 | + *cliResp = '\0'; |
| 104 | + while (tries && !(gotDone && gotConfirmation)) { |
| 105 | + size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp)); |
| 106 | + cliResp[len] = '\0'; |
| 107 | + log_d("Try[%d]::MSG[%s]", tries, cliResp); |
| 108 | + if (strlen(cliResp)) { |
| 109 | + log_d("%s", cliResp); |
| 110 | + if (!strncmp(cliResp, "coap response from", 18)) { |
| 111 | + gotConfirmation = true; |
| 112 | + } |
| 113 | + if (!strncmp(cliResp, "Done", 4)) { |
| 114 | + gotDone = true; |
| 115 | + } |
| 116 | + } |
| 117 | + tries--; |
| 118 | + } |
| 119 | + if (gotDone && gotConfirmation) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + return false; |
| 123 | +} |
| 124 | + |
| 125 | +// this fucntion is used by the Switch mode to check the BOOT Button and send the user action to the Lamp node |
| 126 | +void checkUserButton() { |
| 127 | + static long unsigned int lastPress = 0; |
| 128 | + const long unsigned int debounceTime = 500; |
| 129 | + static bool lastLampState = false; |
| 130 | + |
| 131 | + pinMode(USER_BUTTON, INPUT_PULLUP); // C6/H2 User Button |
| 132 | + if (millis() > lastPress + debounceTime && digitalRead(USER_BUTTON) == LOW) { |
| 133 | + if (otCoapPUT(!lastLampState)) { |
| 134 | + lastLampState = !lastLampState; |
| 135 | + } else { |
| 136 | + // timeout from the CoAP PUT message... restart the node. |
| 137 | + neopixelWrite(RGB_BUILTIN, 255, 0, 0); // RED ... something failed! |
| 138 | + Serial.println("Reseting the Node as Switch... wait."); |
| 139 | + // start over... |
| 140 | + setupNode(); |
| 141 | + } |
| 142 | + lastPress = millis(); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +void setup() { |
| 147 | + Serial.begin(115200); |
| 148 | + // LED starts RED, indicating not connected to Thread network. |
| 149 | + neopixelWrite(RGB_BUILTIN, 64, 0, 0); |
| 150 | + OThreadCLI.begin(false); // No AutoStart is necessary |
| 151 | + OThreadCLI.setTimeout(250); // waits 250ms for the OpenThread CLI response |
| 152 | + setupNode(); |
| 153 | +} |
| 154 | + |
| 155 | +void loop() { |
| 156 | + checkUserButton(); |
| 157 | + delay(10); |
| 158 | +} |
0 commit comments