zhouhan0126 / WIFIMANAGER-ESP32

wifimanager-esp32
MIT License
401 stars 135 forks source link

How to get the saved AP SSID or the connected SSID ,to, reuse and reconnect? #48

Open shariq-azim opened 3 years ago

shariq-azim commented 3 years ago

Hello All, I just started tinkering with the library and use it in my project where I would like to reconnect to the device in case it loses connection. I am exploring the methods WiFi.begin(ssid, pass) and WiFi.reconnect() in loop.

But suppose it has been set freshly or started after timeout of config page without connecting. It wont reconnect.

I am not able to use wifiManager.getSSID() as it somehow gives error as" 'class WiFiManager' has no member named 'getSSID' ". My code is as below:

`#include

include

include //copiedota

include //copiedota

include //copiedota

//#if defined(ESP8266)//wifimanager //#include
//#else //#include
//#endif

//needed for library

include

//#if defined(ESP8266) //#include //#else //#include //#endif

include

include // https://github.com/bblanchon/ArduinoJson

//wifimanager

include

BlynkTimer timer;

define DEBUG_SW 1

int MODE = 0; bool connected_once = false;

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). char auth[] = "**";

// Your WiFi credentials. const char* host = "shariqESP"; //copiedota // Set password to "" for open networks. char ssid[] = "rm7pro"; char pass[] = "**";//same all accross

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 wifiManager; //reset settings - for testing //wifiManager.resetSettings();

//sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds wifiManager.setTimeout(60);

//fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration wifiManager.setDebugOutput(true);

//set custom ip for portal //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

Serial.println("start"); // Serial.println( wifiManager.getSSID()); // Serial.println( WiFi.getSSID()); //Serial.println( wifiManager.getSSID() );

if(!wifiManager.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("SSID0: %s\n", WiFi.SSID().c_str()); Serial.println("SSID00: " + (String)wifiManager.getSSID());

String saved_ssid =WiFi.SSID().c_str();

// 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); strcpy(ssid , char_array); Serial.println( ssid);

WiFi.begin(ssid, pass); if (WiFi.status() != WL_CONNECTED) { connected_once = true; }

}

void loop() { Serial.println("Loop started"); if (WiFi.status() != WL_CONNECTED) { if (DEBUG_SW) Serial.println("Not Connected");

if( connected_once == true){
  Serial.println("Reconnecting");
WiFi.reconnect();
}
else{
  Serial.println("Re-attempting to connect");
  WiFi.begin(ssid, pass);
  }

} else { if (DEBUG_SW) Serial.println(" Connected"); //Blynk.run(); }

delay(400); //custom

if (MODE == 0) with_internet(); else without_internet(); // put your main code here, to run repeatedly: }

`