tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.63k stars 1.98k forks source link

Loss of connection after reboot operation #620

Open ghost opened 6 years ago

ghost commented 6 years ago

With Nodemcu works 100% even after reboot With ESP-01you need to setup again (network selection, password and ...) after reboot

Is the hardware problem a software problem

#include <FS.h>                   //this needs to be first, or it all crashes and burns...

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <SimpleTimer.h>
#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
//#define BLYNK_DEBUG

//define your default values here, if there are different values in config.json, they are overwritten.
//char mqtt_server[40];
//char mqtt_port[6] = "8080";
char blynk_token[34] = "YOUR_BLYNK_TOKEN";

//flag for saving data
bool shouldSaveConfig = false;

//callback notifying us of the need to save config
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();

  //clean FS, for testing
  //SPIFFS.format();

  //read configuration from FS json
  Serial.println("mounting FS...");

  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");

          //strcpy(mqtt_server, json["mqtt_server"]);
          //strcpy(mqtt_port, json["mqtt_port"]);
          strcpy(blynk_token, json["blynk_token"]);

        } else {
          Serial.println("failed to load json config");
        }
      }
    }
  } else {  
    Serial.println("failed to mount FS");
  }
  //end read

  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 33);

  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  //set static ip
  //wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

  //add all your parameters here
  //wifiManager.addParameter(&custom_mqtt_server);
  //wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_blynk_token);

  //reset settings - for testing
  //wifiManager.resetSettings();

  //set minimu quality of signal so it ignores AP's under that quality
  //defaults to 8%
  //wifiManager.setMinimumSignalQuality();

  if (!wifiManager.autoConnect("Wifi_Manager", "password")) {
   Serial.println("failed to connect and hit timeout");
   delay(3000);
    //reset and try again, or maybe put it to deep sleep
   ESP.reset();
   delay(5000);
 }

 //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");

 strcpy(blynk_token, custom_blynk_token.getValue());

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    //json["mqtt_server"] = mqtt_server;
    //json["mqtt_port"] = mqtt_port;
    json["blynk_token"] = blynk_token;

    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }

    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
    //end save
  }

  //Serial.println("local ip");
  //Serial.println(WiFi.localIP());

  Blynk.config(blynk_token);
  bool result = Blynk.connect();

if (result != true)
{
  Serial.println("BLYNK Connection Fail");
  Serial.println(blynk_token);
  wifiManager.resetSettings();
  ESP.reset();
  delay (5000);
}
else
{
  Serial.println("BLYNK Connected");
}

}

void loop() {

Blynk.run();

}
tablatronix commented 6 years ago

Did you try erasing the flash?

tablatronix commented 6 years ago

So do you think it is not saving the wifi credentials on save ?

What do the serial logs say ?

tablatronix commented 6 years ago

Hmmm same with the autoconnect example?

ghost commented 6 years ago

autoconnect example

code

#include <ESP8266WiFi.h>            //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>            //https://github.com/tzapu/WiFiManager

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);

//WiFiManager
//Local intialization. Once its business is done,   there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();

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

//fetches ssid and pass from eeprom 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.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();

//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}

void loop() {
// put your main code here, to run repeatedly:

}

serial logsWM: WM: AutoConnect WM: Connecting as wifi client... WM: Using last saved values, should be faster WM: Connection result: WM: 0 WM: SET AP STA WM: WM: Configuring access point... WM: AutoConnectAP WM: AP IP address: WM: 192.168.4.1 WM: HTTP server started WM: Handle root WM: Request redirected to captive portal WM: Request redirected to captive portal WM: Handle root WM: Handle root WM: Scan done WM: Pro-Syrian.com WM: -57 WM: DemirT WM: -73 WM: Emirhan WM: -84 WM: iPhone WM: -96 WM: Sent config page WM: Handle root WM: Request redirected to captive portal WM: Handle root WM: Request redirected to captive portal WM: Handle root WM: Request redirected to captive portal WM: Scan done WM: Pro-Syrian.com WM: -57 WM: DemirT WM: -77 WM: Emirhan WM: -87 WM: iPhone WM: -92 WM: Sent config page WM: Handle root WM: Handle root WM: Handle root WM: Scan done WM: Pro-Syrian.com WM: -58 WM: DemirT WM: -73 WM: iPhone WM: -91 WM: Sent config page WM: Request redirected to captive portal WM: Handle root WM: Request redirected to captive portal WM: WiFi save WM: Sent wifi save page WM: Connecting to new AP WM: Connecting as wifi client... WM: Connection result: WM: 4 WM: Failed to connect. WM: Request redirected to captive portal WM: Handle root WM: Request redirected to captive portal WM: Handle root WM: WiFi save WM: Sent wifi save page WM: Connecting to new AP WM: Connecting as wifi client... WM: Already connected. Bailing out. connected...yeey :)


