Skip to content

WDT reset when using modem sleep #3662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
trentbrown13 opened this issue Sep 30, 2017 · 11 comments
Closed

WDT reset when using modem sleep #3662

trentbrown13 opened this issue Sep 30, 2017 · 11 comments
Labels
type: troubleshooting waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.

Comments

@trentbrown13
Copy link

Basic Infos

Hardware

Hardware: Wemos d1 mini
Core Version: 2.4.0-rc1

Description

I am using modem sleep with the wemos D1 mini which works fine with 2.3.0. After upgrading to 2.4.0-rc1 I get a wdt reset immediately after issuing "WiFi.forceSleepBegin();" I later downgraded back to 2.3.0 and it works fine again. One note, my free heap reported by "ESP.getFreeHeap(); seemed to be much lower when using 2.4.0-rc1 but I was primarily trying to isolate the wdt reset cause. I only noticed this as I am chasing a memory leak problem and printing heap size all over the place.

Settings in IDE

Module: Wemos D1 R2 mini
Flash Size: ?4MB/1MB?
CPU Frequency: ?80Mhz?
Flash Mode: ?qio?
Flash Frequency: ?40Mhz?
Upload Using: SERIAL
Reset Method: ?ck / nodemcu?

Sketch

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>  // for mqtt
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>  
#include "SSD1306.h"
#include <ESP8266mDNS.h> 
#include <ArduinoOTA.h>
#include <WiFiUdp.h> 
#include <cactus_io_BME280_I2C.h>
// ntp includes
#include <TimeLib.h>
//#include "WifiConfig.h" 
#include <NtpClientLib.h>

void setup() {

}

void loop() {
************ snip *************
 getTemperatures();
     
     client.publish("/TbOffice/Tub/temperature", tubTempFString);
     client.publish("/TbOffice/Out/temperature", outTmpFString);

    Serial.print(" End loop number: ");
    Serial.println(x);
   
  lcdDisplayTemps();
  yield();
  } // end for loop

 Serial.println("Going down for a 60 second nap");
 yield();
 //delay(5000);
 //Serial.println(F("just before Wifi.forceSleepBegin"));
  WiFi.forceSleepBegin();

  ///////////// NEVER GETS HERE when using 2.4.0-rc1
  
 //Serial.println(F("just after Wifi.forceSleepBegin"));
 delay(1 * sleepM); // 10 seconds for test
}
************** end snip ***************

Stack Trace

Soft WDT reset

ctx: cont
sp: 3fff0990 end: 3fff0c00 offset: 01b0

stack>>>
3fff0b40: 0001260d 00000600 3ffef794 4021ad48
3fff0b50: 0fffffff 40203d5e 00000000 40203d49
3fff0b60: 3ffefb18 3ffef914 3ffef794 40203452
3fff0b70: 00000000 00000000 00000000 4020a77c
3fff0b80: 00000000 00000000 00000000 00000000
3fff0b90: 00000000 00000000 00000000 00000000
3fff0ba0: 00000000 00000000 00000000 00000000
3fff0bb0: 3ffe9730 b564a8c0 3ffe9730 b564a8c0
3fff0bc0: 00000006 42955b23 4479c281 feefeffe
3fff0bd0: feefeffe feefeffe feefeffe 3ffefbd8
3fff0be0: 3fffdad0 00000000 3ffefbd0 4020b4b0
3fff0bf0: feefeffe feefeffe 3ffefbe0 4010070c
<<<stack<<<

ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
vf6d232f1
~ld

Decoding 8 results
0x4021ad48: ieee80211_setup_ratetable at ?? line ?
0x40203d5e: ESP8266WiFiGenericClass::enableSTA(bool) at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/ESP8266WiFiGeneric.cpp line 96
0x40203d49: ESP8266WiFiGenericClass::getMode() at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/ESP8266WiFiGeneric.cpp line 96
: (inlined by) ESP8266WiFiGenericClass::enableSTA(bool) at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi\src/ESP8266WiFiGeneric.cpp line 329
0x40203452: loop at D:\Programming\ESP\Weather nodes\Trents_Office_Msleep1G/Trents_Office_Msleep1G.ino line 738 (discriminator 3)
0x4020a77c: String::changeBuffer(unsigned int) at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/WString.cpp line 720
0x4020b4b0: String::move(String&) at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/WString.cpp line 205
: (inlined by) String::String(String&&) at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/WString.cpp line 51
0x4010070c: cont_resume at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/cont.S line 99

