commands.ino 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #define SER_INPUT_SIZE 70
  2. char serBuffer[SER_INPUT_SIZE + 1]; // Serial Input-Buffer
  3. int serBufferCount; // Anzahl der eingelesenen Zeichen
  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'; // string nullterminieren
  10. #ifdef DEBUG_VERBOSE
  11. Serial.print("serial cmd: '");
  12. Serial.print(serBuffer);
  13. Serial.println("'");
  14. #endif
  15. strlcpy(cmdPayload, serBuffer, 101);
  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, "HEARTBEAT", 9) == 0) {
  26. Serial.println("resetting HEARTBEAT");
  27. mqttLastHeartbeat = millis();
  28. mqttConnected = true;
  29. }
  30. else if (strncmp(cmdPayload, "loadconf", 8) == 0) {
  31. loadConfig();
  32. loadConfig2();
  33. mqttPrepareConnection();
  34. }
  35. else if (strncmp(cmdPayload, "set ", 4) == 0) {
  36. char buf[81];
  37. char setconfCmd[16];
  38. char setconfPayload[62];
  39. int len = strlen(cmdPayload) - 4;
  40. for (int i = 0; i < len; i++) {
  41. if (i < 81) buf[i] = cmdPayload[i + 4];
  42. }
  43. if (len <= 81) buf[len] = '\0';
  44. else buf[81] = '\0';
  45. #ifdef DEBUG_VERBOSE
  46. Serial.print("Buf: ");
  47. Serial.println(buf);
  48. #endif
  49. boolean cmdNoPayload = false;
  50. int setconfCmdLen = 0;
  51. for (int i = 0; i < len; i++) {
  52. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  53. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  54. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  55. cmdNoPayload = true;
  56. break;
  57. }
  58. setconfCmd[i] = buf[i];
  59. setconfCmdLen++;
  60. }
  61. setconfCmd[setconfCmdLen] = '\0';
  62. yield();
  63. if (setconfCmdLen == len) cmdNoPayload = true;
  64. #ifdef DEBUG_VERBOSE
  65. Serial.print("setconfCmd: '");
  66. Serial.print(setconfCmd);
  67. Serial.print("'");
  68. #endif
  69. if ( cmdNoPayload ) {
  70. #ifdef DEBUG_VERBOSE
  71. Serial.println(", no payload, displaying current value");
  72. #endif
  73. getConfig(setconfCmd);
  74. }
  75. else {
  76. #ifdef DEBUG_VERBOSE
  77. Serial.println();
  78. #endif
  79. int setconfPayloadLen = 0;
  80. for (int i = 0; i < len; i++) {
  81. char c = buf[i + setconfCmdLen + 1];
  82. if (c == 0 || c == 10 || c == 13) break; // if \0, LF (10) or CR (13) command parameter is finished
  83. setconfPayload[i] = c;
  84. setconfPayloadLen++;
  85. }
  86. setconfPayload[setconfPayloadLen] = '\0';
  87. #ifdef DEBUG_VERBOSE
  88. Serial.print("setconfPayload: '");
  89. Serial.print(setconfPayload);
  90. Serial.println("'");
  91. #endif
  92. setConfig(setconfCmd, setconfPayload);
  93. }
  94. }
  95. else if (strncmp(cmdPayload, "get ", 4) == 0) {
  96. char buf[81];
  97. char setconfCmd[16];
  98. char setconfPayload[62];
  99. int len = strlen(cmdPayload) - 4;
  100. for (int i = 0; i < len; i++) {
  101. if (i < 81) buf[i] = cmdPayload[i + 4];
  102. }
  103. if (len <= 81) buf[len] = '\0';
  104. else buf[81] = '\0';
  105. #ifdef DEBUG_VERBOSE
  106. Serial.print("Buf: ");
  107. Serial.println(buf);
  108. #endif
  109. boolean cmdNoPayload = false;
  110. int setconfCmdLen = 0;
  111. for (int i = 0; i < len; i++) {
  112. //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing
  113. if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished
  114. else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing
  115. cmdNoPayload = true;
  116. break;
  117. }
  118. setconfCmd[i] = buf[i];
  119. setconfCmdLen++;
  120. }
  121. setconfCmd[setconfCmdLen] = '\0';
  122. yield();
  123. #ifdef DEBUG_VERBOSE
  124. Serial.print("setconfCmd: '");
  125. Serial.print(setconfCmd);
  126. Serial.println("'");
  127. #endif
  128. getConfig(setconfCmd);
  129. }
  130. else if (strncmp(cmdPayload, "restart", 7) == 0) {
  131. Serial.print("restarting...");
  132. delay(100);
  133. ESP.restart();
  134. }
  135. else if (strncmp(cmdPayload, "save", 4) == 0) {
  136. saveConfig();
  137. yield();
  138. saveConfig2();
  139. yield();
  140. saveSetTemp();
  141. saveHeatingMode();
  142. //Serial.println("saved config to SPIFFS");
  143. sendStatus("saved config to SPIFFS");
  144. Serial.println("reloading config to check...");
  145. loadConfig();
  146. yield();
  147. loadConfig2();
  148. yield();
  149. }
  150. else if (strncmp(cmdPayload, "getconf", 7) == 0) {
  151. printConfig();
  152. printConfig2();
  153. }
  154. else if (strncmp(cmdPayload, "delconf", 7) == 0) {
  155. deleteConfig();
  156. }
  157. cmdInQueue = false;
  158. }
  159. }