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