tzapu / WiFiManager

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

ESP-12E nodemcu unable to reconnect to WIFI after deepsleep #729

Open kevcavein opened 5 years ago

kevcavein commented 5 years ago

Hi all,

I'm facing a problem that my ESP12E does not connect to my wifi after wake up from deepsleep. It happen randomly. Sometimes it happens just the second time after wake up from deep sleep, sometimes it happens after 2hrs and sometimes even 12hrs. After disconnecting, it will launch the AP webpage waiting for wifi SSID and password. I'm using the latest wifimanger 2.4.2 and even revert to 2.4.0 before but still the same. I'm very new to Arduino and my sample is mainly cut and paste from different reference. Besides, i place the device very near to my wifi setup, should not be the wifi signal strength problem.

If this a same topic from another threat, please reference me to there. Most same topic seems to be 2years or 7months ago(which is closed).

Thanks in advance.

Below is my sketch.

include //this needs to be first, or it all crashes and burns...

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

include

include

include

include //https://github.com/tzapu/WiFiManager

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

char blynk_token[34] = "BLYNK_TOKEN";

bool shouldSaveConfig = false; //flag for saving data

include

include

SimpleTimer timer;

include

include

define ONE_WIRE_BUS 4 // Your ESP8266 pin (ESP8266 GPIO 4 = pin D2)

OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire);

int roomTemperature; //temperature in C

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

void sendTemps() { sensors.requestTemperatures(); // Polls the sensors. roomTemperature = sensors.getTempCByIndex(0); // Stores temperature. Change to getTempFByIndex(0) for ferenheit. Blynk.virtualWrite(1, roomTemperature); // Send temperature to Blynk app virtual pin 1. Serial.println(""); Serial.println("I'm gooooing to sleeeeeeeeep....ZZZZzzzzzzzzz"); ESP.deepSleep(900e6,WAKE_RF_DEFAULT); delay(100); Serial.println("*****"); }

void setup() {

Serial.begin(115200); Serial.println(); Serial.println("Mounting FS..."); //read configuration from FS json

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(blynk_token, json["blynk_token"]); } else { Serial.println("Failed to load json config"); } } } } else { Serial.println("Failed to mount FS"); }

WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34); // was 32 length Serial.println(blynk_token); WiFiManager wifiManager; wifiManager.setSaveConfigCallback(saveConfigCallback); //set config save notify callback wifiManager.addParameter(&custom_blynk_token); //add all your parameters here wifiManager.setTimeout(600); // 10 minutes to enter data and then Wemos resets to try again. 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.reset(); delay(5000); } Serial.println("Connected To Access Point"); //if you get here you have connected to the WiFi strcpy(blynk_token, custom_blynk_token.getValue()); //read updated parameters

if (shouldSaveConfig) { //save the custom parameters to FS Serial.println("saving config"); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); 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();

}

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

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

sensors.begin(); // Starts the DS18B20 sensor(s). sensors.setResolution(10);
timer.setInterval(2000L, sendTemps); // Temperature sensor read interval. 2000 (ms) = 2 seconds.

}

void loop() { Blynk.run(); // Initiates Blynk timer.run(); // Initiates SimpleTimer
}

tablatronix commented 5 years ago

maybe try hotfixes branch or development branch of WM

or provide some debug logs

kevcavein commented 5 years ago

Sorry may I know what is hotfix branch or development branch? Sorry.. I'm very new..