Dual Mega +WiFi Exception when using WiFi.begin(ssid, password);

Hello,

I have read many posts and could not really find a solution to my problem. and quite a few posts are in different languages...
I have a few Dual Mega + Wifi "WeMOS Mega + WiFi R3 AT mega2560 + ESP8266 USB-TTL Arduino Mega NodeMCU...." So I have tried this on more than one hardware setup and the results are the same.

I can program both the Mega side and the ESP8266. But at a certain stage. The ESP8266 always comes up with an Exception error. The debug window get flooded with exception code.

I have narrowed it down to the "WiFi.begin(ssid, password);" as soon as I include this line the code screws up and I get exception codes.

Just simplifying my code to the bare minimum causes the problem:

#include <ESP8266WebServer.h>

const char* ssid = "";
const char* password = "
****";

ESP8266WebServer server(80);

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

// start WiFi
WiFi.begin(ssid, password);
Serial.println("");
}

void loop(void){
delay(1000);
}

I have tried the ESP8266WebServer.h, WiFiEsp.h, ESP8266WiFi.h and various other libraries.
But when I run the code in the ESP8266 part of the dual card, the debug window is flooded with exceptions.

When I use the WiFiEsp.h library, I get the following, then exception 28
17:08:58.490 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
17:08:58.490 ->
17:08:58.490 -> ets Jan 8 2013,rst cause:2, boot mode:(3,6)
17:08:58.490 ->
17:08:58.537 -> load 0x4010f000, len 3584, room 16
17:08:58.537 -> tail 0
17:08:58.537 -> chksum 0xb0
17:08:58.537 -> csum 0xb0
17:08:58.537 -> v2843a5ac
17:08:58.537 -> ~ld
17:08:58.583 ->
17:08:58.583 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------

17:08:58.583 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
17:08:58.583 ->
17:08:58.583 -> Exception (28):
17:08:58.583 -> epc1=0x40201d2c epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

and if I use either the ESP8266WebServer.h or ESP8266WiFi.h libraries, I get the same start and then exception 3

17:24:57.770 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
17:24:57.770 ->
17:24:57.770 -> ets Jan 8 2013,rst cause:2, boot mode:(3,6)
17:24:57.770 ->
17:24:57.817 -> load 0x4010f000, len 3584, room 16
17:24:57.817 -> tail 0
17:24:57.817 -> chksum 0xb0
17:24:57.817 -> csum 0xb0
17:24:57.817 -> v2843a5ac
17:24:57.817 -> ~ld
17:24:57.864 ->
17:24:57.864 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------

17:24:57.864 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
17:24:57.864 ->
17:24:57.864 -> Exception (3):
17:24:57.864 -> epc1=0x40100718 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4006e991 depc=0x00000000

The debug decoder tells me resp. as follows, but it makes no sense to me.

Can anybody tell me what I am doing wrong?

Exception 28: LoadProhibited: A load referenced a page mapped with an attribute that does not permit loads
PC: 0x40201d2c: EspDrv::espEmptyBuf(bool) at C:\Users\hvegt\Documents\Arduino\libraries\WiFiEsp\src\utility\EspDrv.cpp line 1094
EXCVADDR: 0x00000000

Decoding stack results
0x402046c1: uart_set_debug(int) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\uart.cpp line 933
0x402020c7: EspDrv::sendCmd(__FlashStringHelper const*, int) at C:\Users\hvegt\Documents\Arduino\libraries\WiFiEsp\src\utility\EspDrv.cpp line 998
0x4020216a: EspDrv::config(IPAddress) at C:\Users\hvegt\Documents\Arduino\libraries\WiFiEsp\src\utility\EspDrv.cpp line 230
0x40202620: HardwareSerial::begin(unsigned long, SerialConfig, SerialMode, unsigned char, bool) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\HardwareSerial.cpp line 51
0x40201401: WiFiEspClass::config(IPAddress) at C:\Users\hvegt\Documents\Arduino\libraries\WiFiEsp\src\WiFiEsp.cpp line 85
0x40201344: setup() at C:\Users\hvegt\Documents\Arduino\NewESP826602/NewESP826602.ino line 25
0x402033f0: loop_wrapper() at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\core_esp8266_main.cpp line 194

and

Exception 3: LoadStoreError: Processor internal physical address or data error during load or store
PC: 0x40100718: umm_malloc_core(size_t) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\umm_malloc\umm_malloc.cpp line 433
EXCVADDR: 0x4006e991

Decoding stack results
0x40100900: malloc(size_t) at C:\Users\hvegt\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\hvegt\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\hvegt\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\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\core_esp8266_phy.cpp line 309
0x40100647: umm_free_core(void*) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\umm_malloc\umm_malloc.cpp line 351
0x40100c2d: __wrap_spi_flash_read(uint32_t, uint32_t*, size_t) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\core_esp8266_phy.cpp line 309
0x40201962: ESP8266WiFiSTAClass::begin(char const*, char const*, int, unsigned char const*, bool) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WiFi\src\ESP8266WiFiSTA.cpp line 189
0x402018b7: ESP8266WiFiSTAClass::begin(char const*, char const*, int, unsigned char const*, bool) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WiFi\src\ESP8266WiFiSTA.cpp line 157
0x40100900: malloc(size_t) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\umm_malloc\umm_malloc.cpp line 552
0x4020491b: base_from_object at /workdir/repo/gcc/libgcc/unwind-dw2-fde.c line 254
0x40204add: read_encoded_value_with_base at /workdir/repo/gcc/libgcc/unwind-pe.h line 257
0x40100900: malloc(size_t) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\umm_malloc\umm_malloc.cpp line 552
0x40203631: uart_flush(uart_t*) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\uart.cpp line 544
0x40203943: uart_init(int, int, int, int, int, size_t, bool) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\uart.cpp line 668
0x40203b05: uart_set_debug(int) at C:\Users\hvegt\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266\uart.cpp line 933
0x4020105c: setup() at

you run the WiFiEsp library on en esp8266? that is for other Arduino to use the AT firmware on esp8266.

try first the WiFiScan example of the ESP8266WiFi library

Thank you Juraj, I was going to get back to this, but I was concentrating on the other post. I understand now what you are suggesting. I like the option using the WiFiEspAT and doing all the code in the Mega and just sending AT commands to the ESP8266, because that way I don't have to change all the DIP switches every time I make a change or need to fault-find. Possible drawback would be speed I suppose. But so far I have enough capacity to do what I want to do.
(I have had a datalogger for temperature and power consumption reading sensors, webclients and storing to SD or USB using a raspberryPI for a number of years, but that does not handle power interruptions too good. Hence I have re-written for the Arduino Mega).

I think we can close this post.