Restarting the controller when using sockets in the web server ESP32

I program in the platformio.
I use web sockets to exchange data between a web page and a controller. It has always been like this, I have many projects constantly on this principle. But now for some reason as soon as I try to open a web page, my controller reboots. For a long time I did not understand what could be wrong, as a result I found out that the problem is in the web socket.
If you remove the web socket initialization ( ws.onEvent(onEvent);
server.addHandler(&ws);). The server starts and no reboots are observed.
how can this be, i have used this method of working with web server for esp32 so many times, what happened now?

My platformini File.

[env:esp32dev]

platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
upload_port = COM12
monitor_port = COM12
board_build.partitions = partitions.csv

lib_deps =

ESP Async WebServer
arduino-libraries/Arduino_JSON @ 0.1.0
esphome/ESP32-audioI2S@^2.0.7

My partitions

#Name,       Type,  SubType,           Offset,           Size,           Flags
nvs,         data,  nvs,               0x9000,          0x4000,
phy_init,    data,  phy,               0xf000,          0x1000,
factory,      app,  factory,          0x10000,              3M,
spiffs,      data,  spiffs,                  ,         0x50000,

Work with WebServ Lib/


AsyncWebServer server(80);
AsyncWebSocket ws("/ws");

//Event handle
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,AwsEventType type,
                                                void *arg, uint8_t *data, size_t len) 
{
  switch (type)
   {
    case WS_EVT_CONNECT:  
    ESP_LOGE(TAG,"WebSocket client #%u connected from %s", client->id(), client->remoteIP().toString().c_str());
    if(sWebServControl.FormSelect == SELECT_FORM_WIFI)  FnChangeForm(SELECT_FORM_WIFI);   //  SELECT_FORM_WIFI SELECT_FORM_SOUND
      else  FnChangeForm(SELECT_FORM_SOUND); 
 
      break;

    case WS_EVT_DISCONNECT:
    ESP_LOGE(TAG,"WebSocket client #%u disconnected", client->id() );
      break;

    case WS_EVT_DATA:
   // ESP_LOGE(TAG,"WS_EVT_DATA !!!");
      handleWebSocketMessage(arg, data, len);
      break;

    case WS_EVT_PONG:
    case WS_EVT_ERROR:
      break;
  }
}
//init WebSocket
void initWebSocket() 
{
  test = 0;
    ws.onEvent(onEvent);
    server.addHandler(&ws);
}

//Init WebServ
void FnStartWebServer(void)
{
initWebSocket();
   server.on("/index_main", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index_main.html", "text/html");
  });

   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index_main.html", "text/html", false);
  });

  server.serveStatic("/", SPIFFS, "/");
  server.begin();
}

JS File for init WebSocket

var gateway = `ws://${window.location.hostname}/ws`;

var websocket;

function initWebSocket()

{

console.log('Trying to open a WebSocket connection...');

websocket = new WebSocket(gateway);

websocket.onopen = onOpen;

websocket.onclose = onClose;

websocket.onmessage = onMessage;

}

function onLoad(event)
{
    console.log("Start Init Web_SOCKET");
    initWebSocket();
}
function onOpen(event)

{

console.log('Connection opened');

}

function onClose(event)

{

console.log('Connection closed');

setTimeout(initWebSocket, 2000);

}

window.addEventListener('load', onLoad);

I repeat, I have used this model of working with a web server many times and it works now in other projects, but it does not work in this project. And even if I copy the working project and try to use it, my controller reboots.
Whenever I try to open a web page in a browser, I get the following error.
ets Jun 8 2016 00:22:57


