Lolin ESP32 devkit fails to restart after power fail

I have an ESP32 monitoring my web connectivity by pinging a remote site.
Its supplied from a powered USB hub.
Last night we were expecting a thunderstorm so I removed the mains supply (so the equipment was isolated.)

This morning when I reconnected the equipment I noticed the board had not restarted; (on board power LED on but no other activity) but after a reset via the button on the board all is working again.
As the connected PC wasnt turned on I have no knowledge of the serial output, but after I turned it on there were no messages until I reset it.
The web page was not being displayed, and the browser said the page was unavailable so likely the MDNS also not working.

The sketch is too long to post, but the code following the setup() I feel isnt likely to be relevant anyway, so here is the setup() code.

void setup() {
  Serial.begin(BAUDRATE);
  pinMode(trigger, INPUT_PULLUP);
  delay(5000);  //allow to start & clear serial monitor
  Serial.println();
  Serial.println(__FILE__);  //print name of sketch for easy identification
  pingSamples = 60.0 / timeInterval;
  initIO();          //initialise hardware IO devices all leds off
  flashAll(5, 200);  //test leds
  Serial.println("Serial comms established");
  attachInterrupt(digitalPinToInterrupt(trigger), ISR, RISING);

  if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {  //start LittleFS system
    Serial.println("LittleFS Mount Failed");
    return;
  }

  getCreds();     //in wifi tab; recover credentials that were saved in the int response
  connectWiFi();  //also gets time from time server ntp
  buildStrings(); //in webData.ino - creates local copies of time server values int YYYY, MM, DD, hh, mm, ss; 

  pRecoverStats();  //retrieve stats (preferences) after reset power fail
  prepareStats();   //put them into strings for web page

  // Initialize mDNS
  if (!MDNS.begin(mDNSname)) {  // Set the hostname 
    Serial.println("Error setting up MDNS responder!");
  }
  Serial.println("mDNS responder started");

  manageServer();  //handle web page requests

  addToFile(LittleFS, "/connData.csv", messRestart);  //save a line to show unexpected reset has occurred
  fileHead();                                         //repeat heading after restart event
  //dont allow newMin or newH to run as they reset counts recovered from pRecoverStats();
  currMin = mm;
  currHour = hh;
  currDay = DD;
  currMonth = MM;
  firstHour = 1;  //following restart no valid hourly data to display
  timeWas = millis();  //used for timing pings in loop
  wpUpdate = millis(); //times the web page update
}

Normally when I remove and reinstate the power it resets as we would expect.

Any suggestions?

Maybe some code is needed to detect the power failure and do a clean restart.

Is this phenomenon of a bad startup repeatable on demand ?

You need to know if the ESP32 actually starts running your code when power is restored.

Maybe turn a LED on as the first instruction in setup() ?

I don't actually know what happens when power is applied, does it ALWAYS start at a specific point in the code? What code, the user uploaded code, or the 'OS'. I have never seen a diagram of what happens but I have no difficulty believing the OP and therefore that it is NOT the first setup instruction.

Not as far as I can see.

"If a tree falls in the forest and no-one is there does it make a sound?"
The project is for continuous monitoring and recording (amongst other things) a power fail. I cant watch the LED all the time. Setup() includes a sequence to flash the LEDs .

I did a search and found this
https://arduino.stackexchange.com/questions/25448/bootloader-sequence
so if its right the "setup()" and "loop()" live inside a control program.
If my understanding is correct the removal and reinstatemnet of the supply should have the same effect as a reset. So why has it not in this case?

int main(void) {
    init();
    setup();
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
    return 0;
}

Following my own advice heres a schematic: I've added the decoupling cap to see if a slower power up would make the issue show up - it didnt.
How to make a schematic you can post. - Using Arduino / Introductory Tutorials - Arduino Forum

This is going to be very difficult to trouble shoot if you can't simulate the conditions which caused this failed startup by switching off power in a controlled way, to various devices, and later restoring it.
Such a test would include switching off the Internet router to ensure the ESP32 can recover from a dropout of the WLAN either at power up of the ESP32 or during its normal operation.

I suppose you could try an uninteruptable power supply and wait for the next thunderstorm to see if it works.

You could also have a task which checks for a continuous loss of connectivity and forces a clean restart periodically.

Indeed, and that was not suggested.

In the OP you described that the board had not restarted, after the power had been reconnected presumably, and that you had to go and reset the board. Thus at that point if a LED on the board was on you would know if the sketch had actually started to run after power was restored.

Mains supply to the house?
Did you wait until the router had restarted, before connecting the ESP.
I use WiFiManager with a portal and a 120sec timeout for that reason.
Leo..

could be a strapping pin in the wrong state during boot..
not sure what pins you have attached to what, so may not be the case but worth a look..

yes, but with esp32 a bit more to it..

i'm thinking it was in uploading mode..

good luck.. ~q

often the reset vector or bootloader, not directly user code

The on-board lights showed power was on, but the serial monitor showed the board was not responding.

Hadnt thought of that. Its just on a breadboard with no connections other then what are shown in the schematic ; possibly static? As I said it normally restarts from a power down (however that occurs) with no issues.

Have you tried just switching off the internet router for a few minutes and then switching it back on again? It may be that the ESP gets stuck in a loop while trying but failing to restore the connection giving the impression that it [the ESP] did not start correctly.