Reconfiguring WiFi without a reboot

#1
  1. My goal is: To reconfigure WiFi credentials without having to reboot. The use case here is that the WiFi credentials were initially misconfigured (wrong password, for example), and I would like the end user to be able to correct the password without rebooting the device. The configuration is done through Bluetooth, so I don’t want the bonding between device and user to be broken.
  2. My actions are: I’ve tried two methods. Each begins with setting the new credentials with mgos_sys_config_set_wifi_sta_ssid() and mgos_sys_config_set_wifi_sta_pass(). I’ve tried (1) calling mgos_wifi_disconnect() and then mgos_wifi_connect(), and (2) trying to reset the WiFi sta like so:
struct mgos_config_wifi_sta sta_config = {
        .enable = true,
        .ssid = ssid,
        .pass = pass,
};

mgos_wifi_disconnect();

if (mgos_wifi_setup_sta(&sta_config)) {
        mg_rpc_send_responsef(ri, "ok");
} else {
        mg_rpc_send_responsef(ri, "couldn't set up sta");
}

where ssid and pass are the new SSID and password, respectively.
3. The result I see is: I’m still connecting to the same network. Trying to set an incorrect password, for example, will not disconnect me from the AP.
4. My expectation & question is: I expect to be able to smoothly change WiFi creds while the device is running without any restarts. Is this possible?

Thanks!

#2

Seems like swapping out my call to mgos_wifi_setup_sta() with mgos_wifi_setup((struct mgos_config_wifi *)mgos_sys_config_get_wifi()) in option 2 does the trick. I guess I was missing something important in my mgos_config_wifi_sta struct?

1 Like
#3

Hi I am having exactly the same issue, except I am trying to send the credentials to the ESP32 via HTTP over AP mode. Therefore, if I use

mgos_wifi_setup((struct mgos_config_wifi *)mgos_sys_config_get_wifi())

my AP is also restarted, which I do not want. I only want the STA to restart, but not the AP.

I also tried

mgos_wifi_setup_sta(&sta_config)

and when I print out

sta_cfg.ssid

and

sta_cfg.pass

I seem to have the correct credentials. However, my ESP32 still seems to be connecting with the previous, incorrect password. So, again, my question is:

How can I avoid rebooting the system and still setting the WiFi configuration, so a correct, rather than a previously wrong password is used by the ESP32 when trying to connect to a network?

I would greatly appreciate your help!

#4

I’m not sure, search the forum 'cause this has already been answered, and there are some people that also have posted links to provisioning libraries (which I imagine is what you are trying to do). IIRC you have to reconfigure both AP and STA, but (again) check the forum. (just in case, did you actually disconnect before calling setup ?)
If you are working from an HTTP page you can also call the RPC functions from AJAX code in your browser, no need to write C code for simple scenarios. (I believe I’ve already posted something abouth this…)

1 Like
#5

Thank you for your reply, much appreciated! I’ll look into what you recommended and post here upon finding a solution.