Enable WiFi and Connect via MQTT after boot

Our goal is to turn on station wifi mode on and start connecting via MQTT after boot, but having it boot with it off first. We want to save on power consumption. We want to read off the I2C bus first, and use data from reads to determine whether we should turn on WiFi or not. We use mjs for prototyping, but did attempt to import create custom c function(s) to achieve.

We first tried changing the config, with no reboot, this did not enable wifi as we expected. We did not see the power consumption jump that would indicate the WiFi peripherals were enabled. We would need to reboot to have the config changes applied, but that would be inconvenient, but if we need to do so, that is the best option we have so far.

We next tried creating a custom function as mentioned above. The logic we attempted to achieve turning on the WiFi without a reboot is below, but this caused a brown out on the device, which we found interesting.

  esp_netif_create_default_wifi_sta();
  esp_wifi_set_mode(WIFI_MODE_STA);
  esp32_wifi_ensure_init();
  esp32_wifi_ensure_init();
  esp32_wifi_ensure_start();
  esp_wifi_connect();

Any thoughts on how turning on the WiFi and connecting via MQTT can be achieved after boot? Thank you very much.

This has been answered several times in the forum, search for those posts. You need to call the setup functions passing the proper parameters.

It is not possible to cause a brown-out on the device by calling a function; unless:

  1. your program writes where it shouldn’t and overwrites the brown-out detector configuration (in case your hardware allows this)
  2. your device crashes and somehow the boot code interprets there was a brown-out condition
  3. your power supply voltage actually drops and you do have a real brown-out condition. This is not “caused by the function call” but by an improperly designed hardware.

IIRC I’ve already posted about how turning on wifi causes high-current spikes and how improperly designed hardware may not cope with that.

@scaprile Thank you. I appreciate the candor.

I was able to search posts you answered talking about the setup functions and found my answer on how to perform what I needed here: Crash on WiFi Enable.

The brown out appears to be a hardware design issue (our regulator doesn’t supply enough current).