tonyp7 / esp32-wifi-manager

Captive Portal for ESP32 that can connect to a saved wireless network or start an access point where you can connect to existing wifis.
MIT License
664 stars 217 forks source link

Time to execute esp_wifi_scan_start #40

Closed alexanderturner closed 5 years ago

alexanderturner commented 5 years ago

Hey there, I'm writing an application on top of wifi-manager that extends the web server such to support my own API. In development I noticed that the esp_wifi_scan_start appears to be stealing 2.5s of CPU time every time it runs (which if often). My next course of action is to disable scanning once the unit enters STA however I was wondering if it was something you've come across and if there are more event driven alternatives?

Alex

else if(uxBits & WIFI_MANAGER_REQUEST_WIFI_SCAN){                        
  int64_t timeBeforeTasks = esp_timer_get_time();
  ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, true));
  int64_t timeAfterTasks = esp_timer_get_time();
  printf("time to scan AP list: %lld ms\n", (timeAfterTasks-timeBeforeTasks)/1000);

time to scan AP list: 2500 ms time to scan AP list: 2497 ms time to scan AP list: 2497 ms

alexanderturner commented 5 years ago

Digging a bit deeper into this to force the IDF functions event-drivenness with a new arg in wifi_manager_event_handler and passing false to the blocking attribute. It appears to be an issue with esp-idf blocking the thread when running the scan. Perhaps there should be a timer arg for the wifi scan instead of it being called every time jquery call it.

tonyp7 commented 5 years ago

Helo Alex,

You can indeed set it to false but this brings a whole lot of new issues with thread synchronization. However, it is true blocking the thread is not ideal.

The whole architecture of the manager is hindered by the events being only triggered by a bitfield.

I am currently re-writing the app so that it uses a RTOS queue as primary thread sync instead. This a major rewrite so bear with me; I do not wish to commit broken code to the repository for now!

tonyp7 commented 5 years ago

This is fixed in branch 2.0

tonyp7 commented 5 years ago

Fixed in latest master trunk -- scans are now non blocking

alexanderturner commented 5 years ago

Thanks for this Tony! I should have jumped in with a PR sooner but awesome to see this being actively maintained. I had set the blocking field to false and actioned the update task on in the event handler but whilst it's now 'non blocking' it's still blocking the thread for similar time when it's executing the scan (which is pretty frequently when the jquery script is running).

I'm working on project which could benefit from RTOS queues - are you keeping that effort in a working branch?

tonyp7 commented 5 years ago

I merged my dev branch into the master so you can simply get the master :)

Queue process starts here: https://github.com/tonyp7/esp32-wifi-manager/blob/1a710dabcb9e7f7e0f3a8bb68fd0048ff4681908/wifi_manager/main/wifi_manager.c#L730

alexanderturner commented 5 years ago

Ah awesome :) Thanks. Would be awesome to have a build status for master :)