ESP8266 WiFi 管理器和调制解调器休眠

ESP8266 WiFi Manager and Modem Sleep

菜鸟问题,我很了解这里,我正在尝试使用 wifi 管理器和调制解调器睡眠,但是一旦我关闭 wifi,我就无法再次连接。我知道我缺少的是关闭 wifi 后连接到 wifi 的配置,但如果我使用 WiFi Manager,我不知道该怎么做。

步骤如下:

  1. 开启ESP8266
  2. 调用WiFi Manager并设置凭据。留着下次用
  3. 关闭WiFi(为了在不消耗电力的情况下从传感器收集数据)
  4. 收集了我的 5 个数据我需要打开 WiFi 并连接它

我做了两个功能,一个是关闭wifi,一个是打开wifi

void active_mode(){
  //se spento accendiamo il wifi 
  if(WiFi.status() != WL_CONNECTED){
    WiFi.forceSleepWake();
    
    //here I should connect to the wifi but how
    //if I'm using wifi manager?
    //normally I would use this:
    //WiFi.mode(WIFI_STA);
    //WiFi.begin(ssid, password);
    //but what if I already saved my credential the first time I powered on the esp 
    //how to retrieve them in order to connect to wifi?
    
    delay(1);
  }
}

void sleep_mode(){
  //spegniamo il wifi
  WiFi.disconnect();
  WiFi.forceSleepBegin();
  if(WiFi.status() != WL_CONNECTED){
    Serial.print(WiFi.status());
    Serial.println(" :WiFi spento");
  }
  delay(1);
}

在无效设置中我得到了这个。这样,当我第一次打开我的 esp 时,我可以保存我的 WiFi 凭证,并在每次打开我的 esp 时连接到它。

void setup(){
  // WiFiManager
  // Local intialization.
  WiFiManager wifiManager;

  //wifiManager.autoConnect("AutoConnectAP");
  // if you get here you have connected to the WiFi
  Serial.println("Connected.");
  delay(5000);

  //I call sleep_mode here because I need to collect data from a sensor without the WiFi 
  powered on
  sleep_mode();
}

这是循环

void loop() {
 
  //Reading data every 3 seconds and save them in file.txt
  now = millis();    
  if (now - start_time >= timer_save){ 
    //if (count < 3){   
      read_data();
      saveHistory();
      start_time = now;
      count++; 
    //}
  }
     
  //saved 5 data turn on wifi, read them and clear the content of file.txt
  if (count == 5){
    active_mode();
    // checking connection
    if (!client.connected()) {
            reconnect();
          }
    client.loop();
    readHistory();
    //mqtt_publish();
    LittleFS.remove("/file.txt");
    count = 0;
    start_time = 0;
    sleep_mode();
    //delay(5000);
  }

}//end of void loop();

如果我没看错的话。这可能会有很大帮助: https://github.com/tzapu/WiFiManager/issues/272 如果没有,我的错。