@igrr
Copy link
Member

igrr commented Oct 6, 2017

Sorry, i tried running the sketch you provided, but it does not compile. Could you please check and make sure that the sketch can be compiled and reproduces the issue? Thanks.

@trentbrown13
Copy link
Author

Sorry Ivan, that was just a code snippet. Here is another example

#include <TimeLib.h>
#include "WifiConfig.h"
#include <NtpClientLib.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//#include <DHT.h>
#define DEBUG 1

//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

extern "C" {
#include "gpio.h"
}

extern "C" {
#include "user_interface.h"
}
/*#ifndef WIFI_CONFIG_H
#define YOUR_WIFI_SSID BlueZebra
#define YOUR_WIFI_PASSWD jY7bzy2XJv
#endif // !WIFI_CONFIG_H
*/

#ifndef WIFI_CONFIG_H
#define YOUR_WIFI_SSID "YOUR_WIFI_SSID"
#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD"
#endif // !WIFI_CONFIG_H

//const char* YOUR_WIFI_SSID = "BlueZebra";
//const char* YOUR_WIFI_PASSWD = "jY7bzy2XJv";
const char* ssid = "BlueZebra";
const char* password = "jY7bzy2XJv";
//const char* ssid = "SRGTEST_2G";
//const char* password = "5martRG!";

static int connectAttempts = 0;
static int connectPass = 0;
static int connectFail = 0;
long previousMillis = 0;
long interval = 5100;

const char* ahostname = "MQTT_Test";
//#define ONBOARDLED 2 // Built in LED on ESP-12/ESP-07
//#define WIFI_PASS_LED 5
//#define WIFI_FAIL_LED 4

#define SLEEPM 60000 // one minute

//int8_t timeZone = 1;

int8_t timeZone = -8; // PST

// Start NTP only after IP network is connected
void onSTAGotIP(WiFiEventStationModeGotIP ipInfo) {
//NTP.stop();
//pinMode(WIFI_PASS_LED, OUTPUT);
//pinMode(WIFI_FAIL_LED, OUTPUT);
Serial.printf("Got IP: %s\r\n", ipInfo.ip.toString().c_str());
connectPass ++;
//digitalWrite(WIFI_PASS_LED, HIGH);
//delay(1000);
//digitalWrite(WIFI_PASS_LED, LOW);
//Serial.println("Just before NTP.begin - CRASHES HERE");
NTP.begin("pool.ntp.org", timeZone, true);
//Serial.println("Just after NTP.begin and before ntp.setInterval");
NTP.setInterval(63);
//Serial.println("Just after ntp.setInterval");
//digitalWrite(ONBOARDLED, LOW); // Turn on LED
}

// Manage network disconnection
void onSTADisconnected(WiFiEventStationModeDisconnected event_info) {
Serial.printf("Disconnected from SSID: %s\n", event_info.ssid.c_str());
Serial.printf("Reason: %d\n", event_info.reason);
//digitalWrite(ONBOARDLED, HIGH); // Turn off LED
//NTP.stop(); // NTP sync can be disabled to avoid sync errors
}

void processSyncEvent(NTPSyncEvent_t ntpEvent) {
if (ntpEvent) {
Serial.print("Time Sync error: ");
if (ntpEvent == noResponse)
Serial.println("NTP server not reachable");
else if (ntpEvent == invalidAddress)
Serial.println("Invalid NTP server address");
}
else {
Serial.print("Got NTP time: ");
Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
}
}

boolean syncEventTriggered = false; // True if a time even has been triggered
NTPSyncEvent_t ntpEvent; // Last triggered event

void setup()
{
static WiFiEventHandler e1, e2;

Serial.begin(115200);
Serial.println();
//WiFi.mode(WIFI_STA);
//WiFi.begin(YOUR_WIFI_SSID, YOUR_WIFI_PASSWD);

//connectAttempts++;
//pinMode(ONBOARDLED, OUTPUT); // Onboard LED
//digitalWrite(ONBOARDLED, HIGH); // Switch off LED
//pinMode(WIFI_PASS_LED, OUTPUT);
//digitalWrite(WIFI_PASS_LED, LOW);
//pinMode(WIFI_FAIL_LED, OUTPUT);
//digitalWrite(WIFI_FAIL_LED, LOW);
//digitalWrite(WIFI_PASS_LED, HIGH);
//delay(1000);
//digitalWrite(WIFI_PASS_LED, LOW);

NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
ntpEvent = event;
syncEventTriggered = true;
});

