#include #include #include #include "base.h" #include "mqtt.h" #include "wifi.h" #include "config.h" char consoleBuffer[64] = ""; char *consoleBufferW = consoleBuffer; void _debug(); void _reboot(); void _usage(); void _info(); void _wifi(); void _mqtt(); void _setConfigString(const char *name, bool allowEmpty); const char *getFlashChipMode(); void consoleLoop() { uint8_t i = 0; while (Serial.available() > 0 && i++ < 100) { const char symbol = static_cast(Serial.read()); if (symbol == '\n' || symbol == '\r') { if (*consoleBuffer != 0) { Serial.print('\n'); info("CONSOLE > %s", consoleBuffer); consoleHandle(consoleBuffer); consoleBufferW = consoleBuffer; *consoleBufferW = 0; } } else if (symbol == '\b') { if (consoleBufferW > consoleBuffer) { consoleBufferW--; *consoleBufferW = 0; Serial.print("\b \b"); } } else { if (consoleBufferW < consoleBuffer + sizeof consoleBuffer - 1) { *consoleBufferW = symbol; consoleBufferW++; *consoleBufferW = 0; Serial.print("\r"); Serial.print(consoleBuffer); } else { Serial.print("\nCONSOLE BUFFER OVERFLOW\n"); Serial.print(consoleBuffer); } } } } void consoleHandle(char *cmd) { char *first = strtok(cmd, " "); if (strcmp(first, "help") == 0) { _usage(); } else if (strcmp(first, "wifi") == 0) { _wifi(); } else if (strcmp(first, "mqtt") == 0) { _mqtt(); } else if (strcmp(first, "info") == 0) { _info(); } else if (strcmp(first, "reboot") == 0) { _reboot(); } else if (strcmp(first, "debug") == 0) { _debug(); } else if (strcmp(first, "config reset") == 0) { configReset(); } else if (!patrix_command((char *) cmd)) { info("Unknown command: %s", cmd); } } void _usage() { info("help"); info("info"); info("debug"); info("reboot"); info("config reset"); info("wifi reconnect"); info("wifi ssid "); info("wifi pkey "); info("mqtt reconnect"); info("mqtt host "); } void _debug() { setDebugEnabled(!isDebugEnabled()); info("DEBUG: %s", isDebugEnabled() ? "ON" : "OFF"); } void _reboot() { info("Rebooting..."); delay(500); yield(); ESP.restart(); } void _mqtt() { char *sub = strtok(nullptr, " "); if (strcmp(sub, "reconnect") == 0) { info("Reconnecting MQTT..."); mqttDisconnect(); } else if (strcmp(sub, "host") == 0) { _setConfigString("MQTT_HOST", false); } } void _wifi() { char *sub = strtok(nullptr, " "); if (strcmp(sub, "reconnect") == 0) { info("Reconnecting WiFi..."); wifiConnect(); } else if (strcmp(sub, "ssid") == 0) { _setConfigString("WIFI_SSID", false); } else if (strcmp(sub, "pkey") == 0) { _setConfigString("WIFI_PKEY", false); } } void _setConfigString(const char *name, bool allowEmpty) { char *value = strtok(nullptr, ""); if (!allowEmpty && strcmp(value, "") == 0) { error(R"(Value for "%s" cannot be empty!")", name); return; } if (!configPutString(name, value)) { error(R"(Failed to persist "%s" = "%s")", name, value); return; } info(R"(Set "%s" to "%s")", name, value); } void _info() { uint64_t millis = getUptimeMillis(); uint64_t seconds = millis / 1000; uint32_t minutes = seconds / 60; uint32_t hours = minutes / 60; uint16_t days = hours / 24; char datetime[26]; getDateTime(datetime, sizeof datetime); info("WiFi:"); info(" hostname: %s", HOSTNAME); info(" mac: %s", WiFi.macAddress().c_str()); info(" ip: %s", WiFi.localIP().toString().c_str()); info(" gateway: %s", WiFi.gatewayIP().toString().c_str()); info(" ssid: %s", WiFi.SSID().c_str()); info(" bssid: %s", WiFi.BSSIDstr().c_str()); info(" rssi: %d", WiFi.RSSI()); info("Time:"); info(" uptime: %dd %2dh %2dm %2ds", days, hours % 24, minutes % 60, seconds % 60); info(" sys: %s", datetime); info("Chip:"); #ifdef ESP32 info(" model: %s", ESP.getChipModel()); info(" cores: %d", ESP.getChipCores()); #endif info(" freq: %d", ESP.getCpuFreqMHz()); info("Flash:"); info(" mode: %s", getFlashChipMode()); info(" size: %d", ESP.getFlashChipSize()); info(" speed: %d", ESP.getFlashChipSpeed()); info("Heap:"); info(" free: %d", ESP.getFreeHeap()); #ifdef ESP32 info(" size: %d", ESP.getHeapSize()); info(" minFree: %d", ESP.getMinFreeHeap()); info(" maxAlloc: %d", ESP.getMaxAllocHeap()); #endif info("PS RAM:"); #ifdef ESP32 info(" free: %d", ESP.getFreePsram()); info(" size: %d", ESP.getPsramSize()); info(" minFree: %d", ESP.getMinFreePsram()); info(" maxAlloc: %d", ESP.getMaxAllocPsram()); #endif info("SDK:"); info(" free: %d", ESP.getFreeSketchSpace()); info(" size: %d", ESP.getSketchSize()); info(" MD5: %s", ESP.getSketchMD5().c_str()); info("SDK:"); info(" version: %s", ESP.getSdkVersion()); } const char *getFlashChipMode() { switch (ESP.getFlashChipMode()) { case FM_QIO: return "QIO"; case FM_QOUT: return "QOUT"; case FM_DIO: return "DIO"; case FM_DOUT: return "DOUT"; #ifdef ESP32 case FM_FAST_READ: return "FAST_READ"; case FM_SLOW_READ: return "SLOW_READ"; #endif case FM_UNKNOWN: return "UNKNOWN"; default: return "[???]"; } }