added hostname, some formatting

This commit is contained in:
proddy
2020-10-03 16:32:52 +02:00
parent 402a80f435
commit 7020b41f55
2 changed files with 48 additions and 35 deletions

View File

@@ -30,13 +30,14 @@ uuid::syslog::SyslogService System::syslog_;
#endif #endif
// init statics // init statics
uint32_t System::heap_start_ = 0; uint32_t System::heap_start_ = 0;
int System::reset_counter_ = 0; int System::reset_counter_ = 0;
bool System::upload_status_ = false; bool System::upload_status_ = false;
bool System::hide_led_ = false; bool System::hide_led_ = false;
uint8_t System::led_gpio_ = 0; uint8_t System::led_gpio_ = 0;
uint16_t System::analog_ = 0; uint16_t System::analog_ = 0;
bool System::analog_enabled_ = false; bool System::analog_enabled_ = false;
std::string System::hostname_;
// send on/off to a gpio pin // send on/off to a gpio pin
// value: true = HIGH, false = LOW // value: true = HIGH, false = LOW
@@ -168,6 +169,8 @@ void System::init() {
analog_enabled_ = settings.analog_enabled; analog_enabled_ = settings.analog_enabled;
}); });
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & settings) { hostname(settings.hostname.c_str()); });
EMSESP::init_tx(); // start UART EMSESP::init_tx(); // start UART
} }
@@ -215,7 +218,7 @@ void System::loop() {
uint32_t currentMillis = uuid::get_uptime(); uint32_t currentMillis = uuid::get_uptime();
if (!last_heartbeat_ || (currentMillis - last_heartbeat_ > SYSTEM_HEARTBEAT_INTERVAL)) { if (!last_heartbeat_ || (currentMillis - last_heartbeat_ > SYSTEM_HEARTBEAT_INTERVAL)) {
last_heartbeat_ = currentMillis; last_heartbeat_ = currentMillis;
send_heartbeat(); send_heartbeat();
} }
#if defined(ESP8266) #if defined(ESP8266)
@@ -266,6 +269,9 @@ void System::send_heartbeat() {
if (analog_enabled_) { if (analog_enabled_) {
doc["adc"] = analog_; doc["adc"] = analog_;
} }
#if defined(ESP8266)
doc["fragmentation"] = ESP.getHeapFragmentation();
#endif
Mqtt::publish_retain(F("heartbeat"), doc.as<JsonObject>(), false); // send to MQTT with retain off. This will add to MQTT queue. Mqtt::publish_retain(F("heartbeat"), doc.as<JsonObject>(), false); // send to MQTT with retain off. This will add to MQTT queue.
} }
@@ -404,11 +410,6 @@ void System::show_system(uuid::console::Shell & shell) {
shell.printfln(F("Flash chip: 0x%08X (%u bytes)"), ESP.getFlashChipId(), ESP.getFlashChipRealSize()); shell.printfln(F("Flash chip: 0x%08X (%u bytes)"), ESP.getFlashChipId(), ESP.getFlashChipRealSize());
shell.printfln(F("Reset reason: %s"), ESP.getResetReason().c_str()); shell.printfln(F("Reset reason: %s"), ESP.getResetReason().c_str());
shell.printfln(F("Reset info: %s"), ESP.getResetInfo().c_str()); shell.printfln(F("Reset info: %s"), ESP.getResetInfo().c_str());
shell.printfln(F("Free heap: %lu bytes"), (unsigned long)ESP.getFreeHeap());
shell.printfln(F("Free mem: %d %%"), free_mem());
shell.printfln(F("Maximum free block size: %lu bytes"), (unsigned long)ESP.getMaxFreeBlockSize());
shell.printfln(F("Heap fragmentation: %u %%"), ESP.getHeapFragmentation());
shell.printfln(F("Free continuations stack: %lu bytes"), (unsigned long)ESP.getFreeContStack());
#elif defined(ESP32) #elif defined(ESP32)
shell.printfln(F("SDK version: %s"), ESP.getSdkVersion()); shell.printfln(F("SDK version: %s"), ESP.getSdkVersion());
shell.printfln(F("CPU frequency: %u MHz"), ESP.getCpuFreqMHz()); shell.printfln(F("CPU frequency: %u MHz"), ESP.getCpuFreqMHz());
@@ -416,6 +417,11 @@ void System::show_system(uuid::console::Shell & shell) {
shell.printfln(F("Sketch size: %u bytes (%u bytes free)"), ESP.getSketchSize(), ESP.getFreeSketchSpace()); shell.printfln(F("Sketch size: %u bytes (%u bytes free)"), ESP.getSketchSize(), ESP.getFreeSketchSpace());
shell.printfln(F("Free heap: %lu bytes"), (unsigned long)ESP.getFreeHeap()); shell.printfln(F("Free heap: %lu bytes"), (unsigned long)ESP.getFreeHeap());
shell.printfln(F("Free mem: %d %%"), free_mem()); shell.printfln(F("Free mem: %d %%"), free_mem());
#if defined(ESP8266)
shell.printfln(F("Heap fragmentation: %u %%"), ESP.getHeapFragmentation());
shell.printfln(F("Maximum free block size: %lu bytes"), (unsigned long)ESP.getMaxFreeBlockSize());
shell.printfln(F("Free continuations stack: %lu bytes"), (unsigned long)ESP.getFreeContStack());
#endif
shell.println(); shell.println();
switch (WiFi.status()) { switch (WiFi.status()) {
@@ -541,7 +547,7 @@ void System::console_commands(Shell & shell, unsigned int context) {
EMSESPShell::commands->add_command(ShellContext::SYSTEM, EMSESPShell::commands->add_command(ShellContext::SYSTEM,
CommandFlags::USER, CommandFlags::USER,
flash_string_vector{F_(show), F_(system)}, flash_string_vector{F_(show)},
[=](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) { [=](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) {
show_system(shell); show_system(shell);
shell.println(); shell.println();
@@ -635,7 +641,6 @@ void System::console_commands(Shell & shell, unsigned int context) {
// the logic is bit abnormal (loading both filesystems and testing) but this was the only way I could get it to work reliably // the logic is bit abnormal (loading both filesystems and testing) but this was the only way I could get it to work reliably
bool System::check_upgrade() { bool System::check_upgrade() {
#if defined(ESP8266) #if defined(ESP8266)
LittleFSConfig l_cfg; LittleFSConfig l_cfg;
l_cfg.setAutoFormat(false); l_cfg.setAutoFormat(false);
LittleFS.setConfig(l_cfg); // do not auto format if it can't find LittleFS LittleFS.setConfig(l_cfg); // do not auto format if it can't find LittleFS
@@ -733,18 +738,18 @@ bool System::check_upgrade() {
EMSESP::esp8266React.getMqttSettingsService()->update( EMSESP::esp8266React.getMqttSettingsService()->update(
[&](MqttSettings & mqttSettings) { [&](MqttSettings & mqttSettings) {
mqttSettings.host = mqtt["ip"] | FACTORY_MQTT_HOST; mqttSettings.host = mqtt["ip"] | FACTORY_MQTT_HOST;
mqttSettings.mqtt_format = (mqtt["nestedjson"] ? Mqtt::Format::NESTED : Mqtt::Format::SINGLE); mqttSettings.mqtt_format = (mqtt["nestedjson"] ? Mqtt::Format::NESTED : Mqtt::Format::SINGLE);
mqttSettings.mqtt_qos = mqtt["qos"] | 0; mqttSettings.mqtt_qos = mqtt["qos"] | 0;
mqttSettings.mqtt_retain = mqtt["retain"]; mqttSettings.mqtt_retain = mqtt["retain"];
mqttSettings.username = mqtt["user"] | ""; mqttSettings.username = mqtt["user"] | "";
mqttSettings.password = mqtt["password"] | ""; mqttSettings.password = mqtt["password"] | "";
mqttSettings.port = mqtt["port"] | FACTORY_MQTT_PORT; mqttSettings.port = mqtt["port"] | FACTORY_MQTT_PORT;
mqttSettings.clientId = FACTORY_MQTT_CLIENT_ID; mqttSettings.clientId = FACTORY_MQTT_CLIENT_ID;
mqttSettings.enabled = mqtt["enabled"]; mqttSettings.enabled = mqtt["enabled"];
mqttSettings.keepAlive = FACTORY_MQTT_KEEP_ALIVE; mqttSettings.keepAlive = FACTORY_MQTT_KEEP_ALIVE;
mqttSettings.cleanSession = FACTORY_MQTT_CLEAN_SESSION; mqttSettings.cleanSession = FACTORY_MQTT_CLEAN_SESSION;
mqttSettings.maxTopicLength = FACTORY_MQTT_MAX_TOPIC_LENGTH; mqttSettings.maxTopicLength = FACTORY_MQTT_MAX_TOPIC_LENGTH;
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
}, },
@@ -837,7 +842,6 @@ bool System::command_info(const char * value, const int8_t id, JsonObject & outp
#ifdef EMSESP_STANDALONE #ifdef EMSESP_STANDALONE
output["test"] = "testing"; output["test"] = "testing";
#else #else
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & settings) { EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & settings) {
char s[7]; char s[7];
JsonObject node = output.createNestedObject("WIFI"); JsonObject node = output.createNestedObject("WIFI");

View File

@@ -63,6 +63,14 @@ class System {
void syslog_init(); void syslog_init();
void send_heartbeat(); void send_heartbeat();
static std::string hostname() {
return hostname_;
}
static void hostname(std::string hostname) {
hostname_ = hostname;
}
private: private:
static uuid::log::Logger logger_; static uuid::log::Logger logger_;
@@ -89,13 +97,14 @@ class System {
static void wifi_reconnect(); static void wifi_reconnect();
static int8_t wifi_quality(); static int8_t wifi_quality();
bool system_healthy_ = false; bool system_healthy_ = false;
uint32_t led_flash_speed_ = LED_WARNING_BLINK_FAST; // default boot flashes quickly uint32_t led_flash_speed_ = LED_WARNING_BLINK_FAST; // default boot flashes quickly
static uint32_t heap_start_; static uint32_t heap_start_;
static int reset_counter_; static int reset_counter_;
uint32_t last_heartbeat_ = 0; uint32_t last_heartbeat_ = 0;
static bool upload_status_; // true if we're in the middle of a OTA firmware upload static bool upload_status_; // true if we're in the middle of a OTA firmware upload
static uint16_t analog_; static uint16_t analog_;
static std::string hostname_;
// settings // settings
static bool hide_led_; static bool hide_led_;