serial logs After separation of current

WM: AutoConnect WM: Connecting as wifi client... WM: Using last saved values, should be faster WM: Connection result: WM: 3 WM: IP Address: *WM: 192.168.1.102 connected...yeey :)

ghost commented 6 years ago

coode with blynk token


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <Ticker.h>
Ticker ticker;

char blynk_token[33];

void tick()
{
  //toggle state
  int state = digitalRead(LED_BUILTIN); 
  digitalWrite(LED_BUILTIN, !state);  
}

void configModeCallback (WiFiManager *myWiFiManager) {
  ticker.attach(0.2, tick);
}

void setup()
{
    Serial.begin(115200);
    WiFiManagerParameter custom_blynk_token("Blynk", "blynk token", blynk_token, 33);
    WiFiManager wifi;
    wifi.addParameter(&custom_blynk_token);
    wifi.autoConnect("Blynk");
    Blynk.config(custom_blynk_token.getValue());

}

void loop()
{
  Blynk.run();

}

serial

WM: Adding parameter WM: Blynk WM: WM: AutoConnect WM: Connecting as wifi client... WM: Using last saved values, should be faster WM: Connection result: WM: 0 WM: SET AP STA WM: WM: Configuring access point... WM: Blynk WM: AP IP address: WM: 192.168.4.1 WM: HTTP server started WM: Handle root WM: Handle root WM: Request redirected to captive portal WM: Request redirected to captive portal WM: Handle root WM: Request redirected to captive portal WM: Handle root WM: Request redirected to captive portal WM: Handle root WM: Request redirected to captive portal WM: Handle root WM: Handle root WM: Handle root WM: Handle root WM: Scan done WM: Pro-Syrian.com WM: -51 WM: DemirT WM: -78 WM: Sent config page WM: Request redirected to captive portal WM: Handle root WM: Request redirected to captive portal WM: Request redirected to captive portal WM: Request redirected to captive portal WM: WiFi save WM: Parameter WM: Blynk WM: de745e5d128647da853bb17127106bf7 WM: Sent wifi save page WM: Connecting to new AP WM: Connecting as wifi client... WM: Connection result: *WM: 3 [213913]


/ )/ / _ / /_ / / / // / \/ '/ ///_, /////_\ /___/ v0.5.2 on ESP8266

[213915] Connecting to blynk-cloud.com:80 [214073] Ready (ping: 1ms).


serial logs After separation of current WM: Blynk WM: WM: AutoConnect WM: Connecting as wifi client... WM: Using last saved values, should be faster WM: Connection result: WM: 3 WM: IP Address: *WM: 192.168.1.102 [3680]


/ )/ / _ / /_ / / / // / \/ '/ ///_, /////_\ /___/ v0.5.2 on ESP8266

[3687] Connecting to blynk-cloud.com:80 [3831] Invalid auth token [8831] Connecting to blynk-cloud.com:80 [8973] Invalid auth token [13974] Connecting to blynk-cloud.com:80 [14113] Invalid auth token [19114] Connecting to blynk-cloud.com:80 [19251] Invalid auth token [24252] Connecting to blynk-cloud.com:80 [24390] Invalid auth token [29391] Connecting to blynk-cloud.com:80 [29717] Invalid auth token [34718] Connecting to blynk-cloud.com:80 [35149] Invalid auth token [40150] Connecting to blynk-cloud.com:80 [40287] Invalid auth token [45288] Connecting to blynk-cloud.com:80 [45424] Invalid auth token

tablatronix commented 6 years ago

so now you get connection result of 3, sounds like your router

ghost commented 6 years ago

[45288] Connecting to blynk-cloud.com:80 [45424] Invalid auth token

It is connected to the Internet but is offline with Blynk

joeldhenry commented 3 years ago

it appears that wifi manager doesn't save parameters like it does the ssid and password. i think you must implement your own custom saving code, so that after a reboot the token still exists.

tablatronix commented 3 years ago

pretty much yes, WM has no native FS dependancy