// Deprecated
/*WiFi.onEvent([](WiFiEvent_t e) {
	Serial.printf("Event wifi -----> %d\n", e);
});*/

e1 = WiFi.onStationModeGotIP(onSTAGotIP);// As soon WiFi is connected, start NTP Client
e2 = WiFi.onStationModeDisconnected(onSTADisconnected);

// **************** WIFI connect section ***************
Serial.println();
connectAttempts += 1;
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA); // try commenting if OTA does not work
WiFi.persistent(false);
//WiFi.config(ip, gateway, subnet, dns);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
connectFail += 1;
//digitalWrite(WIFI_FAIL_LED, HIGH);
//delay(1000);
//digitalWrite(WIFI_FAIL_LED, LOW);
delay(1000);
ESP.restart();
}
connectPass += 1;

}

void loop()
{
//static int i = 0;
//static int last = 0;
unsigned long currentMillis = millis();
static int i = 0;

if (syncEventTriggered) {
	processSyncEvent(ntpEvent);
	syncEventTriggered = false;
}

/*
if ((millis() - last) > 5100) {
//Serial.println(millis() - last);
last = millis();

*/
if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
// Serial.print(i); Serial.print(" ");
Serial.println(F("getting 5 NTP readings before going to sleep"));
for(i = 1; i<=5; i++)
{
yield();

	//Serial.print(i); Serial.print(" ");
Serial.print(NTP.getTimeDateString()); Serial.print(" ");
	Serial.print(NTP.isSummerTime() ? "Summer Time. " : "Winter Time. ");
	Serial.print("WiFi is ");
	Serial.print(WiFi.isConnected() ? "connected" : "not connected"); Serial.print(". ");
	Serial.print("Uptime: ");
	Serial.print(NTP.getUptimeString()); Serial.print(" since ");
	Serial.println(NTP.getTimeDateString(NTP.getFirstSync()).c_str());
#ifdef DEBUG
Serial.println("Free Heap      = " + String(ESP.getFreeHeap()));
Serial.println("Last Reset Reason      = " + String(ESP.getResetReason()));
#endif
Serial.print("End Get NTP loop number ");
Serial.println(i);
delay(5000);

}
Serial.println("Done working hard, going to take a nap");
Serial.println("");

//ESP.deepSleep(SLEEPM * 1, WAKE_RF_DEFAULT);

// print connection results
connectAttempts++;
//connectPass += 1;
Serial.println("*****************************************************************");
Serial.print("Connect Attempts ");
Serial.print(connectAttempts);
Serial.print("\tConnect Complete ");
Serial.print(connectPass);
Serial.print("\tConnect Fail ");
Serial.println(connectFail);
Serial.println("*****************************************************************");
Serial.println(""); 

Serial.print("wifi should be off for 10 seconds, try pinging ");
Serial.println(WiFi.localIP());
#ifdef DEBUG
Serial.println(F("Just before NTP.stop and wifi disconnect"));
Serial.println("Free Heap      = " + String(ESP.getFreeHeap()));
#endif
NTP.stop();
delay(1000);
WiFi.disconnect();
#ifdef DEBUG
Serial.println(F("Just after wifi disconnect and before force sleep begin"));
Serial.println("Free Heap      = " + String(ESP.getFreeHeap())); 
#endif
//delay(5000);
WiFi.forceSleepBegin();
#ifdef DEBUG
Serial.println(F("Just after force sleep and before 1st delay"));
Serial.println("Free Heap      = " + String(ESP.getFreeHeap())); 

delay(5000);
//Serial.println(F("Just after 1st delay"));
//Serial.println("Free Heap      = " + String(ESP.getFreeHeap())) ;  

// delay(10000);
delay(5000);

Serial.println(F("Just after 2nd delay"));
Serial.println("Free Heap      = " + String(ESP.getFreeHeap()));
#endif
delay(5000);
#ifdef DEBUG
Serial.println(F("Just after 3rd delay"));
Serial.println("Free Heap      = " + String(ESP.getFreeHeap()));
#endif
WiFi.forceSleepWake();
Serial.println("wifi should be back on");
#ifdef DEBUG
Serial.println(F("Just after forcesleepwake"));
//Serial.println("Free Heap      = " + String(ESP.getFreeHeap()));
#endif
//yield();
//WiFi.begin(YOUR_WIFI_SSID, YOUR_WIFI_PASSWD);

Serial.println("Doing WiFi Begin");
//WiFi.persistent(false);
WiFi.begin(ssid, password);
#ifdef DEBUG
Serial.println(F("Just wifi begin"));

//Serial.println("Free Heap = " + String(ESP.getFreeHeap()));
#endif
Serial.println("Done WiFi Begin");
Serial.println("Starting 10 sec Delay");
delay(10000);
Serial.println("Finished 10 sec Delay");
//digitalWrite(WIFI_PASS_LED, HIGH);
// delay(1000);
//digitalWrite(WIFI_PASS_LED, LOW);
//pinMode(ONBOARDLED, OUTPUT); // Onboard LED
//digitalWrite(ONBOARDLED, HIGH); // Switch off LED
delay(500);
#ifdef DEBUG
Serial.println(F("Just before on NTP sync event"));
Serial.println("Free Heap = " + String(ESP.getFreeHeap()));
#endif
NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
ntpEvent = event;
syncEventTriggered = true;
#ifdef DEBUG
Serial.println(F("Just after sync event triggered"));
Serial.println("Free Heap = " + String(ESP.getFreeHeap()));
#endif
});