rst:0x8 (TG1WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4

This is too unspecific. Which ESP AsyncWebServer is used in your project?
The latest and most similar AsyncWebServer to the original is esp32async/ESPAsyncWebServer@^3.7.0

You may find the root cause for the crash by enabling the ESP32 Exception Decoder:

monitor_filters = esp32_exception_decoder

Please use pre-formatted text when posting logs, code etc.

Hi!
I changed my platformio.ini file:


[env:esp32dev]

platform = espressif32

board = esp32dev

framework = arduino
monitor_speed = 115200
upload_port = COM3
monitor_port = COM3
board_build.f_cpu = 240000000L

board_build.partitions = partitions.csv
monitor_filters = esp32_exception_decoder

lib_deps =
arduino-libraries/Arduino_JSON @ 0.1.0
esphome/ESP32-audioI2S@^2.0.7
esp32async/ESPAsyncWebServer@^3.7.0

Well, the problem remains, as soon as I try to go to the web page, the controller resets.Here is my log when resetting.

Guru Meditation Error: Core 1 panic’ed (LoadProhibited). Exception was unhandled.


Core  1 register dump:
PC      : 0x400dec84  PS      : 0x00060530  A0      : 0x800dc08c  A1      : 0x3ffeb910
A2      : 0x3ffc6ae8  A3      : 0x3ffebe80  A4      : 0x3ffb2350  A5      : 0x3ffc6b3c  
A6      : 0x00000001  A7      : 0x3ffebc20  A8      : 0xffffffff  A9      : 0x3ffebc20
A10     : 0x3ffc6a78  A11     : 0x3ffebe80  A12     : 0x00000002  A13     : 0x3ffebc22  
A14     : 0x0000000d  A15     : 0x00000000  SAR     : 0x00000018  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000007  LBEG    : 0x40089f25  LEND    : 0x40089f47  LCOUNT  : 0xffffffff

Backtrace: 0x400dec81:0x3ffeb910 0x400dc089:0x3ffeb930 0x400dcd99:0x3ffeb970 0x400dcf8d:0x3ffeb9a0 0x401662dd:0x3ffeb9c0 0x40166355:0x3ffeb9f0 0x40166d22:0x3ffeba10

  #0  0x400dec81 in AsyncWebServer::_attachHandler(AsyncWebServerRequest*) at .pio/libdeps/esp32dev/ESPAsyncWebServer/src/WebServer.cpp:136

  #1  0x400dc089 in AsyncWebServerRequest::_parseLine() at .pio/libdeps/esp32dev/ESPAsyncWebServer/src/WebRequest.cpp:672

  #2  0x400dcd99 in AsyncWebServerRequest::_onData(void*, unsigned int) at .pio/libdeps/esp32dev/ESPAsyncWebServer/src/WebRequest.cpp:158

  #3  0x400dcf8d in std::_Function_handler<void (void*, AsyncClient*, void*, unsigned int), AsyncWebServerRequest::AsyncWebServerRequest(AsyncWebServer*, AsyncClient*)::{lambda(void*, AsyncClient*, void*, unsigned int)#5}>::_M_invoke(std::_Any_data const&, void*&&, AsyncClient*&&, std::_Any_data const&, unsigned int&&) at .pio/libdeps/esp32dev/ESPAsyncWebServer/src/WebRequest.cpp:70
      (inlined by) _M_invoke at c:\users\vitalii\.platformio\packages\toolchain-xtensa-esp32\xtensa-esp32-elf\include\c++\8.4.0\bits/std_function.h:297

  #4  0x401662dd in std::function<void (void*, AsyncClient*, void*, unsigned int)>::operator( (void*, AsyncClient*, void*, unsigned int) const at c:\users\vitalii\.platformio\packages\toolchain-xtensa-esp32\xtensa-esp32 elf\include\c++\8.4.0\bits/std_function.h:687

      (inlined by) AsyncClient::_recv(tcp_pcb*, pbuf*, signed char) at.pio/libdeps/esp32dev/AsyncTCP/src/AsyncTCP.cpp:1122

  #5  0x40166355 in AsyncClient::_s_recv(void*, tcp_pcb*, pbuf*, signed char) at .pio/libdeps/esp32dev/AsyncTCP/src/AsyncTCP.cpp:1487

  #6  0x40166d22 in _async_service_task(void*) at .pio/libdeps/esp32dev/AsyncTCP/src/AsyncTCP.cpp:259
      (inlined by) _async_service_task at .pio/libdeps/esp32dev/AsyncTCP/src/AsyncTCP.cpp:294

Again, please use pre-formatted text as your log is unreadable!

Thanks for editing your post :slight_smile:

I did, sorry I’m a little worried because I don’t understand what my mistake is

Guru Meditation Error: Core 1 panic’ed (LoadProhibited). Exception was unhandled.

It looks like your code is trying to write to an uninitialized memory region (nullptr) or acessing an array outside it’s capacity (out of bounds) error.

Can you provide a minimal reproducible example project?

I can dump the whole project for you, removing everything unnecessary, and leaving only the initialization of the WiFi and web server. Okay?

Yes, the smallest possible project just to reproduce the error. :slight_smile:

I found a problem, it is related to the initialization of the library working with EEPROM. I just removed the initialization. But it is still not clear what the problem could be.

#define  MEMO_SETUP_MEM_SIZE  600 
sMemoSetupTask.SizeOpen = EEPROM.begin((size_t)MEMO_SETUP_MEM_SIZE);
     OpenEEPROM= EEPROM.begin((size_t)MEMO_SETUP_MEM_SIZE);
    if(OpenEEPROM == false) ESP_LOGE(TAG,"ERROR OPEN EEPROM");

On the other hand, the problem still remains open. How can this code affect the work of the web server? It is obvious that the whole thing is in the controller memory, well, I have a partition table, and the EEPROM library works with a section NVS of memory. At the same time, the web server files (html,css,js) are stored in spiff.
how could this be related?

EEPROM is just emulated. There is no real EEPROM. Without knowing more about your code - hard to say. Maybe some part of your code tries to access data behind the 600 byte border…

You should consider using the built-in Preferences library which is a key-value pair storage. The data is stored in the NVS partition and wear leveling mechanism prevents the FLASH from wearing out fast.

1 Like