You can chat about native SDK questions and issues here.

User avatar
By honda4life
#78888 Hi all,

I wish programming ESP was easy, especially if you want maximum power saving :D

I am looking for a clean way to connect to wifi, and guard this with a timeout.
My program is able to connect and to give status informaton by wifi_set_event_handler_cb.

So... how to guard this attempt to connect with a timeout > Set a seperate timer?
How to pass back control to my "main" function?
Same issue with the status connected in the callback, I need connection before a certain point in the "main" function.
What's the best solution?

I can't believe polling connection status in a loop is the best solution here?
Probably there has to be a delay in this loop to hand over control to the system and to keep the watchdog happy in this case?
What is the SDK equivalent to yield()?
I don't want to burn cycles but do measurements in the meantime that can be averaged.

Thanks
User avatar
By quackmore
#79217 This worked for me:

for the guard part:
start a timer and connect to AP (external wifi router)
this will end up with either a EVENT_STAMODE_GOT_IP (successfully connection, stop the timer) or timer exhaustion (failed to connect, take an action. For instance I switch the esp8266 wifi to STATIONAP, so I can control the esp8266 while still trying to connect to the external AP)

for coordinating wifi and other services (what you called 'pass back control to my "main" function'):
forget about a "main" function and use events (wifi_set_event_handler_cb)
start all the services that require wifi (web served, mdns or whatever ...) on EVENT_STAMODE_GOT_IP
and stop them on EVENT_STAMODE_DISCONNECTED

for this coordination stuff, in replacement of the "main" function, I usually create a task with all the relevant events for my application, including the wifi events and more

cheers