diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 614b5d77..5123bd4d 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -168,6 +168,12 @@ #define HEARTBEAT_REPORT_VERSION 1 #define HEARTBEAT_REPORT_INTERVAL 0 +//------------------------------------------------------------------------------ +// Load average +//------------------------------------------------------------------------------ +#define LOADAVG_INTERVAL 30000 // Interval between calculating load average (in ms) +#define LOADAVG_REPORT 1 // Should we report Load average over MQTT? + //------------------------------------------------------------------------------ // RESET //------------------------------------------------------------------------------ @@ -595,6 +601,7 @@ PROGMEM const char* const custom_reset_string[] = { #define MQTT_TOPIC_RFRAW "rfraw" #define MQTT_TOPIC_UARTIN "uartin" #define MQTT_TOPIC_UARTOUT "uartout" +#define MQTT_TOPIC_LOADAVG "loadavg" // Light module #define MQTT_TOPIC_CHANNEL "channel" diff --git a/code/espurna/system.ino b/code/espurna/system.ino index 5002c26d..82c8b854 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -26,6 +26,9 @@ bool _system_send_heartbeat = false; bool _systemStable = true; +// Calculated load average 0 to 100; +unsigned short int _load_average = 100; + void systemCheck(bool stable) { unsigned char value = EEPROM.read(EEPROM_CRASH_COUNTER); if (stable) { @@ -72,14 +75,31 @@ void systemLoop() { #if HEARTBEAT_ENABLED // Heartbeat - static unsigned long last = 0; - if (_system_send_heartbeat || (last == 0) || (millis() - last > HEARTBEAT_INTERVAL)) { + static unsigned long last_hbeat = 0; + if (_system_send_heartbeat || (last_hbeat == 0) || (millis() - last_hbeat > HEARTBEAT_INTERVAL)) { _system_send_heartbeat = false; - last = millis(); + last_hbeat = millis(); heartbeat(); } #endif // HEARTBEAT_ENABLED + // Increase temporary loop counter + static unsigned long load_counter_temp = 0; + static unsigned long load_counter = 0; + static unsigned long load_counter_max = 1; + static unsigned long last_loadcheck = 0; + + load_counter_temp++; + if (millis() - last_loadcheck > LOADAVG_INTERVAL) { + load_counter = load_counter_temp; + load_counter_temp = 0; + if (load_counter > load_counter_max) { + load_counter_max = load_counter; + } + _load_average = 100 - (100 * load_counter / load_counter_max); + last_loadcheck = millis(); + } + // Power saving delay delay(_loopDelay); @@ -120,3 +140,7 @@ void systemSetup() { espurnaRegisterLoop(systemLoop); } + +unsigned long getLoadAverage() { + return _load_average; +} diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index a9ebb4a8..9f0e8e0f 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -172,6 +172,9 @@ void heartbeat() { #if (HEARTBEAT_REPORT_STATUS) mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); #endif + #if (LOADAVG_REPORT) + mqttSend(MQTT_TOPIC_LOADAVG, String(getLoadAverage()).c_str()); + #endif } #endif diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino index eceda63a..2066935f 100644 --- a/code/espurna/ws.ino +++ b/code/espurna/ws.ino @@ -220,6 +220,7 @@ void _wsUpdate(JsonObject& root) { root["heap"] = getFreeHeap(); root["uptime"] = getUptime(); root["rssi"] = WiFi.RSSI(); + root["loadaverage"] = getLoadAverage(); #if NTP_SUPPORT if (ntpSynced()) root["now"] = now(); #endif diff --git a/code/html/index.html b/code/html/index.html index 1e96543a..b147fe1d 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -223,6 +223,9 @@
Uptime
+
Load average
+
%
+
Free heap