//e1 = WiFi.onStationModeGotIP(onSTAGotIP);// As soon WiFi is connected, start NTP Client

// e2 = WiFi.onStationModeDisconnected(onSTADisconnected);

}
//delay(0);

}

@trentbrown13
Copy link
Author

trentbrown13 commented Oct 18, 2017

Not sure if you wouold like like a new ticket opened or just add to this one. This time running 2.3.0 with a much simplified sketch.
stack Decode:

decoding 21 results
0x40104927: lmacRxDone at ?? line ?
0x401004d8: malloc at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266\umm_malloc/umm_malloc.c line 1664
0x40107084: pvPortMalloc at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/heap.c line 13
0x40222670: pbuf_alloc at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/pbuf.c line 366
0x4022550e: udp_sendto at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/udp.c line 450
0x40222b9f: sntp_initialize_request at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/sntp.c line 698
:  (inlined by) sntp_send_request at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/sntp.c line 885
0x40222bb3: sntp_send_request at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/sntp.c line 889
0x40105cdc: sntp_dns_found at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/sntp.c line 917
0x40221cf5: dns_recv at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/dns.c line 825
0x40221d04: dns_recv at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/dns.c line 826
0x40210400: scan_parse_beacon at ?? line ?
0x402252a8: udp_input at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/udp.c line 343
0x40107084: pvPortMalloc at C:\Users\Windows7\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/heap.c line 13
0x402269e4: ip_input at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/core/ipv4/ip.c line 553
0x40225ded: ethernet_input at /Users/igrokhotkov/espressif/arduino/tools/sdk/lwip/src/netif/etharp.c line 1379
0x40205faf: pp_tx_idle_timeout at ?? line ?
0x40205f22: pp_tx_idle_timeout at ?? line ?
0x4021a01b: ets_snprintf at ?? line ?

Sketch:

#define HEAPCHECKER 1        // set to 1 to test HEAP loss fix
#ifdef HEAPCHECKER
  #include "lwip/tcp_impl.h" // losing bytes work around
#endif
#include <TimeLib.h>
//#include "WifiConfig.h"
#include <NtpClientLib.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define DEBUG 1
#define NTP_ON 1

extern "C" {
#include "gpio.h"
}

extern "C" {
#include "user_interface.h"
}


int count_sleep = 0;
int count_wake = 0;
uint32_t originalram;


//*************** SSID Stuff *********************************
const char* ssid = "BlueZebra";
const char* password = "jY7bzy2XJv";
//const char* ssid = "SRGTEST_2G";
//const char* password = "5martRG!";
//**********************************************************************

