[Answered] How to use esp_wifi_set_auto_connect()?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Answered] How to use esp_wifi_set_auto_connect()?

Postby kolban » Sat Oct 08, 2016 6:31 pm

I've been studying the ESP-IDF and been reading about "esp_wifi_esp_set_autoconnect". If I understood this concept from the ESP8266, we could set our boot mode and access point details once and have the ESP8266 save them in flash memory. Then, if we chose, the next (and subsequent) times that the ESP8266 booted, it would use those saved parameters to connect to an access point when in STA or STA+AP modes. Looking at the ESP-IDF, I see functions to enable or disable the ability to autoconnect at what I am assuming to be ESP32 boot time but I'm not seeing any corresponding functions to save/set what the properties it should use to connect. Does anyone have any knowledge or references in this area?

Later ... I may have found the answer. Looking further, I see the method called esp_wifi_set_storage() which can take as a parameter either WIFI_STORAGE_FLASH or WIFI_STORAGE_RAM. If we set this to be WIFI_STORAGE_FLASH then the settings that we may set using esp_wifi_set_config() will be stored in BOTH flash and RAM while if we set this value to be WIFI_STORAGE_RAM, we will ONLY set the current configuration in RAM. My guess is that the values that we set in storage flash are the values used during an autoconnect. This is a guess ... but it would seem to work about right in theory.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

tedwood
Posts: 11
Joined: Thu Mar 16, 2017 2:02 pm

Re: [Answered] How to use esp_wifi_set_auto_connect()?

Postby tedwood » Thu May 18, 2017 6:35 am

That seems to make sense. Has anyone got this to work?

Wilberforce
Posts: 8
Joined: Sun Apr 02, 2017 4:50 am

Re: [Answered] How to use esp_wifi_set_auto_connect()?

Postby Wilberforce » Mon May 22, 2017 11:58 am

Yes - we use this in the esp32 port of espruino.

From memory, there is also a menuconfig option to store in nvs, so you don't need a phy partition to store the data, nvs is used instead.

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: [Answered] How to use esp_wifi_set_auto_connect()?

Postby Trialblazer47 » Thu Jun 29, 2017 7:40 pm

can you please provide more details on how its done? I want to do this. store the wifi details and retrive it every time I restart.
Thanks.

eyaleb
Posts: 31
Joined: Sun May 14, 2017 6:54 am

Re: [Answered] How to use esp_wifi_set_auto_connect()?

Postby eyaleb » Sun Jul 02, 2017 7:44 am

Me too. I would like to see an example where autoconnect works and I do not need to manually set a/p credentials and static IP.

My experience is that I must issue a esp_wifi_connect() or a connection does not happen. The only callback I see is SYSTEM_EVENT_STA_START. I must be doing something wrong. My test logic looks like this (some detail omitted):

Code: Select all

nvs_flash_init();
tcpip_adapter_init();
esp_event_loop_init(event_handler, NULL);
esp_wifi_init(&cfg);

if this is a cold start
    esp_wifi_set_storage(WIFI_STORAGE_FLASH);
    tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
//  set up ssid/pwd/ip/mask/gw/autoconnect:
    tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info_new);
    esp_wifi_set_mode(WIFI_MODE_STA);
    esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
    esp_wifi_set_auto_connect(true);
    esp_wifi_start();
    esp_wifi_connect();    // needed?
else
    esp_wifi_start();
I hoped that I do not need to do any of this on a warm start (wakeup), not even start the adapter and wifi. If I do not set IP info then I see dhcp requests.

grooves
Posts: 12
Joined: Fri Aug 25, 2017 2:51 pm

Re: [Answered] How to use esp_wifi_set_auto_connect()?

Postby grooves » Fri Sep 22, 2017 11:10 am

I fiddled around with the esp_wifi_set_auto_connect() function,
it is true per default, but I don't see any distinction between true or false.
to start the wifi it needs always:

esp_wifi_init
esp_wifi_set_mode (only if not stored in NVS)
esp_wifi_set_config (only if not stored in NVS)
esp_wifi_start
esp_wifi_connect

when the ssid/pw is stored in NVS via esp_wifi_set_storage(WIFI_STORAGE_FLASH) you can skip
,esp_wifi_set_mode and esp_wifi_set_config

not sure to me what that function does,
I thought I could save a little bit time, coming from deep sleep, bring up the wifi again,
but doesn't seems to be, takes always about 1 sec, too long for battery powered applications

Regards,
grooves

Who is online

Users browsing this forum: Baidu [Spider] and 125 guests