commands.ino 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #define SER_INPUT_SIZE 70
  2. char serBuffer[SER_INPUT_SIZE + 1];
  3. int serBufferCount;
  4. void serialEvent() {
  5. char ch = Serial.read();
  6. serBuffer[serBufferCount] = ch;
  7. serBufferCount++;
  8. if (ch == 13 || ch == 10) { // ASCII code 13 = "CR", 10 = "LF"
  9. serBuffer[serBufferCount - 1] = '\0';
  10. #ifdef DEBUG_VERBOSE
  11. Serial.print("serial cmd: '");
  12. Serial.print(serBuffer);
  13. Serial.println("'");
  14. #endif
  15. strlcpy(cmdPayload, serBuffer, sizeof(cmdPayload));
  16. cmdInQueue = true;
  17. evalCmd();
  18. serBufferCount = 0;
  19. }
  20. }
  21. void evalCmd() {
  22. if (cmdInQueue) {
  23. //Serial.print("cmdPayload: ");
  24. //Serial.println(cmdPayload);
  25. if (strncmp(cmdPayload, "loadconf", 8) == 0) {
  26. loadConf_all();
  27. }
  28. else if (strncmp(cmdPayload, "set ", 4) == 0) {
  29. char buf[81];
  30. char setconfCmd[16];
  31. uint8_t len = strlen(cmdPayload) - 4;
  32. for (unsigned char i = 0; i < len; i++) {
  33. if (i < (sizeof(buf)-1)) buf[i] = cmdPayload[i + 4];
  34. }
  35. if (len <= (sizeof(buf)-1)) buf[len] = '\0';
  36. else buf[(sizeof(buf)-1)] = '\0';
  37. #ifdef DEBUG_VERBOSE
  38. Serial.print("Buf: ");
  39. Serial.println(buf);
  40. #endif
  41. bool cmdNoPayload = false;
  42. uint8_t setconfCmdLen = 0;
  43. for (unsigned char i = 0; i < len; i++) {
  44. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  45. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  46. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  47. cmdNoPayload = true;
  48. break;
  49. }
  50. setconfCmd[i] = buf[i];
  51. setconfCmdLen++;
  52. }
  53. setconfCmd[setconfCmdLen] = '\0';
  54. yield();
  55. if (setconfCmdLen == len) cmdNoPayload = true;
  56. #ifdef DEBUG_VERBOSE
  57. Serial.print("setconfCmd: '");
  58. Serial.print(setconfCmd);
  59. Serial.print("'");
  60. #endif
  61. if ( cmdNoPayload ) {
  62. #ifdef DEBUG_VERBOSE
  63. Serial.println(", no payload, displaying current value");
  64. #endif
  65. //getConfig(setconfCmd);
  66. }
  67. else {
  68. char setconfPayload[62];
  69. #ifdef DEBUG_VERBOSE
  70. Serial.println();
  71. #endif
  72. int setconfPayloadLen = 0;
  73. for (int i = 0; i < len; i++) {
  74. char c = buf[i + setconfCmdLen + 1];
  75. if (c == 0 || c == 10 || c == 13) break; // if \0, LF (10) or CR (13) command parameter is finished
  76. setconfPayload[i] = c;
  77. setconfPayloadLen++;
  78. }
  79. setconfPayload[setconfPayloadLen] = '\0';
  80. #ifdef DEBUG_VERBOSE
  81. Serial.print("setconfPayload: '");
  82. Serial.print(setconfPayload);
  83. Serial.println("'");
  84. #endif
  85. setConfig(setconfCmd, setconfPayload);
  86. }
  87. }
  88. else if (strncmp(cmdPayload, "get ", 4) == 0) {
  89. char buf[81];
  90. char setconfCmd[16];
  91. uint8_t len = strlen(cmdPayload) - 4;
  92. for (int i = 0; i < len; i++) {
  93. if (i < 81) buf[i] = cmdPayload[i + 4];
  94. }
  95. if (len <= (sizeof(buf)-1)) buf[len] = '\0';
  96. else buf[(sizeof(buf)-1)] = '\0';
  97. #ifdef DEBUG_VERBOSE
  98. Serial.print("Buf: ");
  99. Serial.println(buf);
  100. #endif
  101. uint8_t setconfCmdLen = 0;
  102. for (int i = 0; i < len; i++) {
  103. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  104. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  105. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  106. break;
  107. }
  108. setconfCmd[i] = buf[i];
  109. setconfCmdLen++;
  110. }
  111. setconfCmd[setconfCmdLen] = '\0';
  112. yield();
  113. #ifdef DEBUG_VERBOSE
  114. Serial.print("setconfCmd: '");
  115. Serial.print(setconfCmd);
  116. Serial.println("'");
  117. #endif
  118. //getConfig(setconfCmd);
  119. }
  120. else if (strncmp(cmdPayload, "restart", 7) == 0) {
  121. Serial.print("restarting...");
  122. delay(100);
  123. ESP.restart();
  124. }
  125. else if (strncmp(cmdPayload, "save", 4) == 0) {
  126. //saveConfig();
  127. //saveConfig2();
  128. saveConfigDevWiFi();
  129. yield();
  130. saveConfigWeb();
  131. yield();
  132. saveConfigMqtt();
  133. yield();
  134. saveConfigBas();
  135. yield();
  136. saveConfigAdv();
  137. yield();
  138. saveConfigAdd();
  139. yield();
  140. saveSetTemp();
  141. yield();
  142. saveSetTempLow();
  143. yield();
  144. saveSetTempLow2();
  145. yield();
  146. saveHeatingMode();
  147. yield();
  148. //Serial.println("saved config to SPIFFS");
  149. sendLog("saved config to SPIFFS");
  150. //Serial.println("reloading config to check...");
  151. //loadConfig();
  152. //yield();
  153. //loadConfig2();
  154. //yield();
  155. }
  156. else if (strncmp(cmdPayload, "getconf", 7) == 0) {
  157. //printConfig();
  158. //printConfig2();
  159. printConfigWeb();
  160. printConfigMqtt();
  161. }
  162. else if (strncmp(cmdPayload, "delconf", 7) == 0) {
  163. deleteConfig();
  164. }
  165. cmdInQueue = false;
  166. }
  167. }