From 0e56044eb5049d17fdbc48500c9fb214a435ab89 Mon Sep 17 00:00:00 2001 From: andrethomas Date: Sat, 24 Nov 2018 18:12:49 +0200 Subject: [PATCH 1/2] Add main loop load average Add main loop avarage duty cycle measured against setoption36 value to telemetry data as LoadAvg --- sonoff/sonoff.ino | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 96b0519a8..b1a279b47 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -190,6 +190,7 @@ char mqtt_data[MESSZ]; // MQTT publish buffer and web page char log_data[LOGSZ]; // Logging char web_log[WEB_LOG_SIZE] = {'\0'}; // Web log buffer String backlog[MAX_BACKLOG]; // Command backlog +uint32_t loop_load_avg = 0; // Indicative loop load average /********************************************************************************************/ @@ -1575,6 +1576,8 @@ void MqttShowState(void) snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_JSON_VCC "\":%s"), mqtt_data, stemp1); #endif + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"LoadAvg\":%u"), mqtt_data, loop_load_avg); + for (byte i = 0; i < devices_present; i++) { if (i == light_device -1) { LightState(1); @@ -2579,6 +2582,15 @@ void GpioInit(void) XdrvCall(FUNC_PRE_INIT); } + +void update_loop_load_avg(uint32_t loop_activity) +{ + uint32_t loops_per_second = 1000 / (uint32_t)Settings.param[P_LOOP_SLEEP_DELAY]; // We need to keep track of this many loops per second + uint32_t this_cycle_ratio = 100 * loop_activity / (uint32_t)Settings.param[P_LOOP_SLEEP_DELAY]; + uint32_t new_load_avg = loop_load_avg-(loop_load_avg/loops_per_second); // Take away one loop average + new_load_avg = new_load_avg + this_cycle_ratio; + loop_load_avg = new_load_avg; +} extern "C" { extern struct rst_info resetInfo; @@ -2785,4 +2797,9 @@ void loop(void) delay(my_activity /2); // If wifi down and my_activity > setoption36 then force loop delay to 1/3 of my_activity period } } + if (my_activity < (uint32_t)Settings.param[P_LOOP_SLEEP_DELAY]) { + update_loop_load_avg(my_activity); + } else { + update_loop_load_avg((uint32_t)Settings.param[P_LOOP_SLEEP_DELAY]); // Assume 100% loop cycle ratio + } } From 4700e49d705cdd29da6a05cadfe0fa9ca0fa9f8b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 24 Nov 2018 17:16:27 +0100 Subject: [PATCH 2/2] Update sonoff.ino --- sonoff/sonoff.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index b1a279b47..f461b6de0 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -125,6 +125,7 @@ int wifi_state_flag = WIFI_RESTART; // Wifi state flag int tele_period = 1; // Tele period timer int blinks = 201; // Number of LED blinks uint32_t uptime = 0; // Counting every second until 4294967295 = 130 year +uint32_t loop_load_avg = 0; // Indicative loop load average uint32_t global_update = 0; // Timestamp of last global temperature and humidity update float global_temperature = 0; // Provide a global temperature to be used by some sensors float global_humidity = 0; // Provide a global humidity to be used by some sensors @@ -190,7 +191,7 @@ char mqtt_data[MESSZ]; // MQTT publish buffer and web page char log_data[LOGSZ]; // Logging char web_log[WEB_LOG_SIZE] = {'\0'}; // Web log buffer String backlog[MAX_BACKLOG]; // Command backlog -uint32_t loop_load_avg = 0; // Indicative loop load average + /********************************************************************************************/