0

In a traditional scenario, after the setup() method we go to the loop() method of our sketch. You know, usually it gets executed probably 5-10 times in a second (or even more).

I am now playing around with WiFi options trying to write a code which allows in to conditionally turn on/off the WiFi module. I've been following this article more or less with some small modifications.

This is the original version of the WiFiOff() method:

void WiFiOff() {

    //Serial.println("diconnecting client and wifi");
    //client.disconnect();
    wifi_station_disconnect();
    wifi_set_opmode(NULL_MODE);
    wifi_set_sleep_type(MODEM_SLEEP_T);
    wifi_fpm_open();
    wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
}

And this is my version with the observed behavior right after the method gets called:

void WiFiOff() {
    Serial.println("diconnecting client and wifi");
    // Disconnects WiFi station from AP
    // ????? (34 seconds between loops) wifi_station_disconnect();

    // Sets WiFi working mode as station, soft-AP or station+soft-AP, and save it to flash. Default is softAP mode
    // ????? (34 seconds between loops) wifi_set_opmode(NULL_MODE);

    // Sets sleep type for power saving.
    wifi_set_sleep_type(MODEM_SLEEP_T);

    // Enable force sleep function.
    wifi_fpm_open();

    // Force ESP8266 to enter sleep mode, and it will wake up automatically on time out.
    // ????? (Keeps on restarting if 2 top most are commmented) wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
    Serial.println("diconnecting client and wifi FINISHED");
}

As you can see, in each of the API methods I've added an explanation based on this document and the observed behavior of the chip when the specific line is not commented.

I don't really understand why after I called WiFiOff() I start getting delayed loop cycles. And the delay is quite specific (maybe for my hardware) and it is 34 seconds.

And, this is the whole code sketch:

/*
 * ESP8266 Deep sleep mode example
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */
extern "C" {
    #include "user_interface.h"  // Required for wifi_station_connect() to work
}

#include <Arduino.h>
#include <ESP8266WiFi.h>

#define FPM_SLEEP_MAX_TIME 0xFFFFFFF

const char* ssid = "ssid";
const char* password = "pass";

void WiFiOn();
void WiFiOff();

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
     delay(500);
  }

  WiFiOff();
}

void loop() {
  Serial.println("In the loop!");

  // ... Some logic to call WiFiOff() or WiFiOn()
}

void WiFiOn() {
    wifi_fpm_do_wakeup();
    wifi_fpm_close();
    wifi_set_opmode(STATION_MODE);
    wifi_station_connect();
}


void WiFiOff() {
    wifi_station_disconnect();
    wifi_set_opmode(NULL_MODE);
    wifi_set_sleep_type(MODEM_SLEEP_T);
    wifi_fpm_open();
    wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
}

I am really wondering what is causing the delay and if this is an expected behavior.

  • maybe it sleeps? – Juraj Nov 27 '19 at 8:10
  • @Juraj I don't really know. That's why I am asking. But, I find it strange disabling the wifi to set the whole ESP to sleeping mode. – user60623 Nov 29 '19 at 8:15
  • reference for wifi_fpm_do_sleep: "Force ESP8266 to enter sleep mode, and it will wake up automatically on time out." – Juraj Nov 29 '19 at 8:23
  • @Juraj Interesting. Is that somehow controllable? – user60623 Dec 22 '19 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.