#define SER_INPUT_SIZE 70 char serBuffer[SER_INPUT_SIZE + 1]; int serBufferCount; void serialEvent() { char ch = Serial.read(); serBuffer[serBufferCount] = ch; serBufferCount++; if (ch == 13 || ch == 10) { // ASCII code 13 = "CR", 10 = "LF" serBuffer[serBufferCount - 1] = '\0'; #ifdef DEBUG_VERBOSE Serial.print("serial cmd: '"); Serial.print(serBuffer); Serial.println("'"); #endif strlcpy(cmdPayload, serBuffer, sizeof(cmdPayload)); cmdInQueue = true; evalCmd(); serBufferCount = 0; } } void evalCmd() { if (cmdInQueue) { //Serial.print("cmdPayload: "); //Serial.println(cmdPayload); char cmdPayload_lower[sizeof(cmdPayload)]; strlcpy(cmdPayload_lower, cmdPayload, sizeof(cmdPayload_lower)); strlwr(cmdPayload_lower); if (strncmp(cmdPayload_lower, "loadconf", 8) == 0) { sendLog(F("loading config...")); loadConf_all(); } else if (strncmp(cmdPayload_lower, "set ", 4) == 0) { char buf[81]; char setconfCmd[16]; uint8_t len = strlen(cmdPayload) - 4; for (unsigned char i = 0; i < len; i++) { if (i < (sizeof(buf) - 1)) buf[i] = cmdPayload[i + 4]; } if (len <= (sizeof(buf) - 1)) buf[len] = '\0'; else buf[(sizeof(buf) - 1)] = '\0'; #ifdef DEBUG_VERBOSE Serial.print("Buf: "); Serial.println(buf); #endif bool cmdNoPayload = false; uint8_t setconfCmdLen = 0; for (unsigned char i = 0; i < len; i++) { //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing cmdNoPayload = true; break; } setconfCmd[i] = buf[i]; setconfCmdLen++; } setconfCmd[setconfCmdLen] = '\0'; yield(); if (setconfCmdLen == len) cmdNoPayload = true; #ifdef DEBUG_VERBOSE Serial.print("setconfCmd: '"); Serial.print(setconfCmd); Serial.print("'"); #endif if (cmdNoPayload) { #ifdef DEBUG_VERBOSE Serial.println(", no payload, displaying current value"); #endif //getConfig(setconfCmd); cmdPrintHelp(); } else { char setconfPayload[62]; #ifdef DEBUG_VERBOSE Serial.println(); #endif int setconfPayloadLen = 0; for (int i = 0; i < len; i++) { char c = buf[i + setconfCmdLen + 1]; if (c == 0 || c == 10 || c == 13) break; // if \0, LF (10) or CR (13) command parameter is finished setconfPayload[i] = c; setconfPayloadLen++; } setconfPayload[setconfPayloadLen] = '\0'; #ifdef DEBUG_VERBOSE Serial.print("setconfPayload: '"); Serial.print(setconfPayload); Serial.println("'"); #endif char bufg[100]; sprintf(bufg, "set '%s'->'%s'", setconfCmd, setconfPayload); sendLog(bufg, LOGLEVEL_VERBOSE); setConfig(setconfCmd, setconfPayload); } } else if (strncmp(cmdPayload_lower, "get ", 4) == 0) { char buf[81]; char setconfCmd[16]; uint8_t len = strlen(cmdPayload) - 4; for (int i = 0; i < len; i++) { if (i < 81) buf[i] = cmdPayload[i + 4]; } if (len <= (sizeof(buf) - 1)) buf[len] = '\0'; else buf[(sizeof(buf) - 1)] = '\0'; #ifdef DEBUG_VERBOSE Serial.print("Buf: "); Serial.println(buf); #endif uint8_t setconfCmdLen = 0; for (int i = 0; i < len; i++) { //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing break; } setconfCmd[i] = buf[i]; setconfCmdLen++; } setconfCmd[setconfCmdLen] = '\0'; yield(); #ifdef DEBUG_VERBOSE Serial.print("setconfCmd: '"); Serial.print(setconfCmd); Serial.println("'"); #endif char bufg[50]; sprintf(bufg, "get '%s'", setconfCmd); sendLog(bufg); getConfig(setconfCmd); } /*else if (strncmp(cmdPayload, "enc ", 4) == 0) { char buf[81]; char setconfCmd[16]; uint8_t len = strlen(cmdPayload) - 4; for (int i = 0; i < len; i++) { if (i < 81) buf[i] = cmdPayload[i + 4]; } if (len <= (sizeof(buf)-1)) buf[len] = '\0'; else buf[(sizeof(buf)-1)] = '\0'; #ifdef DEBUG_VERBOSE Serial.print("Buf: "); Serial.println(buf); #endif uint8_t setconfCmdLen = 0; for (int i = 0; i < len; i++) { //if (buf[i] == 32 || buf[i] == '\0') break; // if SPACE command name is finished, if \0 command parameter is missing if (buf[i] == 32 || buf[i] == 61 || buf[i] == 58) break; // if SPACE(32) or =(61) or :(58) command name is finished else if (buf[i] == 0 || buf[i] == 10 || buf[i] == 13) { // if \0, LF (10) or CR (13) command parameter is missing break; } setconfCmd[i] = buf[i]; setconfCmdLen++; } setconfCmd[setconfCmdLen] = '\0'; yield(); #ifdef DEBUG_VERBOSE Serial.print("setconfCmd: '"); Serial.print(setconfCmd); Serial.println("'"); #endif char bufg[50]; sprintf(bufg, "enc '%s'", setconfCmd); sendLog(bufg); sprintf(bufg, "%s", XORENC(setconfCmd, encKey)); sendLog(bufg); //getConfig(setconfCmd); }*/ else if (strncmp(cmdPayload_lower, "restart", 7) == 0) { sendLog(F("restarting...")); restart(); } else if (strncmp(cmdPayload_lower, "sysinfo", 7) == 0) { logSysdata(); } else if (strncmp(cmdPayload_lower, "debugmode 1", 11) == 0) { sendLog(F("debugmode on")); sysInfoEverySecond = true; } else if (strncmp(cmdPayload_lower, "debugmode 0", 11) == 0) { sendLog(F("debugmode off")); sysInfoEverySecond = false; } #ifdef ENABLE_FEATURE_NTP_TIME else if (strncmp(cmdPayload_lower, "date", 7) == 0) { printDate(); } #endif #ifdef ENABLE_FEATURE_NTP_TIME else if (strncmp(cmdPayload_lower, "syncclock", 7) == 0) { syncClock(true); } #endif else if (strncmp(cmdPayload_lower, "clearcreds", 10) == 0) { confClearCredentials(); } else if (strncmp(cmdPayload_lower, "clearwifi", 10) == 0) { confClearWiFiCredentials(); } else if (strncmp(cmdPayload_lower, "saveconf", 8) == 0) { saveConfig_all(); } else if (strncmp(cmdPayload_lower, "savevalues", 10) == 0) { saveValues(); } else if (strncmp(cmdPayload_lower, "encrypt", 10) == 0) { confEncrypt(); } else if (strncmp(cmdPayload_lower, "decrypt", 10) == 0) { confDecrypt(); } else if (strncmp(cmdPayload_lower, "ls", 2) == 0) { SPIFFS_listFiles(); } else if (strncmp(cmdPayload_lower, "ipcfg", 5) == 0) { printIpcfg(); } else if (strncmp(cmdPayload_lower, "help", 4) == 0) { cmdPrintHelp(); } else if (strncmp(cmdPayload_lower, "mqttreset", 9) == 0) { mqttResetConnection(); } else if (strncmp(cmdPayload_lower, "mqttinfo", 8) == 0) { mqttPublishStatus(); } else if (strncmp(cmdPayload_lower, "getconf", 7) == 0) { sendLog(F("CONF: listing all conf parameters:")); getConfig((char *)"confDevWiFi"); getConfig((char *)"confWeb"); getConfig((char *)"confMqtt"); getConfig((char *)"confBas"); getConfig((char *)"confAdv"); getConfig((char *)"confAdd"); getConfig((char *)"confTime"); getConfig((char *)"confLog"); sendLog(F("------")); } else if (strncmp(cmdPayload_lower, "delconf", 7) == 0) { sendLog(F("deleting configuration...")); deleteConfig(); } cmdInQueue = false; } } void cmdPrintHelp() { sendLog(F("valid commands:")); sendLog(F(" sysinfo show uptime, heap usage...")); sendLog(F(" date show current date/time")); sendLog(F(" syncclock perform NTP time sync now")); sendLog(F(" debugmode [0/1]")); sendLog(F(" loadconf load saved configuration")); sendLog(F(" getconf list all conf parameters")); sendLog(F(" get [PARAM] show conf value")); sendLog(F(" get values list current saved values")); sendLog(F(" set [PARAM] [VALUE] (1 blank after PARAM to clear)")); sendLog(F(" saveconf save all configurations")); sendLog(F(" savevalues save set values")); sendLog(F(" clearcreds delete all passwords")); sendLog(F(" clearwifi delete WiFi configurations")); sendLog(F(" mqttinfo MQTT connection status")); sendLog(F(" mqttreset reset MQTT connection")); sendLog(F(" encrypt switch on encrypted stored passwords")); sendLog(F(" decrypt switch off encrypted stored passwords")); sendLog(F(" WARNING: deletes all currently saved credentials")); sendLog(F(" ls list files on SPIFFS")); sendLog(F(" delconf delete ALL configuration")); sendLog(F(" restart")); }