Wemos D1 Mini ESP8266 - WiFi Crash

I'm at a loss on this, I've been working on a project with a TOF sensor and I have now had 2 D1 mini develop a fault and crash when calling WiFi.disconnect() or WiFi.begin()

It is possible for code to screw up a ESP8266? I have run is through the ESP Exception Decoder but not sure what I'm looking at?

Any help would be great

Thanks

Brian

#include <Arduino.h>
#include <ESP8266WiFi.h>

enum AccessMode_t : uint8_t
{
  STANDALONE,
  MTLNET,
  LOCALNET
};

AccessMode_t MODE = LOCALNET;

/////////////////////////////////////////////////////////////////////////////

void setup()
{
  Serial.begin(115200);

  switch (MODE)
  {
    case MTLNET:
      {
        break;
      }
    case LOCALNET:
      {
        Serial.print("Setting LOCAL NETWORK ... ");
        WiFi.disconnect(); // <<<<<< CRASH
        WiFi.mode(WIFI_STA);
        WiFi.begin("SSID", "PASSWORD"); // <<<<<< CRASH
        uint8_t t_count = 0;
        while (WiFi.status() != WL_CONNECTED)
        {
          delay(500);
          Serial.print("|");
          ++t_count;
          if (t_count == 48)
          { // 20 seconds 40
            Serial.println("Failed to connect to network");
            WiFi.disconnect();
            while (1)
              ;
          }
        }

      }
      break;
    case STANDALONE:
      {
      }
      break;
  }
}


//////////////////////////////////////////////////////////////

void loop()
{

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

load 0x4010f000, len 3584, room 16 
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
Setting LOCAL NETWORK ... 
--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (3):
epc1=0x40100718 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4006e989 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffc30 end: 3fffffc0 offset: 0190
3ffffdc0:  00000020 003ff000 3ffee3d4 00000030  
3ffffdd0:  feefeffe feefeffe feefeffe feefeffe  
3ffffde0:  feefeffe feefeffe feefeffe 3fffff20  
3ffffdf0:  0000049c 0000049c 00000020 40100900  
3ffffe00:  feefeffe feefeffe feefeffe feefeffe  
3ffffe10:  00000000 400042db 000003fd 40100b50  
3ffffe20:  40004b31 00001000 000003fd 4010027c  
3ffffe30:  40105b24 feefeffe feefeffe 4022dec5  
3ffffe40:  40100c2d 4022dfaf 3ffef124 0000049c  
3ffffe50:  000003fd 3fffff20 3ffef124 4022df92  
3ffffe60:  ffffff00 55aa55aa 00000014 00000020  
3ffffe70:  00000020 00000060 00000062 aa55aa55  
3ffffe80:  000003ff 4022e492 3ffef124 3ffef124  
3ffffe90:  000000ff 3ffed504 00000001 00000001  
3ffffea0:  40100c2d 00000001 3ffef134 4022e6b2  
3ffffeb0:  00000005 3ffef124 000000ff 3fffff20  
3ffffec0:  3fffff40 3ffef15b 00000014 00000020  
3ffffed0:  3ffef1e4 3fffff81 00000001 4022e762  
3ffffee0:  3fffff20 40239d10 3ffee328 3ffee3d4  
3ffffef0:  3ffef524 3fffff40 3fff545c 4022e731  
3fffff00:  3ffef124 4022e798 00000001 00000000  
3fffff10:  402017e0 00000001 feefeffe 402017bc  
3fffff20:  40203600 3ffef004 3ffeefec 402036b7  
3fffff30:  3ffe8626 00000000 7c00202e 4020330d  
3fffff40:  3ffe8600 40203879 ffffffff 00000001  
3fffff50:  feefeffe 00000001 3ffee35c 3ffee3d4  
3fffff60:  40201afc 3ffee35c 3ffe860c 40201b08  
3fffff70:  40201afc 3ffee35c 3ffe860c 40201dd1  
3fffff80:  3fffdad0 3ffee35c 3ffee394 40201e68  
3fffff90:  3ffee328 3ffee35c 3ffee394 40201069  
3fffffa0:  3fffdad0 00000000 3ffee394 40202560  
3fffffb0:  feefeffe feefeffe 3ffe84e4 40100b85  
<<<stack<<<
Exception 3: LoadStoreError: Processor internal physical address or data error during load or store
PC: 0x40100718: umm_malloc_core(size_t) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\umm_malloc\umm_malloc.cpp line 433
EXCVADDR: 0x4006e989

Decoding stack results
0x40100900: malloc(size_t) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\umm_malloc\umm_malloc.cpp line 552
0x40100b50: calloc(size_t, size_t) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\umm_malloc\umm_malloc.cpp line 902
0x4010027c: pvPortZalloc(size_t, char const*, int) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\heap.cpp line 301
0x40100c2d: __wrap_spi_flash_read(uint32_t, uint32_t*, size_t) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\core_esp8266_phy.cpp line 309
0x40100c2d: __wrap_spi_flash_read(uint32_t, uint32_t*, size_t) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\core_esp8266_phy.cpp line 309
0x402017e0: ESP8266WiFiSTAClass::disconnect(bool) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WiFi\src\ESP8266WiFiSTA.cpp line 385
0x402017bc: ESP8266WiFiSTAClass::disconnect(bool) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WiFi\src\ESP8266WiFiSTA.cpp line 378
0x40203600: uart_init(int, int, int, int, int, size_t, bool) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\uart.cpp line 666
0x402036b7: uart_init(int, int, int, int, int, size_t, bool) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\uart.cpp line 668
0x4020330d: uart_write(uart_t*, char const*, size_t) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\uart.cpp line 509
0x40203879: uart_set_debug(int) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\uart.cpp line 933
0x40201afc: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/HardwareSerial.h line 164
0x40201b08: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/HardwareSerial.h line 165
0x40201afc: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/HardwareSerial.h line 164
0x40201dd1: Print::write(char const*) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/Print.h line 62
0x40201e68: Print::print(char const*) at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\Print.cpp line 130
0x40201069: setup() at C:\Users\NEWPC\Desktop\New folder\main/main.ino line 29
0x40202560: loop_wrapper() at C:\Users\NEWPC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\core_esp8266_main.cpp line 194

try the other way around

WiFi.mode(WIFI_STA);
WiFi.disconnect();

No difference.

I'm beginning to get to the point that I think the module itself is damaged what my concern is, is how it was damaged

Do they get enough power ? Disconnect everything that might be connected to the pins and test one of the examples like WiFiScan

Solution found via another group

The code above was just sample code. I code using PIO, and had run a clear that I through would sort out most problem, but it looks like that’s wasn't doing the trick.
Having now done a Erase Flash + Wifi via the IDE not only does the above code run but my main code as well

My main code either sets up and Access Point or Connects to the WiFi depending on a setting file (read from a SPIFFS).
If the Wifi fails to connect then is resets the settings file to make sure it loads in Access Point and does a ESP.restart(), now knowing what the problem is and what to look for I have added ESP.eraseConfig(); before the restart

All Sorted