Chat freely about anything...

User avatar
By dayzman
#17313 Does anyone know if user_init() is called after trying to establish a connection to the AP? If a connection can be established, would it be safe to assume that by the time it reaches user_init(), there is already a connection? Otherwise, how to ensure that the device is already connected?

Thanks
User avatar
By ToSa
#17353 Try this:

Code: Select allvoid SystemInitDoneHandler() {
   // do whatever you want to do here
}

void user_init() {
   // do whatever is independent from connection e.g. uart setup etc. here
   // and register the callback to run that code when connection is established (if possible and if autoconnect is true)
   system_init_done_cb(SystemInitDoneHandler);
}
User avatar
By dayzman
#17355 Thanks. But is the callback registered with system_init_done_cb() triggered after a connection to the AP is established or is it triggered once the system boot sequence is complete (regardless of the state of the WiFi connection)?

Thanks
User avatar
By eriksl
#17472 It's a little bit different.

The user_init routine is called when barely any initialisation is done. That's good because that way it has major influence on the initialisation and process. That also means you can't cannot to an ap at that stage. You can call the relevant functions but the actual connecting will only take place after initialisation has been completed.

So imho there are a few approaches if you want to connect to a specific access point.

1) simplest; load any AT+ firmware, join the access point, the data will be stored into flash and the next time after reboot the system code will connect to the access point again, even if another firmware is loaded in the meantime
2) disable "autoconnect", and/or disconnect in the user_init code and also connect there. The actual connection will be made when the user_init function has returned, you cannot wait for it
3) add the above callback and do all wifi initialisation there, it will be processed "immediately" (although some things need to run in the background and will run when you return from the callback).
4) add a sytem "post" background task, it will only be called when the system is completely up and running; never wait on something but simply return from the task, let the system do it's processing, and everytime the background task is called, check if it has completed. IMHO that's the best way, because some non-time-critical tasks from the system need to be run regularly and only get run when you return control to the system.