mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
send_info_mqtt called from main, not from ntp task
This commit is contained in:
@@ -82,5 +82,4 @@ void NTPSettingsService::configureTime(AsyncWebServerRequest * request, JsonVari
|
|||||||
void NTPSettingsService::ntp_received(struct timeval * tv) {
|
void NTPSettingsService::ntp_received(struct timeval * tv) {
|
||||||
// emsesp::EMSESP::logger().info("NTP sync to %d sec", tv->tv_sec);
|
// emsesp::EMSESP::logger().info("NTP sync to %d sec", tv->tv_sec);
|
||||||
emsesp::EMSESP::system_.ntp_connected(true);
|
emsesp::EMSESP::system_.ntp_connected(true);
|
||||||
emsesp::EMSESP::system_.send_info_mqtt("connected", true); // send info topic with NTP time
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -541,19 +541,26 @@ void System::loop() {
|
|||||||
|
|
||||||
led_monitor(); // check status and report back using the LED
|
led_monitor(); // check status and report back using the LED
|
||||||
system_check(); // check system health
|
system_check(); // check system health
|
||||||
|
send_info_mqtt();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// send MQTT info topic appended with the version information as JSON, as a retained flag
|
// send MQTT info topic appended with the version information as JSON, as a retained flag
|
||||||
void System::send_info_mqtt(const char * event_str, bool send_ntp) {
|
void System::send_info_mqtt() {
|
||||||
// use dynamic json because it is called from NTP-callback from lwip task with small stack
|
static uint8_t _connection = 0;
|
||||||
DynamicJsonDocument doc = DynamicJsonDocument(EMSESP_JSON_SIZE_MEDIUM);
|
uint8_t connection = (ethernet_connected() ? 1 : 0) + ((WiFi.status() == WL_CONNECTED) ? 2 : 0) + (ntp_connected_ ? 4 : 0) + (has_ipv6_ ? 8 : 0);
|
||||||
doc["event"] = event_str;
|
// check if connection status has changed
|
||||||
|
if (!Mqtt::connected() || connection == _connection) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_connection = connection;
|
||||||
|
StaticJsonDocument<EMSESP_JSON_SIZE_MEDIUM> doc;
|
||||||
|
doc["event"] = "connected";
|
||||||
doc["version"] = EMSESP_APP_VERSION;
|
doc["version"] = EMSESP_APP_VERSION;
|
||||||
|
|
||||||
// if NTP is enabled send the boot_time in local time in ISO 8601 format (eg: 2022-11-15 20:46:38)
|
// if NTP is enabled send the boot_time in local time in ISO 8601 format (eg: 2022-11-15 20:46:38)
|
||||||
// https://github.com/emsesp/EMS-ESP32/issues/751
|
// https://github.com/emsesp/EMS-ESP32/issues/751
|
||||||
if (send_ntp) {
|
if (ntp_connected_) {
|
||||||
char time_string[25];
|
char time_string[25];
|
||||||
time_t now = time(nullptr) - uuid::get_uptime_sec();
|
time_t now = time(nullptr) - uuid::get_uptime_sec();
|
||||||
strftime(time_string, 25, "%FT%T%z", localtime(&now));
|
strftime(time_string, 25, "%FT%T%z", localtime(&now));
|
||||||
@@ -1200,6 +1207,9 @@ bool System::command_info(const char * value, const int8_t id, JsonObject & outp
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) {
|
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) {
|
||||||
|
if (WiFi.status() == WL_CONNECTED && !settings.bssid.isEmpty()) {
|
||||||
|
node["BSSID"] = "set";
|
||||||
|
}
|
||||||
node["static ip config"] = settings.staticIPConfig;
|
node["static ip config"] = settings.staticIPConfig;
|
||||||
node["enable IPv6"] = settings.enableIPv6;
|
node["enable IPv6"] = settings.enableIPv6;
|
||||||
node["low bandwidth"] = settings.bandwidth20;
|
node["low bandwidth"] = settings.bandwidth20;
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ class System {
|
|||||||
bool check_restore();
|
bool check_restore();
|
||||||
bool heartbeat_json(JsonObject & output);
|
bool heartbeat_json(JsonObject & output);
|
||||||
void send_heartbeat();
|
void send_heartbeat();
|
||||||
void send_info_mqtt(const char * event_str, bool send_ntp = false);
|
void send_info_mqtt();
|
||||||
|
|
||||||
bool syslog_enabled() {
|
bool syslog_enabled() {
|
||||||
return syslog_enabled_;
|
return syslog_enabled_;
|
||||||
@@ -181,6 +181,10 @@ class System {
|
|||||||
ethernet_connected_ = b;
|
ethernet_connected_ = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void has_ipv6(bool b) {
|
||||||
|
has_ipv6_ = b;
|
||||||
|
}
|
||||||
|
|
||||||
void ntp_connected(bool b);
|
void ntp_connected(bool b);
|
||||||
bool ntp_connected();
|
bool ntp_connected();
|
||||||
|
|
||||||
@@ -297,6 +301,7 @@ class System {
|
|||||||
|
|
||||||
bool upload_status_ = false; // true if we're in the middle of a OTA firmware upload
|
bool upload_status_ = false; // true if we're in the middle of a OTA firmware upload
|
||||||
bool ethernet_connected_ = false;
|
bool ethernet_connected_ = false;
|
||||||
|
bool has_ipv6_ = false;
|
||||||
|
|
||||||
bool ntp_connected_ = false;
|
bool ntp_connected_ = false;
|
||||||
uint32_t ntp_last_check_ = 0;
|
uint32_t ntp_last_check_ = 0;
|
||||||
|
|||||||
@@ -38,13 +38,12 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|||||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
||||||
EMSESP::logger().warning("WiFi disconnected. Reason code=%s", disconnectReason(info.wifi_sta_disconnected.reason)); // IDF 4.0
|
EMSESP::logger().warning("WiFi disconnected. Reason code=%s", disconnectReason(info.wifi_sta_disconnected.reason)); // IDF 4.0
|
||||||
WiFi.disconnect(true);
|
WiFi.disconnect(true);
|
||||||
|
EMSESP::system_.has_ipv6(false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||||
EMSESP::logger().info("WiFi connected with IP=%s, hostname=%s", WiFi.localIP().toString().c_str(), WiFi.getHostname());
|
EMSESP::logger().info("WiFi connected with IP=%s, hostname=%s", WiFi.localIP().toString().c_str(), WiFi.getHostname());
|
||||||
// EMSESP::system_.syslog_init();
|
|
||||||
mDNS_start();
|
mDNS_start();
|
||||||
EMSESP::system_.send_info_mqtt("connected");
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ARDUINO_EVENT_ETH_START:
|
case ARDUINO_EVENT_ETH_START:
|
||||||
@@ -64,22 +63,21 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|||||||
// prevent double calls
|
// prevent double calls
|
||||||
if (!EMSESP::system_.ethernet_connected()) {
|
if (!EMSESP::system_.ethernet_connected()) {
|
||||||
EMSESP::logger().info("Ethernet connected with IP=%s, speed %d Mbps", ETH.localIP().toString().c_str(), ETH.linkSpeed());
|
EMSESP::logger().info("Ethernet connected with IP=%s, speed %d Mbps", ETH.localIP().toString().c_str(), ETH.linkSpeed());
|
||||||
// EMSESP::system_.send_heartbeat();
|
|
||||||
// EMSESP::system_.syslog_init();
|
|
||||||
EMSESP::system_.ethernet_connected(true);
|
EMSESP::system_.ethernet_connected(true);
|
||||||
mDNS_start();
|
mDNS_start();
|
||||||
EMSESP::system_.send_info_mqtt("connected");
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
||||||
EMSESP::logger().warning("Ethernet disconnected");
|
EMSESP::logger().warning("Ethernet disconnected");
|
||||||
EMSESP::system_.ethernet_connected(false);
|
EMSESP::system_.ethernet_connected(false);
|
||||||
|
EMSESP::system_.has_ipv6(false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ARDUINO_EVENT_ETH_STOP:
|
case ARDUINO_EVENT_ETH_STOP:
|
||||||
EMSESP::logger().info("Ethernet stopped");
|
EMSESP::logger().info("Ethernet stopped");
|
||||||
EMSESP::system_.ethernet_connected(false);
|
EMSESP::system_.ethernet_connected(false);
|
||||||
|
EMSESP::system_.has_ipv6(false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||||
@@ -105,9 +103,8 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|||||||
} else {
|
} else {
|
||||||
EMSESP::logger().info("WiFi connected with IPv6=%s, hostname=%s", WiFi.localIPv6().toString().c_str(), WiFi.getHostname());
|
EMSESP::logger().info("WiFi connected with IPv6=%s, hostname=%s", WiFi.localIPv6().toString().c_str(), WiFi.getHostname());
|
||||||
}
|
}
|
||||||
// EMSESP::system_.syslog_init();
|
|
||||||
mDNS_start();
|
mDNS_start();
|
||||||
EMSESP::system_.send_info_mqtt("connected");
|
EMSESP::system_.has_ipv6(true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -151,7 +148,7 @@ void WebStatusService::webStatusService(AsyncWebServerRequest * request) {
|
|||||||
if (EMSESP::sensor_enabled()) {
|
if (EMSESP::sensor_enabled()) {
|
||||||
statJson = statsJson.createNestedObject();
|
statJson = statsJson.createNestedObject();
|
||||||
statJson["id"] = 3;
|
statJson["id"] = 3;
|
||||||
statJson["s"] = EMSESP::temperaturesensor_.reads();
|
statJson["s"] = EMSESP::temperaturesensor_.reads() - EMSESP::temperaturesensor_.fails();
|
||||||
statJson["f"] = EMSESP::temperaturesensor_.fails();
|
statJson["f"] = EMSESP::temperaturesensor_.fails();
|
||||||
statJson["q"] =
|
statJson["q"] =
|
||||||
EMSESP::temperaturesensor_.reads() == 0 ? 100 : 100 - (uint8_t)((100 * EMSESP::temperaturesensor_.fails()) / EMSESP::temperaturesensor_.reads());
|
EMSESP::temperaturesensor_.reads() == 0 ? 100 : 100 - (uint8_t)((100 * EMSESP::temperaturesensor_.fails()) / EMSESP::temperaturesensor_.reads());
|
||||||
@@ -159,7 +156,7 @@ void WebStatusService::webStatusService(AsyncWebServerRequest * request) {
|
|||||||
if (EMSESP::analog_enabled()) {
|
if (EMSESP::analog_enabled()) {
|
||||||
statJson = statsJson.createNestedObject();
|
statJson = statsJson.createNestedObject();
|
||||||
statJson["id"] = 4;
|
statJson["id"] = 4;
|
||||||
statJson["s"] = EMSESP::analogsensor_.reads();
|
statJson["s"] = EMSESP::analogsensor_.reads() - EMSESP::analogsensor_.fails();
|
||||||
statJson["f"] = EMSESP::analogsensor_.fails();
|
statJson["f"] = EMSESP::analogsensor_.fails();
|
||||||
statJson["q"] = EMSESP::analogsensor_.reads() == 0 ? 100 : 100 - (uint8_t)((100 * EMSESP::analogsensor_.fails()) / EMSESP::analogsensor_.reads());
|
statJson["q"] = EMSESP::analogsensor_.reads() == 0 ? 100 : 100 - (uint8_t)((100 * EMSESP::analogsensor_.fails()) / EMSESP::analogsensor_.reads());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user