//******************* Connect Attempt ***********************************
static int connectAttempts = 0;
static int connectPass = 0;
static int connectFail = 0;
//*********************************************************************

//************** Millis stuff *****************************************
unsigned long previousMillis = 0;
const unsigned long sleeptime = 30000; // milliseconds
bool awake = true;  //flag for waking up and getting busy
//*********************************************************************



//********************* NTP Stuff ***********************************
int8_t timeZone = -8; // PST

// Start NTP only after IP network is connected
void onSTAGotIP(WiFiEventStationModeGotIP ipInfo) {
  Serial.printf("Got IP: %s\r\n", ipInfo.ip.toString().c_str());
  connectPass ++;
  //#ifdef NTP_ON
  //NTP.begin("pool.ntp.org", timeZone, true);
  //NTP.setInterval(63);
  
 // #endif
  //digitalWrite(ONBOARDLED, LOW); // Turn on LED
}

// Manage network disconnection
void onSTADisconnected(WiFiEventStationModeDisconnected event_info) {
  Serial.printf("Disconnected from SSID: %s\n", event_info.ssid.c_str());
  Serial.printf("Reason: %d\n", event_info.reason);
  NTP.stop();
 
}
#ifdef NTP_ON
void processSyncEvent(NTPSyncEvent_t ntpEvent) {
  if (ntpEvent) {
    Serial.print("Time Sync error: ");
    if (ntpEvent == noResponse)
      Serial.println("NTP server not reachable");
    else if (ntpEvent == invalidAddress)
      Serial.println("Invalid NTP server address");
  }
  else {
    Serial.print("Got NTP time: ");
    Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
  }
}

boolean syncEventTriggered = false; // True if a time event has been triggered
NTPSyncEvent_t ntpEvent; // Last triggered event
#endif
//****************************************************************************************



//************************** Losing bytes work around ************************************
void tcpCleanup()   // losing bytes work around
{
  while(tcp_tw_pcbs!=NULL)
  {
    tcp_abort(tcp_tw_pcbs);
  }
}
//******************************************************************************************







void setup() {
  //Commenting out event handler stuff in VG, just start NTP once
 // static WiFiEventHandler e1, e2;
  //timeZone = -8; // PST
  //timeZone = -17; // PST ??
  Serial.begin(115200);
  Serial.println(F("In setup, runs once"));
  Serial.println(F("Before declaring static WiFIEventHandler e1, e2"));
  Serial.println("Free Heap      = " + String(ESP.getFreeHeap(),DEC));
  //*************** NTP setup **************************************
  /*
  NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
    ntpEvent = event;
    syncEventTriggered = true;
  });
  */
// **************** WIFI connect section ***************
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);   // try uncommenting if OTA does not work
  WiFi.persistent(false);
  //WiFi.config(ip, gateway, subnet, dns);

  // Need to put connectAttempts and connetPass into eventhandlers
  WiFi.begin(ssid, password);
  connectAttempts ++;
  
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      delay(200);
  }
// Added this in VG, jsut start NTP once
  connectPass ++;
  NTP.begin("pool.ntp.org", timeZone, true);
  NTP.setInterval(63);
  
  //Commenting out event handler stuff in VG, just start NTP once
/*
   NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
    ntpEvent = event;
    syncEventTriggered = true;
  });
  */
  
/*  
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }
  */
  //Commenting out event handler stuff in VG, just start NTP once
  //e1 = WiFi.onStationModeGotIP(onSTAGotIP);// As soon WiFi is connected, start NTP Client
  //e2 = WiFi.onStationModeDisconnected(onSTADisconnected);
}

void loop()
{
  //static WiFiEventHandler e1, e2;
  int i;
  uint32_t ram = ESP.getFreeHeap();
 
 
 #ifdef NTP_ON 
  if (syncEventTriggered) {
    processSyncEvent(ntpEvent);
    syncEventTriggered = false;
  }
  #endif
  //static WiFiEventHandler e1, e2;  // might move to global moved to static TestD ?
  Serial.println(F("At top of loop now,"));
  Serial.println("Free Heap      = " + String(ESP.getFreeHeap(),DEC));
  WiFi.persistent(false);
  WiFi.mode(WIFI_OFF);
  WiFi.mode(WIFI_STA);
  // get the current time at the start of each loop
  unsigned long currentMillis = millis();

  if (awake == true) {
    //WiFi.mode(WIFI_ON);

  Serial.printf("RAM: %d  change %d\n", ram, (ram - originalram ));

  
  /*
  if(HEAPCHECKER){          // losing bytes work around
    tcpCleanup();           
    Serial.printf("tcpCleanup completed\n");
  }
  */
    Serial.println(F("awake = true, getting to work"));
    Serial.println("Free Heap      = " + String(ESP.getFreeHeap(),DEC));

    Serial.println(F("starting Wifi"));
    Serial.print(F("Connecting to "));
    Serial.println(ssid);
    // try commenting if OTA does not work

    //WiFi.config(ip, gateway, subnet, dns);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  connectPass ++;
  #ifdef NTP_ON
  delay(1000);
 /*
  NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
    ntpEvent = event;
    syncEventTriggered = true;
  });
  
  //e1 = WiFi.onStationModeGotIP(onSTAGotIP);// As soon WiFi is connected, start NTP Client
  //e2 = WiFi.onStationModeDisconnected(onSTADisconnected);
  */
  #endif
  
    Serial.println(F("Right after WiFi.begin and before NTP crap"));
    Serial.println("Free Heap      = " + String(ESP.getFreeHeap(),DEC));
  #ifdef NTP_ON
    Serial.println(F("getting 5 NTP readings before going to sleep"));
    for (i = 1; i <= 5; i++)
    {
      yield();

      //Serial.print(i); Serial.print(" ");
      Serial.print(NTP.getTimeDateString()); Serial.print(" ");
      Serial.print(NTP.isSummerTime() ? "Summer Time. " : "Winter Time. ");
      Serial.print("WiFi is ");
      Serial.print(WiFi.isConnected() ? "connected" : "not connected"); Serial.print(". ");
      Serial.print("Uptime: ");
      Serial.print(NTP.getUptimeString()); Serial.print(" since ");
      Serial.println(NTP.getTimeDateString(NTP.getFirstSync()).c_str());
//#ifdef DEBUG
      Serial.println("Free Heap      = " + String(ESP.getFreeHeap(),DEC));
      Serial.println("Last Reset Reason      = " + String(ESP.getResetReason()));
//#endif
      Serial.print("End Get NTP loop number ");
      Serial.println(i);
      delay(1000);
    }
#endif

    Serial.println("Done working hard, going to take a nap");
    Serial.println("");


    // print connection results
    connectAttempts++;
    //connectPass += 1;
    Serial.println("*****************************************************************");
    Serial.print("Connect Attempts ");
    Serial.print(connectAttempts);
    Serial.print("\tConnect Complete ");
    Serial.print(connectPass);
    Serial.print("\tConnect Fail ");
    Serial.println(connectFail);
    Serial.println("*****************************************************************");
    Serial.println("");
    Serial.println(F("Right after print connect attempts info"));
    Serial.println("Free Heap      = " + String(ESP.getFreeHeap(),DEC));
  
     // Serial.println(F("Getting NTP stuff"));
     // NTP.begin("pool.ntp.org", timeZone, true);
     //Serial.println("Just after NTP.begin and before ntp.setInterval");
     //NTP.setInterval(63);
     //Serial.println("Free Heap      = " + String(ESP.getFreeHeap(),DEC));
    Serial.println(F("Done Working, taking a nap"));  //
    Serial.println("Free Heap      = " + String(ESP.getFreeHeap(),DEC));
     // put into sleep mode here
     //NTP.stop();
     
     //Serial.println(F("Getting rid of TCP stuff"));
     //Serial.println(F("Getting rid of NTP stuff"));
     count_sleep += 1;
     Serial.print(F("sleep count = "));
     Serial.println(count_sleep);
     #ifdef NTP_ON
     NTP.stop();
     #endif
     WiFi.disconnect();
     WiFi.mode(WIFI_OFF);
 
     Serial.println(F("************ forceing modem sleep *******************"));
     WiFi.forceSleepBegin();
     delay(1);
     Serial.println(F("**************** Setting awake to False ***************"));
     awake = false;
     //e2 = WiFi.onStationModeDisconnected(onSTADisconnected);

     Serial.println(F("*************** End if awake = TRUE **********************"));
         }  // End if awake = true
     else {
       delay(1000);
       Serial.println(F("*************** Still sleeping but checking TIME **********************"));
       Serial.print(F("sleeping for  = "));
       Serial.print(sleeptime /1000);
       Serial.print(F(" seconds, Time Elapsed = "));
       Serial.print((currentMillis - previousMillis)/1000);
       Serial.println(F(" seconds"));
       Serial.println(F("***********************************************************************"));
     }
         if ((unsigned long)(currentMillis - previousMillis) >= sleeptime) {
         Serial.println(F("**************Time to Wake up and get to work!******************"));
         Serial.println(F("************** awake = TRUE ******************"));
             awake = true;
             count_wake += 1;
             Serial.print(F("Wake count = "));
                 Serial.println(count_wake);
                 Serial.println(F("**************** Waking up WiFi ***************"));
                 WiFi.forceSleepWake();
                 previousMillis = millis();
               }
   }

