简体   繁体   English

如何在使用 wifimanager 库暂时断开连接并且不停止后将 esp32 重新连接到 wifi

[英]How to reconnect esp32 to wifi after getting disconnected temporarily using wifimanager library and not halting

Can someone please suggest how to reconnect automatically to the last known wifi network (through wifi manager library) after it gets disconnected for a while and without blocking forever till a network is connected?有人可以建议如何在断开一段时间后自动重新连接到最后一个已知的 wifi 网络(通过 wifi 管理器库),并且在连接网络之前不会永远阻塞?

The below code will work but needs restart every time a network is lost or no network was found during initialization.下面的代码可以工作,但每次网络丢失或初始化期间未找到网络时都需要重新启动。 But I cannot block it till network is connected because my program is supposed to run both in offline and online mode, ie can control pins even there is no internet, and in every loop it will try to connect to network.但是在网络连接之前我无法阻止它,因为我的程序应该在离线和在线模式下运行,即即使没有互联网也可以控制引脚,并且在每个循环中它都会尝试连接到网络。 If network is available then control the pins using Blynk.如果网络可用,则使用 Blynk 控制引脚。

#include <WiFi.h>

#include <WiFiClient.h>

#include <WebServer.h> //copiedota
#include <ESPmDNS.h> //copiedota
#include <Update.h> //copiedota


#include <WiFiManager.h>   
#include <ArduinoJson.h>          // https://github.com/bblanchon/ArduinoJson


#include <BlynkSimpleEsp32.h>
BlynkTimer timer;

#define DEBUG_SW 1

// By default the mode is with_internet
int MODE = 0;
bool connected_once = false;

char auth[] = "***";

// Your WiFi credentials.
const char* host = "shariqESP"; //copiedota
// Set password to "" for open networks.

char ssid[] = "rm7pro";

//char ssid[] = "";

char pass[] = <"*****">;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wm;

  wm.setConfigPortalTimeout(40);
  
   //wm.setDebugOutput(true);
  
Serial.printf("SSID0: %s\n", wm.getWiFiSSID().c_str());
String saved_ssid_old =wm.getWiFiSSID().c_str();

  if(!wm.autoConnect("AutoConnectAP")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    //ESP.restart();
    //delay(5000);
  } 
   Serial.printf("SSID1: %s\n", WiFi.SSID().c_str());
   Serial.println("SSID00: " + (String)wm.getWiFiSSID());
   Serial.println( wm.getWiFiIsSaved() );


if(WiFi.SSID().c_str() == ""){

int str_len = saved_ssid_old.length() + 1; 
char char_array[str_len];
// Copy it over 
saved_ssid_old.toCharArray(char_array, str_len);
Serial.println( ssid);

strcpy(ssid , char_array);

}
  
if(WiFi.SSID().c_str() != ""){
    Serial.println("not blank");

  String saved_ssid =WiFi.SSID().c_str();
  String saved_ssid_old =wm.getWiFiSSID().c_str();
  Serial.println(saved_ssid);
  Serial.println( saved_ssid_old );

    //if( !saved_ssid_old.equals(saved_ssid)  ){
      // Length (with one extra character for the null terminator)
      int str_len = saved_ssid.length() + 1; 
  // Prepare the character array (the buffer)
  char char_array[str_len];
   
  // Copy it over 
  saved_ssid.toCharArray(char_array, str_len);
  Serial.println( ssid);
  Serial.println( "different ssid");
  

  memset( ssid,0, sizeof(ssid)) ;
  //char ssid[str_len];
  strcpy(ssid , char_array);


char ssid2[] ="rm7pro";
if ( strcmp(ssid2, ssid) == 0 ) {
  Serial.println( "same ssid"); 

}
else if ( strcmp(ssid2, ssid)  > 0 ){
  Serial.println( "rm7pro is great same ssid"); 

}
else{
   Serial.println( "rm7pro is less same ssid"); 

  }
       
   // }
        
}
Serial.print("Now printing SSID:");
  Serial.println( ssid);

wm.disconnect();
delay(1000);  

   WiFi.begin(ssid, pass);
    Serial.print("main begin ssid:");
    Serial.println(ssid);
    int ct= 0;
      Serial.print("before while ssid:");
    Serial.println(ssid);
    
    while(WiFi.status() != WL_CONNECTED ){
      delay(1000);
      ct=ct+1000;
      if(ct>5000){
        Serial.println("not able to connect to wifi");        
        break;
        }
      }

    Serial.print("after while bfore if ssid:");
    Serial.println(ssid);
    
   if (WiFi.status() == WL_CONNECTED)
  {
     Serial.println( "connected once");
   connected_once = true;
  }
  Serial.print("before while after if ssid:");
    Serial.println(ssid);
  
  //setOTA(); //custom

   Serial.print("End ssid:");
    Serial.println(ssid);
}


void loop()
{
  Serial.println("Loop started");
  Serial.print("Wifi status: ");
  Serial.println(WiFi.status());
   Serial.print("Start ssid:");
    Serial.println(ssid);
  
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(ssid);
    Serial.println(WiFi.SSID().c_str() );
    if (DEBUG_SW) Serial.println("Not Connected");
    
   /* if( connected_once == true){
      Serial.println("Reconnecting");
        WiFi.disconnect();
        delay(1100); //custom

        WiFi.begin(ssid, pass);
    //WiFi.reconnect();
    }
    else{*/
      Serial.println("Re-attempting to connect");
      //WiFi.reconnect();
       WiFi.disconnect();
        delay(500); //custom
        WiFi.begin(ssid, pass);
         delay(1200); //custom
    //  }
  }
  else
  {
    if (DEBUG_SW) Serial.println(" Connected");
    //Blynk.run();
  }
 delay(400); //custom

}

If you observe the image now, you will notice that the esp wont connect to wifi even though providing the same ssid, it will still not connect.如果您现在观察图像,您会注意到即使提供相同的 ssid,esp 也无法连接到 wifi,它仍然无法连接。

在此处输入图像描述

Which is very strange as per my understanding.根据我的理解,这很奇怪。

Please suggest I am badly struck here.请建议我在这里受到严重打击。

Regards, Shariq问候, 沙里克

I realised the issue was with this lines ork is available then control the pins using Blynk.我意识到问题在于这条线 ork 可用,然后使用 Blynk 控制引脚。

    if (WiFi.status() == WL_CONNECTED)
   {
      Serial.println( "connected once");
    connected_once = true;
   }

This was somehow causing memory issue.这在某种程度上导致了 memory 问题。

I also added while loop inside loop function with a 10 sec timeout counter to reconnect.我还在循环 function 内添加了 while 循环,并带有 10 秒超时计数器以重新连接。

       while(WiFi.status() != WL_CONNECTED)
      {
    delay(1000);
         ct=ct +1000;
    if(ct >11000){
    break;
    }
    
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM