ESP32 crashes when starting web server

pscott
Posts: 5
Joined: Mon Mar 07, 2022 9:13 pm

ESP32 crashes when starting web server

Postby pscott » Mon Mar 07, 2022 9:55 pm

Hi,

I'm using PlatformIO IDE to program a WeMos D1 Mini ESP32.

My goal is for the ESP32 to be able to send/receive UDP packets via ethernet, but I also need to be able to update the firmware with a web updater OTA. I was able to achieve this while using Wi-Fi, but not while using ethernet.

Here is my code:

Code: Select all

/*
  Rui Santos
  Complete project details
   - Arduino IDE: https://RandomNerdTutorials.com/esp32-ota-over-the-air-arduino/
   - VS Code: https://RandomNerdTutorials.com/esp32-ota-over-the-air-vs-code/
  
  This sketch shows a Basic example from the AsyncElegantOTA library: ESP32_Async_Demo
  https://github.com/ayushsharma82/AsyncElegantOTA
*/

#define LISTEN_PORT 4004

#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <AsyncUDP.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

uint8_t eth_MAC[] = { 0x02, 0xF0, 0x0D, 0xBE, 0xEF, 0x01 };
IPAddress myIP(192, 168, 167, 101);
IPAddress myGW(192, 168, 167, 1);
IPAddress mySN(255, 255, 255, 0);
IPAddress myDNS(8, 8, 8, 8);

EthernetUDP Udp;
AsyncWebServer server(80);

void setup(void) {
  Serial.begin(115200);
  Ethernet.init(5);

  Serial.println("Starting ETHERNET connection...");
  
  Ethernet.begin(eth_MAC, myIP, myDNS, myGW, mySN);
  delay(200);

  Serial.print("Ethernet IP is: ");
  Serial.println(Ethernet.localIP());
  Serial.print("Local port is: ");
  Serial.println(LISTEN_PORT);

  Udp.begin(LISTEN_PORT);

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(200, "text/plain", "Hi! I am ESP32.");
  });

  AsyncElegantOTA.begin(&server);    // Start ElegantOTA
  Serial.println("I'm about to crash!");
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {

}
I'm able to get ethernet communication without the OTA/AsyncWebServer code added in, but when I add the OTA/AsyncWebServer code, the ESP32 gives an error and restarts during setup.

Here is what the serial monitor shows when I run the program:

Code: Select all

Starting ETHERNET connection...
Ethernet IP is: 192.168.167.101
Local port is: 4004
I'm about to crash!
assertion "Invalid mbox" failed: file "/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c", line 416, function: tcpip_api_call
abort() was called at PC 0x400e3a0b on core 1

ELF file SHA256: 0000000000000000

Backtrace: 0x400852a4:0x3ffb1e30 0x40085521:0x3ffb1e50 0x400e3a0b:0x3ffb1e70 0x4011f063:0x3ffb1ea0 0x4011485c:0x3ffb1ed0 0x400d6966:0x3ffb1f20 0x400d224e:0x3ffb1f40 0x400e1686:0x3ffb1fb0 0x40086532:0x3ffb1fd0

Rebooting...
I've been banging my head trying to figure this one out, so any help is appreciated!
Last edited by pscott on Mon Mar 28, 2022 3:33 pm, edited 1 time in total.

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: ESP32 crashes when starting web server

Postby ESP_YJM » Tue Mar 08, 2022 2:12 am

You need check whether you call the function tcpip_init.

pscott
Posts: 5
Joined: Mon Mar 07, 2022 9:13 pm

Re: ESP32 crashes when starting web server

Postby pscott » Tue Mar 08, 2022 2:33 pm

I don't think I am calling the tcpip_init function. Should i be?

Here is the code for the server.begin function where my program crashes:

Code: Select all

void AsyncServer::begin(){
    if(_pcb) {
        return;
    }

    if(!_start_async_task()){
        log_e("failed to start task");
        return;
    }
    int8_t err;
    _pcb = tcp_new_ip_type(IPADDR_TYPE_V4);
    if (!_pcb){
        log_e("_pcb == NULL");
        return;
    }

    ip_addr_t local_addr;
    local_addr.type = IPADDR_TYPE_V4;
    local_addr.u_addr.ip4.addr = (uint32_t) _addr;
    err = _tcp_bind(_pcb, &local_addr, _port);

    if (err != ERR_OK) {
        _tcp_close(_pcb, -1);
        log_e("bind error: %d", err);
        return;
    }

    static uint8_t backlog = 5;
    _pcb = _tcp_listen_with_backlog(_pcb, backlog);
    if (!_pcb) {
        log_e("listen_pcb == NULL");
        return;
    }
    tcp_arg(_pcb, (void*) this);
    tcp_accept(_pcb, &_s_accept);
}

pscott
Posts: 5
Joined: Mon Mar 07, 2022 9:13 pm

Re: ESP32 crashes when starting web server

Postby pscott » Tue Mar 08, 2022 4:49 pm

I added the following line in my setup function:

Code: Select all

tcpip_init(NULL, NULL);
Now the ESP32 does not crash, but I am unable to access the web server using the device's IP address. Any ideas?

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: ESP32 crashes when starting web server

Postby ESP_YJM » Wed Mar 09, 2022 3:28 am

You need call netif_add to add your ethernet netif to lwip netif and make this netif up.

pscott
Posts: 5
Joined: Mon Mar 07, 2022 9:13 pm

Re: ESP32 crashes when starting web server

Postby pscott » Wed Mar 09, 2022 3:07 pm

Thank you for answering my questions, but can you provide more detail in your answers? I'm unfamiliar with all of this netif and lwip stuff.

All I need to do is call the netif_add function? I'm not sure what values I would enter for the State, Init, and Input parameters of the netif_add function.

ESP_YJM
Posts: 300
Joined: Fri Feb 26, 2021 10:30 am

Re: ESP32 crashes when starting web server

Postby ESP_YJM » Thu Mar 10, 2022 2:05 am

I think you can follow the examples/common_components/protocol_examples_common/connect.c, function eth_start,use our esp-netif components to startup your eth netif. Maybe it's difficult to raise an ethernet netif on your own.

nicedaynot
Posts: 1
Joined: Sun Nov 03, 2024 10:49 pm

Re: ESP32 crashes when starting web server

Postby nicedaynot » Sun Nov 03, 2024 10:50 pm

@pscott @ESP_YJM

Did you guys ever manage to work out how to solve this? I am trying to start the same thing over ethernet and it crashes.

Who is online

Users browsing this forum: f.damien12 and 53 guests