This crash appears at random times, perhaps I need to reformat the flash?
Please let me know if I should add updates etc in a better manor

@trentbrown13
Copy link
Author

Sorry, did not mean to close this ticket. I reformatted the flash with esptool and uploaded again. monitoring for now.
thanks
-trent

@devyte
Copy link
Collaborator

devyte commented Oct 24, 2017

@trentbrown13 what was the result of your monitoring?

@devyte devyte added the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label Oct 24, 2017
@trentbrown13
Copy link
Author

It was still crashing after re-flashing and I am now downgraded again to 2.3 which is running flawlessly on several esp's. I'll try upgrading to 2.4.0-rc2 soon and try again on a few devices. Thanks for the help!

@gsimard
Copy link

gsimard commented Jan 1, 2018

I too get this issue with 8edeac0.

The issue seems to be timing related and is hard to pin point.

Essentially I do:

setup() {
  // Credentials are known from a previous WPS transaction and retrieved from EEPROM.
  WiFi.begin(WiFi.SSID().c_str(),WiFi.psk().c_str());
  // Some more code to wait the above to complete. Connects fine.
  ...
}

loop() {
  // This runs fine although it takes a bit of time to complete the transaction
  // after remote end has hanged up
  doHttpsGetTransaction(); 

  // Reset WDT in case we blocked without yield'ing for a while, but shouldn't be necessary
  ESP.wdtFeed();

  // Turn the WiFi off before we sleep
  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();

  // The WDT (soft or hard if I disable the software wdt) trips HERE, before deepSleep.

  ESP.deepSleep(20e6, WAKE_RF_DEFAULT);
}

If I do NOT call doHttpsGetTransaction, the ESP8266 goes to sleep and the WDT does NOT trip.

@gsimard
Copy link

gsimard commented Jan 2, 2018

Turns out reducing the timeout value of the WiFiSecureClient from 15s to 100ms helps a lot with this issue.

Indeed after the peer hangs up on the connection it took a while for my sketch to take notice and this is due to Stream::timedRead() waiting for _timeout = 15000 set in WiFiClientSecure::WiFiClientSecure().

It looks like shaving these previous seconds allows the return from loop() and the hardware WDT being reset on time.

I don't like the feel of this though as I expect to be bitten again later down the road.

Is there a maximum absolute amount of time one should stay in loop before returning ? Does calling ESP.wdtFeed() reset the hardware WDT or just the software WFT ?

Best regards,

@SunboX
Copy link

SunboX commented Aug 8, 2018

Can confirm, WiFi.forceSleepBegin(); triggers a Soft WDT reset also for me. Commenting out that line works.

@trentbrown13
Copy link
Author

As with issue #4082, WiFiForceSleepBegin() still caused a WDT reset with core 2.5 until I added a delay(1000) just before it. With this delay, my sketch has been running for 2+ days with a WiFiForceSleepBegin() every 60 seconds or so.

Not sure if the "waiting for feed back" label is still pertinent, let me know if I can provide more info.

@earlephilhower
Copy link
Collaborator

Wow, looks like WiFi power down has been an issue a looong time. Closing as we've got an active discussion of this in #6172 where folks are making some progress, I believe. This will also make a link there to this discussion so they can avoid trying the same dead-ends you ran into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: troubleshooting waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.
Projects
None yet
Development

No branches or pull requests

6 participants