PuceBaboon / ESP_Power_Save

How to reliably switch the ESP8266 WiFi on and off to achieve significant power savings.
Apache License 2.0
3 stars 0 forks source link

Convert to library? #1

Open Llaves opened 5 years ago

Llaves commented 5 years ago

Have you considered packaging the WiFi_On and WiFi_Off functions as a library? Obviously it's easy enough to cut and paste from your sample code, but a standalone library would be useful to folks.

BTW, used the WiFi_Off and WiFi_On() functions in a program with the Arduino IDE and they compiled with no problems.

Llaves commented 5 years ago

The reason your code requires explicit SSID and password parms to the begin() call is that the disconnect() call erases the ssid and password values from flash (if WiFi.persistent() is set to true, which it is by default in most WiFi libraries). You can overcome this:

bool WiFiOff() {
  conn_tries = 0;
  // disconnect will rewrite the configuration to show no SSID or password. If persistent is
  // set to true, this will null the values in flash, so don't do that.
  // However, if set to false, the current values are set to null, so this will
  // have to be accounted for in the WiFiOn function
  WiFi.persistent(false);
  WiFi.disconnect();
  WiFi.persistent(true);
  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();
  yield();

  // It can take a while for the ESP to disconnect, so we need
  // to give it a couple of seconds before returning a fail.

  while ((WiFi.status() == WL_CONNECTED)
         && (conn_tries++ < WIFI_RETRIES))
  {
    delay(100);
#ifdef DEBUG
    Serial.print(".");
#endif
  }
#ifdef DEBUG
  if (WiFi.status() != WL_CONNECTED)
    return (true);
  else
    return (false);
#endif
}

This doesn't solve the problem completely, however, because begin() uses the current configuration parameters, which were nulled out by the disconnect() call. Here's a WiFiOn() function that takes no parameters and instead uses the ones cached in flash.

struct station_config config;
bool WiFiOn()
{
  // the current config has no SSID or password - these were blown away by the disconnect with
  // persistent = false;
  // We can fetch from EEPROM the parameters that existed before disconnect.

  bool status = wifi_station_get_config_default(&config);
#ifdef DEBUG
  if (!status)
    Serial.println("Failed to fetch config");
  else
    Serial.println("Config fetched");
  Serial.print("Parameters fetched from EEPROM. SSID = ");
  Serial.println((const char *) config.ssid);
#endif

  return WiFiOn((const char *) config.ssid, (const char *) config.password);
}
billbee commented 2 years ago

I was using this to help maybe get ESPNOW to work, somewhere I read it works better with wifi off, but alas in my case did not. I guess the ESPNOW uses even less power than WiFi and while that does work, the signal is also very weak in the garage. Darn! But I liked learning what you had, so that was good.

Cheers.

PuceBaboon commented 2 years ago

...maybe get ESPNOW to work, somewhere I read it works better with wifi off, but alas in my case did not. ...and while that does work, the signal is also very weak in the garage.

Hi Billbee,

I'm using ESPNOW in an outdoor project at the moment (as a poor-man's remote control) and, line of sight, it works reasonably well (for the record, initializing WiFi in STA mode and then calling set channel, followed by the esp_now_init). I guess the problem for both of us is that the ESP isn't blasting out power at the same level as an access point with multiple antennas. You might want to try one of the TTGO boards with an external antenna for the module inside the garage (you could even replace the "rubber ducky" with a cantenna if your point-to-point connection is fairly static ...even a kitchen strainer works quite well if you're only trying to localize the signal in one general direction).

Good luck!

          -John-