Chat freely about anything...
User avatar
By Vladis
#60736 Hello,

I am using the Arduino IDE.

In a time critical application I am using something like this:

Code: Select allvoid loop() {
    if (WiFi.softAPgetStationNum() != 0) {
      DoSomething();
    }
  DoCritical();
}


I wonder how much time takes the if test on WiFi.softAPgetStationNum(). I could, for example, use a timer, to test, say every 10 seconds, the state of WiFi.softAPgetStationNum() and set a global boolean variable accordingly. Then, inside the loop(), the test would be on that boolean variable.

In other words, I wonder if WiFi.softAPgetStationNum() is just a "property" of the WiFi object that we read or if it is a method that takes time to execute each time it is invoked!

Thanks for any feedback,

Vladis
User avatar
By EdZamper
#70796 Hi Vladis,

I'm fairly new to the forum too, but I think I can help you with this. Just put the following code in the beginning of the function and it will give you the execution time of the command you are asking for:

micros()

Code: Select all
unsigned long functionStartTime;
unsigned long functionEndTime;
unsigned long total_FunctionProcessingTime; // Declare these values to get the time for your function

void setup(){}   // your setup stuff here

void loop() {
  functionStartTime = micros();   // Start counting here
  WiFi.softAPgetStationNum();   //execute your function
  functionEndTime = micros();   //Store elapsed time since function was executed
  total_FunctionProcessingTime = functionEndTime - functionStartTime; //You'll get a value in uSeconds here
  Serial.println( total_FunctionProcessingTime);  //print your value to the Monitor screen
 delay(500);  // slow down the loop so you can see the average processing time for your function

//resume your normal code here

    if (WiFi.softAPgetStationNum() != 0) {
      DoSomething();
    }
  DoCritical();
}


Connect to a wifi first in the setup function and you can test execution time with this same function at setup for no connection and then inside the loop for connection times to learn the averages. Don't forget to clear your variables from time to time to prevent an overflow. You can just run the code before the // resume your normal code line and place any other function in between to test the execution times. Enjoy!

-EZ