diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 96b0519a8..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 @@ -191,6 +192,7 @@ char log_data[LOGSZ]; // Logging char web_log[WEB_LOG_SIZE] = {'\0'}; // Web log buffer String backlog[MAX_BACKLOG]; // Command backlog + /********************************************************************************************/ char* Format(char* output, const char* input, int size) @@ -1575,6 +1577,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 +2583,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 +2798,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 + } }