diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index 4084dda63..67ce8b1f6 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -125,8 +125,7 @@ void MqttSettingsService::configureMqtt() { _mqttClient.disconnect(); // only connect if WiFi is connected and MQTT is enabled - if (_state.enabled && (WiFi.isConnected() || ETH.linkUp())) { - // Serial.println(F("Connecting to MQTT...")); + if (_state.enabled && emsesp::EMSESP::system_.network_connected()) { _mqttClient.setServer(retainCstr(_state.host.c_str(), &_retainedHost), _state.port); if (_state.username.length() > 0) { _mqttClient.setCredentials(retainCstr(_state.username.c_str(), &_retainedUsername), retainCstr(_state.password.length() > 0 ? _state.password.c_str() : nullptr, &_retainedPassword)); diff --git a/src/WebStatusService.cpp b/src/WebStatusService.cpp index a212830bd..0ea0c67a2 100644 --- a/src/WebStatusService.cpp +++ b/src/WebStatusService.cpp @@ -41,6 +41,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { #endif EMSESP::system_.wifi_tweak(); EMSESP::system_.send_heartbeat(); + EMSESP::system_.syslog_start(); break; case SYSTEM_EVENT_ETH_START: @@ -55,6 +56,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { EMSESP::logger().info(F("Ethernet Connected with IP=%s, speed %d Mbps"), ETH.localIP().toString().c_str(), ETH.linkSpeed()); #endif EMSESP::system_.send_heartbeat(); + EMSESP::system_.syslog_start(); EMSESP::system_.ethernet_connected(true); } break; diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 93fe10621..fff282132 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -611,7 +611,7 @@ void Mqtt::on_connect() { doc["version"] = EMSESP_APP_VERSION; #ifndef EMSESP_STANDALONE - if (ETH.linkUp()) { + if (EMSESP::system_.ethernet_connected()) { doc["ip"] = ETH.localIP().toString(); } else { doc["ip"] = WiFi.localIP().toString(); diff --git a/src/system.cpp b/src/system.cpp index 30c5720c4..192fe380c 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -119,19 +119,29 @@ void System::format(uuid::console::Shell & shell) { System::restart(); } +void System::syslog_start() { + if (syslog_enabled_) { +#ifndef EMSESP_STANDALONE + syslog_.start(); +#endif + EMSESP::logger().info(F("Starting Syslog")); + } +} + void System::syslog_init(bool refresh) { if (refresh) { get_settings(); } #ifndef EMSESP_STANDALONE - // check for empty hostname + // check for empty or invalid hostname IPAddress addr; if (!addr.fromString(syslog_host_.c_str())) { syslog_enabled_ = false; } - // in case service is still running, this flushes the queue - https://github.com/emsesp/EMS-ESP/issues/496 + // in case service is still running, this flushes the queue + // https://github.com/emsesp/EMS-ESP/issues/496 if (!syslog_enabled_) { syslog_.log_level((uuid::log::Level)-1); syslog_.mark_interval(0); @@ -140,13 +150,10 @@ void System::syslog_init(bool refresh) { } // start & configure syslog - syslog_.start(); syslog_.log_level((uuid::log::Level)syslog_level_); syslog_.mark_interval(syslog_mark_interval_); syslog_.destination(addr, syslog_port_); - syslog_.hostname(hostname_.c_str()); - - EMSESP::logger().info(F("Syslog started")); + syslog_.hostname(hostname().c_str()); #endif } @@ -229,7 +236,7 @@ void System::start(uint32_t heap_start) { get_settings(); EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { - hostname(networkSettings.hostname.c_str()); + hostname(networkSettings.hostname.c_str()); // sets the hostname LOG_INFO(F("System %s booted (EMS-ESP version %s) "), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); // print boot message }); @@ -238,7 +245,7 @@ void System::start(uint32_t heap_start) { adc_init(false); // analog ADC button_init(false); // the special button network_init(false); // network - syslog_init(false); // init SysLog, must start after network + syslog_init(false); // init SysLog EMSESP::init_uart(); // start UART } @@ -484,7 +491,7 @@ void System::network_init(bool refresh) { eth_clock_mode_t clock_mode; // ETH_CLOCK_GPIO0_IN or ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT for 50Hz inverted clock if (board_profile_.equals("E32") || board_profile_.equals("LAN8720")) { - // Gateway E32 (LAN8720) + // BBQKees Gateway E32 (LAN8720) phy_addr = 1; power = 16; mdc = 23; diff --git a/src/system.h b/src/system.h index 6228358a9..3183d4145 100644 --- a/src/system.h +++ b/src/system.h @@ -66,6 +66,9 @@ class System { void show_mem(const char * note); void get_settings(); void wifi_tweak(); + void syslog_start(); + bool check_upgrade(); + void send_heartbeat(); void led_init(bool refresh); void syslog_init(bool refresh); @@ -75,12 +78,8 @@ class System { void commands_init(); static bool is_valid_gpio(uint8_t pin); - static bool load_board_profile(std::vector & data, const std::string & board_profile); - bool check_upgrade(); - void send_heartbeat(); - std::string hostname() { return hostname_; } @@ -97,6 +96,14 @@ class System { ethernet_connected_ = b; } + bool network_connected() { +#ifndef EMSESP_STANDALONE + return (ethernet_connected_ || WiFi.isConnected()); +#else + return true; +#endif + } + private: static uuid::log::Logger logger_; static uint32_t heap_start_; @@ -133,16 +140,16 @@ class System { void wifi_reconnect(); int8_t wifi_quality(); - bool system_healthy_ = false; - uint32_t led_flash_speed_ = LED_WARNING_BLINK_FAST; // default boot flashes quickly - uint32_t last_heartbeat_ = 0; - uint32_t last_system_check_ = 0; - bool upload_status_ = false; // true if we're in the middle of a OTA firmware upload - bool ethernet_connected_; + bool system_healthy_ = false; + uint32_t led_flash_speed_ = LED_WARNING_BLINK_FAST; // default boot flashes quickly + uint32_t last_heartbeat_ = 0; + uint32_t last_system_check_ = 0; + bool upload_status_ = false; // true if we're in the middle of a OTA firmware upload + bool ethernet_connected_ = false; uint16_t analog_; // settings - std::string hostname_; + std::string hostname_ = "ems-esp"; bool hide_led_; uint8_t led_gpio_; bool syslog_enabled_;