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



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

以下是步骤:

  1. 打开ESP8266电源
  2. 致电WiFi管理器并设置凭据。保存以备下次使用
  3. 关闭WiFi(以便在不耗电的情况下从传感器收集数据(
  4. 收集了我的5个数据我需要打开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如果没有,我的坏。

最新更新