hoeken / PsychicHttp

Simple + Robust HTTP/S server with websockets for ESP32 based on ESP-IDF http server.
GNU Lesser General Public License v3.0
92 stars 23 forks source link

Assert failed: tcpip_send_msg_wait_sem using Ethernet #77

Open JC-Electronics-Design opened 5 months ago

JC-Electronics-Design commented 5 months ago

Hello,

I'm trying to build a webserver in combination with ethernet instead of WiFi. While testing I stumbled upon the following error when the function listen() is called.

Start test webserver

assert failed: tcpip_send_msg_wait_sem IDF/components/lwip/lwip/src/api/tcpip.c:455 (Invalid mbox)

Backtrace: 0x40083539:0x3ffb1f10 0x40088999:0x3ffb1f30 0x4008dd4d:0x3ffb1f50 0x400e616e:0x3ffb2080 0x400f0fdd:0x3ffb20b0 0x400f103c:0x3ffb20d0 0x400e5fe5:0x3ffb2120 0x400f7408:0x3ffb2140 0x400d3899:0x3ffb21c0 0x400d38cf:0x3ffb21e0 0x400d3979:0x3ffb2220 0x400d2b3e:0x3ffb2240 0x400daa5a:0x3ffb2290

  #0  0x40083539:0x3ffb1f10 in panic_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/panic.c:408
  #1  0x40088999:0x3ffb1f30 in esp_system_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/esp_system.c:137
  #2  0x4008dd4d:0x3ffb1f50 in __assert_func at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/assert.c:85
  #3  0x400e616e:0x3ffb2080 in tcpip_send_msg_wait_sem at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/lwip/src/api/tcpip.c:455 (discriminator 1)
  #4  0x400f0fdd:0x3ffb20b0 in netconn_apimsg at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/lwip/src/api/api_lib.c:136
  #5  0x400f103c:0x3ffb20d0 in netconn_new_with_proto_and_callback at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/lwip/src/api/api_lib.c:166
  #6  0x400e5fe5:0x3ffb2120 in lwip_socket at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/lwip/src/api/sockets.c:1774
  #7  0x400f7408:0x3ffb2140 in socket at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/lwip/lwip/src/include/lwip/sockets.h:656
      (inlined by) httpd_server_init at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_http_server/src/httpd_main.c:268
      (inlined by) httpd_start at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_http_server/src/httpd_main.c:423
  #8  0x400d3899:0x3ffb21c0 in PsychicHttpServer::_startServer() at .pio/libdeps/esp32dev/PsychicHttp/src/PsychicHttpServer.cpp:97
  #9  0x400d38cf:0x3ffb21e0 in PsychicHttpServer::_start() at .pio/libdeps/esp32dev/PsychicHttp/src/PsychicHttpServer.cpp:81
  #10 0x400d3979:0x3ffb2220 in PsychicHttpServer::listen(unsigned short) at .pio/libdeps/esp32dev/PsychicHttp/src/PsychicHttpServer.cpp:68
  #11 0x400d2b3e:0x3ffb2240 in setup() at src/main.cpp:154
  #12 0x400daa5a:0x3ffb2290 in loopTask(void*) at /Users/jonathancaes/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:42

ELF file SHA256: 4ced09abcc31700f

Rebooting...

Here is the code I'm running:

#include <Arduino.h>
// #include <WiFi.h>
#include <LittleFS.h>
#include <ESPmDNS.h>
#include <esp_sntp.h>
#include <Ethernet.h>
#include <PsychicHttp.h>

FS* filesystem =      &LittleFS;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

PsychicHttpServer server;
PsychicEventSource events;

void setup() {
    delay(500);
    Serial.begin(1000000);
    delay(500);
    Serial.println("Start test webserver");

    // WiFi.mode(WIFI_STA);
    // WiFi.begin(ssid, password);
    // if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    //     Serial.printf("WiFi Failed!\n");
    //     return;
    // }

    // Serial.print("IP Address: ");
    // Serial.println(WiFi.localIP());

    Ethernet.init(5);
    delay(100);

    if(Ethernet.linkStatus() != LinkON) {
        Serial.println("Check ethernet connection!");
        // #ifdef USE_DISPLAY
        // link_status_warning(true);
        // #endif
        while(Ethernet.linkStatus() != LinkON) {
            delay(100);
        }
    }

    if(!Ethernet.begin(mac)) {
        Serial.println("Retrieving ip address via DHCP failed!\nReset MCU!");
        // Wait 100ms before resetting W5500 and MCU
        delay(100);
        ESP.restart();
    }

    if(!LittleFS.begin()){
        Serial.println("An Error has occurred while mounting SPIFFS");
        return;
    }

    //setup server config stuff here
    server.config.max_uri_handlers = 20; //maximum number of uri handlers (.on() calls)

    server.listen(80);

    server.serveStatic("/index.html", LittleFS, "/index.html");

    server.serveStatic("/front", LittleFS, "/front.jpg");

    server.serveStatic("/global_style.css", LittleFS, "/global_style.css");
    server.serveStatic("/index_style.css", LittleFS, "/index_style.css");
    server.serveStatic("/script.js", LittleFS, "/script.js");
}

void loop() {
}

platformio.ini file:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 1000000
upload_speed = 1000000
board_build.filesystem = littlefs
monitor_filters = esp32_exception_decoder
lib_deps = 
    https://github.com/hoeken/PsychicHttp
    arduino-libraries/Ethernet@^2.0.0

Everything works when using WiFi but once I try using ethernet (W5500 chip) I start getting this issue. I also tried the Ethernet2.h library from Adafruit but this has the same results.

Hope someone can help me with this issue. Maybe I missed some setting? Maybe I have to use another Ethernet library? Is it possible to implement ethernet compatibility?

Pablo2048 commented 5 months ago

Hi, IIRC it seems like you have not initialized ip stack (lwip). Try to just start wifi ap to achieve this (I've seen method to initialize lwip somewhere, but I can not find it right now :-( ). Maybe at least try to use WiFi.onEvent(onEvent); as seen in Ethernet library example.

JC-Electronics-Design commented 5 months ago

@Pablo2048 Hi Pablo, I've tried WiFi.onEvent(onEvent);, WiFi.softAP("ssid", "pass"); and esp_netif_init();. With all of these I don't get the assert failed error but am still not able to access the webpage via ethernet. Any other suggestions?

johnnytolengo commented 5 months ago

tcpip_adapter_init(); //Call this before start any Ethernet device

Pablo2048 commented 5 months ago

Any other suggestions?

Here you are:

Probably best practice is to make working one of Ethernet library examples and then add PsychicHttp...

JC-Electronics-Design commented 5 months ago

@johnnytolengo I don't get any errors when including tcpip_adapter_init();, but am still not able to connect to the server. @Pablo2048 The device does have an ip address before I call, in this case, listen(80). I don't get the assert error and am able to ping the device with the ip address the device is assigned but when I request a webpage for example index.html the device does not receive the request. It seems like PsychicHttp does not look for/consider requests from coming from ethernet client.

@hoeken Can you give me some pointers on what to do? Did you ever test the library with ethernet? Or do you now it is or is not possible to use the library with ethernet?

johnnytolengo commented 5 months ago

first of all you must be sure that your ethernet is working properly, did you test it?

hoeken commented 5 months ago

Can you test with the esp-idf official webserver example: https://github.com/espressif/esp-idf/blob/master/examples/protocols/http_server/simple/README.md

If that works, then try to figure out what they are doing that we aren't as PsychicHttp is built on top of this library.

It looks like they do quite a bit of config setup which is located in this shared file: https://github.com/espressif/esp-idf/blob/master/examples/common_components/protocol_examples_common/eth_connect.c

I've never worked with esp32 + eth so this is just my 5 minute take on it.


Zach Smith

On Fri, Feb 16, 2024 at 10:46 AM JC Design @.***> wrote:

@johnnytolengo https://github.com/johnnytolengo I don't get any errors when including tcpip_adapter_init();, but am still not able to connect to the server. @Pablo2048 https://github.com/Pablo2048 The device does have an ip address before I call, in this case, listen(80). I don't get the assert error and am able to ping the device with the ip address the device is assigned but when I request a webpage for example index.html the device does not receive the request. It seems like PsychicHttp does not look for/consider requests from coming from ethernet client.

— Reply to this email directly, view it on GitHub https://github.com/hoeken/PsychicHttp/issues/77#issuecomment-1948674883, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABEHSWHM7XQFZBQQFIPRR3YT55MHAVCNFSM6AAAAABDG5M2VWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNBYGY3TIOBYGM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

JC-Electronics-Design commented 5 months ago

Yes, I pinged the ip address and that worked no problem. With other similar code that communicates with an MQTT server (with the same setup) I have no problems and everything is stable. Would you like me to post the code when I'm home? Maybe you see something?

hoeken commented 5 months ago

Take a look at the 2nd link I posted and try some of that code. It's probably some sort of init thing.

On Fri, Feb 16, 2024, 15:42 JC Design @.***> wrote:

Yes, I pinged the ip address and that worked no problem. With other similar code that communicates with an MQTT server (with the same setup) I have no problems and everything is stable. Would you like me to post the code when I'm home? Maybe you see something?

— Reply to this email directly, view it on GitHub https://github.com/hoeken/PsychicHttp/issues/77#issuecomment-1949306230, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABEHSWISVB5BMZP2RWUDJ3YT7ADVAVCNFSM6AAAAABDG5M2VWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNBZGMYDMMRTGA . You are receiving this because you were mentioned.Message ID: @.***>

Pablo2048 commented 5 months ago

Guys the problem is in the library-he uses Arduino Ethernet instead of esp32 Ethernet native one. That is why I suggested to start with example...

JC-Electronics-Design commented 5 months ago

@hoeken Sorry didn't see your reply at first. I'll try the example via the esp-idf and see if that works. I've never worked with the esp-idf though so this will take a bit longer to figure out.

JC-Electronics-Design commented 5 months ago

Here an update on the matter. I've installed the ESP-IDF with VS code and got everything up and running. Configured the http_server simple example with the SDK Configuration Editor. Build, installed and monitored and am able to connect to the sever requesting the /hello page.

At first I had some issue with the version the W5500 chip returned, but I found that the SPI clock speed was too high for my prototype setup. Once I reduced the clock speed I didn't have any problems and everything just worked.

Below you can find a debug log of the example application.

--- esp-idf-monitor 1.4.0 on /dev/cu.SLAB_USBtoUART 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x17 (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:7172
load:0x40078000,len:15532
load:0x40080400,len:4
0x40080400: _init at ??:?

load:0x40080404,len:3904
entry 0x40080640
I (29) boot: ESP-IDF v5.2-dirty 2nd stage bootloader
I (29) boot: compile time Feb 18 2024 13:07:36
I (29) boot: Multicore bootloader
I (34) boot: chip revision: v3.1
I (38) boot.esp32: SPI Speed      : 40MHz
I (42) boot.esp32: SPI Mode       : DIO
I (47) boot.esp32: SPI Flash Size : 4MB
I (51) boot: Enabling RNG early entropy source...
I (57) boot: Partition Table:
I (60) boot: ## Label            Usage          Type ST Offset   Length
I (68) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (75) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (83) boot:  2 factory          factory app      00 00 00010000 00100000
I (90) boot: End of partition table
I (94) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=1a3b8h (107448) map
I (140) esp_image: segment 1: paddr=0002a3e0 vaddr=3ffb0000 size=02460h (  9312) load
I (143) esp_image: segment 2: paddr=0002c848 vaddr=40080000 size=037d0h ( 14288) load
I (151) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=41374h (267124) map
I (245) esp_image: segment 4: paddr=0007139c vaddr=400837d0 size=0c1e4h ( 49636) load
I (273) boot: Loaded app from partition at offset 0x10000
I (273) boot: Disabling RNG early entropy source...
I (284) cpu_start: Multicore app
D (285) cpu_start: Pro cpu up
D (285) cpu_start: Starting app cpu, entry point is 0x40081230
0x40081230: call_start_cpu1 at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_system/port/cpu_start.c:183

D (0) cpu_start: App cpu up
D (294) clk: RTC_SLOW_CLK calibration value: 3189811
I (306) cpu_start: Pro cpu start user code
I (306) cpu_start: cpu freq: 160000000 Hz
I (306) cpu_start: Application information:
I (311) cpu_start: Project name:     simple
I (315) cpu_start: App version:      1
I (320) cpu_start: Compile time:     Feb 18 2024 13:07:34
I (326) cpu_start: ELF file SHA256:  2d2a8d560...
I (331) cpu_start: ESP-IDF:          v5.2-dirty
I (337) cpu_start: Min chip rev:     v0.0
I (341) cpu_start: Max chip rev:     v3.99 
I (346) cpu_start: Chip rev:         v3.1
D (351) memory_layout: Checking 9 reserved memory ranges:
D (356) memory_layout: Reserved memory range 0x3ff82000 - 0x3ff82000
D (363) memory_layout: Reserved memory range 0x3ffae000 - 0x3ffae6e0
D (369) memory_layout: Reserved memory range 0x3ffb0000 - 0x3ffb3780
D (376) memory_layout: Reserved memory range 0x3ffe0000 - 0x3ffe0440
D (382) memory_layout: Reserved memory range 0x3ffe3f20 - 0x3ffe4350
D (388) memory_layout: Reserved memory range 0x40070000 - 0x40078000
D (395) memory_layout: Reserved memory range 0x40078000 - 0x40080000
0x40080000: _WindowOverflow4 at /Users/jonathancaes/esp/v5.2/esp-idf/components/xtensa/xtensa_vectors.S:2027

D (401) memory_layout: Reserved memory range 0x40080000 - 0x4008f9b4
0x40080000: _WindowOverflow4 at /Users/jonathancaes/esp/v5.2/esp-idf/components/xtensa/xtensa_vectors.S:2027

D (408) memory_layout: Reserved memory range 0x50001fe8 - 0x50002000
D (414) memory_layout: Building list of available memory regions:
D (420) memory_layout: Available memory region 0x3ffae6e0 - 0x3ffb0000
D (427) memory_layout: Available memory region 0x3ffb3780 - 0x3ffb8000
D (433) memory_layout: Available memory region 0x3ffb8000 - 0x3ffc0000
D (440) memory_layout: Available memory region 0x3ffc0000 - 0x3ffc2000
D (447) memory_layout: Available memory region 0x3ffc2000 - 0x3ffc4000
D (453) memory_layout: Available memory region 0x3ffc4000 - 0x3ffc6000
D (460) memory_layout: Available memory region 0x3ffc6000 - 0x3ffc8000
D (466) memory_layout: Available memory region 0x3ffc8000 - 0x3ffca000
D (473) memory_layout: Available memory region 0x3ffca000 - 0x3ffcc000
D (480) memory_layout: Available memory region 0x3ffcc000 - 0x3ffce000
D (486) memory_layout: Available memory region 0x3ffce000 - 0x3ffd0000
D (493) memory_layout: Available memory region 0x3ffd0000 - 0x3ffd2000
D (499) memory_layout: Available memory region 0x3ffd2000 - 0x3ffd4000
D (506) memory_layout: Available memory region 0x3ffd4000 - 0x3ffd6000
D (513) memory_layout: Available memory region 0x3ffd6000 - 0x3ffd8000
D (519) memory_layout: Available memory region 0x3ffd8000 - 0x3ffda000
D (526) memory_layout: Available memory region 0x3ffda000 - 0x3ffdc000
D (532) memory_layout: Available memory region 0x3ffdc000 - 0x3ffde000
D (539) memory_layout: Available memory region 0x3ffde000 - 0x3ffe0000
D (546) memory_layout: Available memory region 0x3ffe0440 - 0x3ffe3f20
D (552) memory_layout: Available memory region 0x3ffe4350 - 0x3ffe8000
D (559) memory_layout: Available memory region 0x3ffe8000 - 0x3fff0000
D (565) memory_layout: Available memory region 0x3fff0000 - 0x3fff8000
D (572) memory_layout: Available memory region 0x3fff8000 - 0x3fffc000
D (579) memory_layout: Available memory region 0x3fffc000 - 0x40000000
0x40000000: _WindowOverflow4 in ROM

D (585) memory_layout: Available memory region 0x4008f9b4 - 0x40090000
D (592) memory_layout: Available memory region 0x40090000 - 0x40092000
D (598) memory_layout: Available memory region 0x40092000 - 0x40094000
D (605) memory_layout: Available memory region 0x40094000 - 0x40096000
D (612) memory_layout: Available memory region 0x40096000 - 0x40098000
D (618) memory_layout: Available memory region 0x40098000 - 0x4009a000
D (625) memory_layout: Available memory region 0x4009a000 - 0x4009c000
D (631) memory_layout: Available memory region 0x4009c000 - 0x4009e000
D (638) memory_layout: Available memory region 0x4009e000 - 0x400a0000
I (645) heap_init: Initializing. RAM available for dynamic allocation:
D (652) heap_init: New heap initialised at 0x3ffae6e0
I (657) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
D (663) heap_init: New heap initialised at 0x3ffb3780
I (668) heap_init: At 3FFB3780 len 0002C880 (178 KiB): DRAM
I (674) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (681) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
D (687) heap_init: New heap initialised at 0x4008f9b4
I (692) heap_init: At 4008F9B4 len 0001064C (65 KiB): IRAM
D (699) intr_alloc: Connected src 46 to int 2 (cpu 0)
D (704) spi_flash: trying chip: issi
D (707) spi_flash: trying chip: gd
D (711) spi_flash: trying chip: mxic
D (714) spi_flash: trying chip: winbond
D (718) spi_flash: trying chip: generic
I (722) spi_flash: detected chip: generic
I (727) spi_flash: flash io: dio
D (731) cpu_start: calling init function: 0x40100044
0x40100044: _GLOBAL__sub_I__ZN9__gnu_cxx9__freeresEv at /Users/brnomac003/.gitlab-runner/builds/qR2TxTby/0/idf/crosstool-NG/.build/xtensa-esp-elf/src/gcc/libstdc++-v3/libsupc++/eh_alloc.cc:462

D (736) cpu_start: calling init function: 0x400ffaf4
0x400ffaf4: _GLOBAL__sub_I__ZN17__eh_globals_init7_S_initE at /Users/brnomac003/.gitlab-runner/builds/qR2TxTby/0/idf/crosstool-NG/.build/xtensa-esp-elf/src/gcc/libstdc++-v3/libsupc++/eh_globals.cc:162

D (741) cpu_start: calling init function: 0x400dc428
0x400dc428: esp_ipc_init at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_system/esp_ipc.c:114

D (746) cpu_start: calling init function: 0x40087014
0x40087014: enable_timer_group0_for_calibration at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_hw_support/port/esp32/rtc_time.c:197

D (751) cpu_start: calling init function: 0x400d2a54
0x400d2a54: esp_app_format_init_elf_sha256 at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_app_format/esp_app_desc.c:68

D (756) cpu_start: calling init function: 0x400d5ad4 on core: 0
0x400d5ad4: __esp_system_init_fn_esp_timer_startup_init at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_timer/src/esp_timer.c:578

D (762) intr_alloc: Connected src 17 to int 3 (cpu 0)
D (767) cpu_start: calling init function: 0x4010dd00 on core: 0
0x4010dd00: __esp_system_init_fn_init_components0 at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_system/startup.c:480

D (773) intr_alloc: Connected src 24 to int 9 (cpu 0)
D (778) app_start: Starting scheduler on CPU0
 (783) intr_alloc: Connected src 25 to int 2 (cpu 1)
D (783) app_start: Starting scheduler on CPU1
mI (783) main_task: Started on CPU0
D (803) heap_init: New heap initialised at 0x3ffe0440
D (803) heap_init: New heap initialised at 0x3ffe4350
D (803) intr_alloc: Connected src 16 to int 12 (cpu 0)
I (803) main_task: Calling app_main()
D (843) esp_netif_lwip: LwIP stack has been initialized
D (843) esp_netif_lwip: esp-netif has been successfully initialized
D (843) event: running task for loop 0x3ffb8074
D (843) event: created task for loop 0x3ffb8074
D (853) event: created event loop 0x3ffb8074
D (853) esp_netif_lwip: check: remote, if=0x3ffb58d0 fn=0x400df39c
0x400df39c: esp_netif_new_api at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:690

D (863) esp_netif_objects: esp_netif_add_to_list_unsafe netif added successfully (total netifs: 1)
D (873) esp_netif_lwip: call api in lwip: ret=0x0, give sem
D (873) intr_alloc: Connected src 22 to int 13 (cpu 0)
D (883) spi: SPI2 use gpio matrix.
D (883) w5500.mac: Using default SPI Driver
D (893) intr_alloc: Connected src 30 to int 17 (cpu 0)
D (893) spi_hal: eff: 13333, limit: 26666k(/2), 0 dummy, 0 delay
D (903) spi_master: SPI2: New device added to CS0, effective clock: 13333kHz
D (903) w5500.mac: Waiting W5500 to start & verify version...
D (923) esp_eth: new ethernet driver @0x3ffba89c
I (923) esp_eth.netif.netif_glue: 02:00:00:12:34:56
D (923) esp_netif_lwip: check: remote, if=0x3ffb8ff4 fn=0x400dee04
0x400dee04: esp_netif_set_mac_api at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:993

D (933) esp_netif_lwip: call api in lwip: ret=0x0, give sem
I (933) esp_eth.netif.netif_glue: ethernet attached to netif
D (953) event: running post ETH_EVENT:0 with handler 0x400e1330 and context 0x3ffba96c on loop 0x3ffb8074
0x400e1330: eth_action_start at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_eth/src/esp_eth_netif_glue.c:76

D (953) esp_eth.netif.netif_glue: eth_action_start: 0x3ffba914, 0x3f4091fc, 0, 0x3ffbaa80, 0x3ffba89c
D (963) esp_netif_handlers: esp_netif action has started with netif0x3ffb8ff4 from event_id=0
D (973) esp_netif_lwip: check: remote, if=0x3ffb8ff4 fn=0x400df5fc
0x400df5fc: esp_netif_start_api at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:1068

I (973) ethernet_connect: Waiting for IP(s).
D (973) esp_netif_lwip: esp_netif_start_api 0x3ffb8ff4
D (983) esp_netif_lwip: esp_netif_get_hostname esp_netif:0x3ffb8ff4
D (993) esp_netif_lwip: check: local, if=0x3ffb8ff4 fn=0x400dfd0c
0x400dfd0c: esp_netif_update_default_netif_lwip at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:316

D (993) esp_netif_lwip: esp_netif_update_default_netif_lwip 0x3ffb8ff4
D (1003) esp_netif_lwip: call api in lwip: ret=0x0, give sem
D (2973) w5500.mac: working in 100Mbps
D (2973) w5500.mac: working in full duplex
D (2973) w5500.mac: link is up
D (2973) event: running post ETH_EVENT:2 with handler 0x400e12a0 and context 0x3ffba9cc on loop 0x3ffb8074
0x400e12a0: eth_action_connected at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_eth/src/esp_eth_netif_glue.c:96

D (2983) esp_eth.netif.netif_glue: eth_action_connected: 0x3ffba914, 0x3f4091fc, 2, 0x3ffbaa80, 0x3ffba89c
D (2993) esp_netif_handlers: esp_netif action connected with netif0x3ffb8ff4 from event_id=2
D (3003) esp_netif_lwip: check: remote, if=0x3ffb8ff4 fn=0x400df980
0x400df980: esp_netif_up_api at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:1647

D (3013) esp_netif_lwip: esp_netif_up_api esp_netif:0x3ffb8ff4
D (3013) esp_netif_lwip: check: local, if=0x3ffb8ff4 fn=0x400dfd0c
0x400dfd0c: esp_netif_update_default_netif_lwip at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:316

D (3023) esp_netif_lwip: esp_netif_update_default_netif_lwip 0x3ffb8ff4
D (3023) esp_netif_lwip: call api in lwip: ret=0x0, give sem
D (3033) esp_netif_lwip: check: remote, if=0x3ffb8ff4 fn=0x400df860
0x400df860: esp_netif_dhcpc_start_api at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:1439

D (3043) esp_netif_lwip: esp_netif_dhcpc_start_api esp_netif:0x3ffb8ff4
D (3043) esp_netif_lwip: esp_netif_start_ip_lost_timer esp_netif:0x3ffb8ff4
D (3053) esp_netif_lwip: if0x3ffb8ff4 start ip lost tmr: interval=120
D (3063) esp_netif_lwip: starting dhcp client
D (3063) esp_netif_lwip: call api in lwip: ret=0x0, give sem
D (3093) w5500.mac: receive len=590
D (3243) w5500.mac: receive len=590
D (3273) w5500.mac: receive len=218
D (3273) w5500.mac: receive len=218
D (3453) w5500.mac: receive len=226
D (4063) esp_netif_lwip: esp_netif_internal_dhcpc_cb lwip-netif:0x3ffb9078
D (4063) esp_netif_lwip: if0x3ffb8ff4 ip changed=1
D (4063) event: running post IP_EVENT:4 with handler 0x400e1230 and context 0x3ffbaa40 on loop 0x3ffb8074
0x400e1230: eth_action_got_ip at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_eth/src/esp_eth_netif_glue.c:119

D (4073) esp_eth.netif.netif_glue: eth_action_got_ip: 0x3ffba914, 0x3f4084bc, 4, 0x3ffbab78, 0x3ffb8ff4
D (4083) esp_netif_handlers: esp_netif action got_ip with netif0x3ffb8ff4 from event_id=4
I (4083) esp_netif_handlers: example_netif_eth ip: 192.168.0.164, mask: 255.255.255.0, gw: 192.168.0.1
D (4093) event: running post IP_EVENT:4 with handler 0x400d845c and context 0x3ffbaa60 on loop 0x3ffb8074
0x400d845c: eth_on_got_ip at /Users/jonathancaes/esp/v5.2/esp-idf/examples/common_components/protocol_examples_common/eth_connect.c:36

I (4103) ethernet_connect: Got IPv4 event: Interface "example_netif_eth" address: 192.168.0.164
D (4113) esp_netif_lwip: check: remote, if=0x400d82a8 fn=0x4010e570
0x400d82a8: print_all_ips_tcpip at /Users/jonathancaes/esp/v5.2/esp-idf/examples/common_components/protocol_examples_common/connect.c:57
0x4010e570: tcpip_exec_api at /Users/jonathancaes/esp/v5.2/esp-idf/components/esp_netif/lwip/esp_netif_lwip.c:680

I (4123) example_common: Connected to example_netif_eth
D (4133) esp_netif_lwip: esp_netif_get_ip_info esp_netif:0x3ffb8ff4
I (4133) example_common: - IPv4 address: 192.168.0.164,
D (4143) esp_netif_lwip: call api in lwip: ret=0x0, give sem
I (4143) example: Starting server on port: '80'
D (4153) httpd: httpd_thread: web server started
D (4153) httpd: httpd_server: doing select maxfd+1 = 56
D (4163) event: no handlers have been registered for event ESP_HTTP_SERVER_EVENT:1 posted to loop 0x3ffb8074
I (4173) example: Registering URI handlers
D (4173) httpd_uri: httpd_register_uri_handler: [0] installed /hello
D (4183) httpd_uri: httpd_find_uri_handler: [0] = /hello
D (4183) httpd_uri: httpd_register_uri_handler: [0] exists /hello
D (4193) httpd_uri: httpd_register_uri_handler: [1] installed /echo
D (4203) httpd_uri: httpd_find_uri_handler: [0] = /hello
D (4203) w5500.mac: receive len=184
D (4213) w5500.mac: receive len=204
D (4213) httpd_uri: httpd_find_uri_handler: [1] = /echo
D (4223) httpd_uri: httpd_register_uri_handler: [0] exists /hello
D (4223) httpd_uri: httpd_register_uri_handler: [1] exists /echo
D (4233) httpd_uri: httpd_register_uri_handler: [2] installed /ctrl
D (4273) w5500.mac: receive len=218
D (4273) w5500.mac: receive len=218
D (4433) w5500.mac: receive len=77
D (4453) w5500.mac: receive len=60
D (5273) w5500.mac: receive len=218
D (5283) w5500.mac: receive len=218
D (5453) w5500.mac: receive len=226
D (6153) w5500.mac: receive len=333
D (6153) w5500.mac: receive len=353
D (6273) w5500.mac: receive len=218
D (6283) w5500.mac: receive len=218
D (6493) w5500.mac: receive len=147
D (6493) w5500.mac: receive len=167
D (6493) w5500.mac: receive len=60
D (6533) w5500.mac: receive len=228
D (6573) w5500.mac: receive len=378
D (6603) w5500.mac: receive len=228
D (6633) w5500.mac: receive len=398
D (6643) w5500.mac: receive len=188
D (6643) w5500.mac: receive len=208
D (6643) w5500.mac: receive len=188
D (6643) w5500.mac: receive len=208
D (6653) w5500.mac: receive len=216
D (6653) w5500.mac: receive len=236
D (6853) w5500.mac: receive len=102
D (6863) w5500.mac: receive len=122
D (6953) w5500.mac: receive len=216
D (6953) w5500.mac: receive len=236
D (7153) w5500.mac: receive len=333
D (7153) w5500.mac: receive len=353
D (7203) w5500.mac: receive len=216
D (7203) w5500.mac: receive len=236
D (7433) w5500.mac: receive len=77
D (7453) w5500.mac: receive len=226
D (7463) w5500.mac: receive len=419
D (7463) w5500.mac: receive len=439
D (7473) w5500.mac: receive len=226
D (7483) w5500.mac: receive len=246
D (7533) w5500.mac: receive len=214
D (7583) w5500.mac: receive len=214
D (8093) w5500.mac: receive len=78
D (8093) w5500.mac: receive len=78
D (8093) w5500.mac: receive len=60
D (8093) w5500.mac: receive len=60
D (8093) httpd: httpd_server: processing listen socket 54
D (8093) w5500.mac: receive len=60
D (8103) w5500.mac: receive len=519
D (8103) httpd: httpd_accept_conn: newfd = 57
D (8113) httpd_sess: httpd_sess_new: fd = 57
D (8113) httpd_sess: httpd_sess_new: active sockets: 1
D (8123) httpd: httpd_accept_conn: complete
D (8123) event: no handlers have been registered for event ESP_HTTP_SERVER_EVENT:2 posted to loop 0x3ffb8074
D (8123) httpd: httpd_server: doing select maxfd+1 = 58
D (8143) httpd: httpd_process_session: processing socket 57
D (8143) httpd_sess: httpd_sess_process: httpd_req_new
D (8153) httpd_txrx: httpd_recv_with_opt: requested length = 128
D (8153) httpd_txrx: httpd_recv_with_opt: received length = 128
D (8163) httpd_parse: read_block: received HTTP request block size = 128
D (8173) httpd_parse: cb_url: message begin
D (8173) httpd_parse: cb_url: processing url = /hello
D (8183) httpd_parse: verify_url: received URI = /hello
D (8183) httpd_parse: cb_header_field: headers begin
D (8193) httpd_txrx: httpd_unrecv: length = 107
D (8193) httpd_parse: pause_parsing: paused
D (8193) httpd_parse: cb_header_field: processing field = Host
D (8203) httpd_txrx: httpd_recv_with_opt: requested length = 128
D (8213) httpd_txrx: httpd_recv_with_opt: pending length = 107
D (8213) httpd_parse: read_block: received HTTP request block size = 107
D (8223) httpd_parse: continue_parsing: skip pre-parsed data of size = 5
D (8233) httpd_parse: continue_parsing: un-paused
D (8233) httpd_parse: cb_header_value: processing value = 192.168.0.164
D (8243) httpd_parse: cb_header_field: processing field = Connection
D (8243) httpd_parse: cb_header_value: processing value = keep-alive
D (8253) httpd_parse: cb_header_field: processing field = Cache-Control
D (8263) httpd_parse: cb_header_value: processing value = max-age=0
D (8263) httpd_parse: cb_header_field: processing field = Upgrade-Insecure-Requests
D (8273) httpd_parse: cb_header_value: processing value = 1
D (8283) httpd_parse: cb_header_field: processing field = User-A
D (8283) httpd_parse: parse_block: parsed block size = 107
D (8293) httpd_txrx: httpd_recv_with_opt: requested length = 128
D (8293) httpd_txrx: httpd_recv_with_opt: received length = 128
D (8303) httpd_parse: read_block: received HTTP request block size = 128
D (8313) w5500.mac: receive len=419
D (8313) w5500.mac: receive len=439
D (8313) httpd_parse: cb_header_field: processing field = gent
D (8323) httpd_parse: cb_header_value: processing value = Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
D (8343) httpd_parse: cb_header_field: processing field = Acc
D (8343) httpd_parse: parse_block: parsed block size = 235
D (8353) httpd_txrx: httpd_recv_with_opt: requested length = 128
D (8353) httpd_txrx: httpd_recv_with_opt: received length = 128
D (8363) httpd_parse: read_block: received HTTP request block size = 128
D (8373) httpd_parse: cb_header_field: processing field = ept
D (8373) httpd_parse: cb_header_value: processing value = text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchang
D (8393) httpd_parse: parse_block: parsed block size = 363
D (8393) httpd_txrx: httpd_recv_with_opt: requested length = 128
D (8403) httpd_txrx: httpd_recv_with_opt: received length = 81
D (8413) httpd_parse: read_block: received HTTP request block size = 81
D (8413) httpd_parse: cb_header_value: processing value = e;v=b3;q=0.7
D (8423) httpd_parse: cb_header_field: processing field = Accept-Encoding
D (8433) httpd_parse: cb_header_value: processing value = gzip, deflate
D (8433) httpd_parse: cb_header_field: processing field = Accept-Language
D (8443) httpd_parse: cb_header_value: processing value = en-US,en;q=0.9
D (8453) httpd_parse: cb_headers_complete: bytes read     = 468
D (8453) httpd_parse: cb_headers_complete: content length = 0
D (8463) event: no handlers have been registered for event ESP_HTTP_SERVER_EVENT:3 posted to loop 0x3ffb8074
D (8463) httpd_parse: pause_parsing: paused
D (8473) httpd_parse: cb_no_body: message complete
D (8483) httpd_parse: httpd_parse_req: parsing complete
D (8483) httpd_uri: httpd_uri: request for /hello with type 1
D (8493) httpd_uri: httpd_find_uri_handler: [0] = /hello
I (8493) example: Found header => Host: 192.168.0.164
D (8503) httpd_txrx: httpd_resp_set_hdr: new header = Custom-Header-1: Custom-Value-1
D (8513) httpd_txrx: httpd_resp_set_hdr: new header = Custom-Header-2: Custom-Value-2
D (8513) w5500.mac: receive len=60
D (8523) httpd_txrx: httpd_send_all: sent = 62
D (8523) w5500.mac: receive len=60
D (8533) httpd_txrx: httpd_send_all: sent = 15
D (8533) w5500.mac: receive len=60
D (8543) httpd_txrx: httpd_send_all: sent = 2
D (8543) w5500.mac: receive len=60
D (8543) w5500.mac: receive len=60
D (8553) httpd_txrx: httpd_send_all: sent = 14
D (8553) httpd_txrx: httpd_send_all: sent = 2
D (8553) w5500.mac: receive len=60
D (8563) httpd_txrx: httpd_send_all: sent = 15
D (8563) w5500.mac: receive len=60
D (8573) httpd_txrx: httpd_send_all: sent = 2
D (8573) w5500.mac: receive len=60
D (8583) httpd_txrx: httpd_send_all: sent = 14
D (8583) w5500.mac: receive len=60
D (8583) w5500.mac: receive len=60
D (8583) httpd_txrx: httpd_send_all: sent = 2
D (8593) w5500.mac: receive len=60
D (8593) httpd_txrx: httpd_send_all: sent = 2
D (8603) event: no handlers have been registered for event ESP_HTTP_SERVER_EVENT:4 posted to loop 0x3ffb8074
D (8613) w5500.mac: receive len=60
D (8613) httpd_txrx: httpd_send_all: sent = 12
D (8623) event: no handlers have been registered for event ESP_HTTP_SERVER_EVENT:6 posted to loop 0x3ffb8074
I (8633) example: Request headers lost
D (8633) httpd_sess: httpd_sess_process: httpd_req_delete
D (8643) httpd_sess: httpd_sess_process: success
D (8643) httpd: httpd_server: processing listen socket 54
D (8653) httpd: httpd_accept_conn: newfd = 58
D (8653) httpd_sess: httpd_sess_new: fd = 58
D (8653) httpd_sess: httpd_sess_new: active sockets: 2
D (8663) httpd: httpd_accept_conn: complete
D (8663) event: no handlers have been registered for event ESP_HTTP_SERVER_EVENT:2 posted to loop 0x3ffb8074
D (8663) httpd: httpd_server: doing select maxfd+1 = 59
D (9163) w5500.mac: receive len=333
D (9163) w5500.mac: receive len=353
D (9453) w5500.mac: receive len=226
D (10313) w5500.mac: receive len=419
D (10313) w5500.mac: receive len=439

I have not yet found why it is not working with the PsychicHttp library but thought I might post this log already. Maybe one of you, more clever people, can see what is going on. I'll keep searching for the solution in the meantime.

Pablo2048 commented 5 months ago

Why it is not working is obvious: Forget about Arduino Ethernet library and use this one instead https://github.com/espressif/arduino-esp32/tree/master/libraries/Ethernet , start with this example https://github.com/espressif/arduino-esp32/blob/master/libraries/Ethernet/examples/ETH_W5500_Arduino_SPI/ETH_W5500_Arduino_SPI.ino and after making it work just add PsychicHttp...

JC-Electronics-Design commented 5 months ago

Ok, I'll try that. Thanks!

JC-Electronics-Design commented 5 months ago

I've tried the library you referred to using Arduino IDE and that worked no problem.

But since I'm using PlatformIO in VS Code I am not able to use the library you referred to. The next problem that popped up is that the platform-espressif32 core is not yet updated to ESP-IDF 5.x and thus the w5500 support was not yet added. Luckily I found this github repository that showed how we can use the latest framework that integrates ESP-IDF 5.1.2. There also seems to be this option but did not test that.

Once that framework was installed in my project I got the error that "esp_https_server.h", included in the "PsychicHttpsServer.h" and "PsychicHttpsServer.cpp" files, does not exist "No such file or directory". Since I'm not using them in my project I just deleted them from the library and that solved that issue.

After that I used the example code "ETH_W5500_Arduino_SPI.ino" @Pablo2048 referred to as a guide to write a test. Basically adding the following just before server.listen(80);

WiFi.onEvent(onEvent);

SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI, (uint8_t)14U);

Serial.print("Ethernet connecting");
while(!eth_connected) {
    Serial.print(".");
    delay(1000);
}

Serial.print("Ethernet: IP Address: ");
Serial.println(ETH.localIP());

I uploaded that test code waited for a connection and tried connecting to the server with the listed IP address. To my surprise I was able to request the webpage from my server. yeey! 😀 After a moment I saw the connected was lost and re-established and tried to request the webpage again and noticed that I could not connect to the server.

To try and solve the issue (maybe not in the best way, but what works), the first thing I tried was adding server.listen(80) in the onEvent() callback function.

case ARDUINO_EVENT_ETH_CONNECTED:
  Serial.println("ETH Connected");
  Serial.println("Start webserver");
  server.listen(80);
  break;

This did not work, still could not connect to the server.

After that I added server.stop() before calling server.listen(80) thinking the webserver object would be restarted of some sort. But that resulted in the ESP32 crashing when the server.stop() was called.

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

Core  1 register dump:
PC      : 0x40082d6b  PS      : 0x00060030  A0      : 0x8008e3f4  A1      : 0x3ffb3d00  
A2      : 0x3ffc2878  A3      : 0x00060023  A4      : 0xffffffe7  A5      : 0x00000000  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x80082d69  A9      : 0x3ffb3ce0  
A10     : 0x00000000  A11     : 0x3ffc2878  A12     : 0x3ffc2aa4  A13     : 0x00000000  
A14     : 0x3ffc292c  A15     : 0x3ffc2930  SAR     : 0x0000001e  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x0000001c  LBEG    : 0x40088fa0  LEND    : 0x40088fb6  LCOUNT  : 0xffffffff  

Backtrace: 0x40082d68:0x3ffb3d00 0x4008e3f1:0x3ffb3d20 0x4013b491:0x3ffb3d40 0x4013aff1:0x3ffb3d60 0x400d5239:0x3ffb3d80 0x40149a3b:0x3ffb3da0 0x4010557b:0x3ffb3dc0 0x400d4ce2:0x3ffb3df0 0x400d26cc:0x3ffb3e10 0x400d220d:0x3ffb3e30 0x400d3965:0x3ffb3f10 0x400d398c:0x3ffb4010

That clearly didn't work so I removed the server.stop() function from the onEvent callback and tried re-initialising the ethernet connection. Since there is a variable "eth_connection" available that stores whether a connection is made or not (updated in the onEvent callback) I used that in the loop to check when connection is lost. Once the connection is lost I re-initialise SPI and ethernet.

if(eth_connected) {
  // Some other tasks
}
else {
    SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
    ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI, (uint8_t)14U);

    Serial.print("Ethernet connecting");
    while(!eth_connected) {
        Serial.print(".");
        delay(1000);
    }

    Serial.print("Ethernet: IP Address: ");
    Serial.println(ETH.localIP());
}

This works perfectly. Once I disconnect the ethernet cable it tries to reconnect again and when I reconnect the ethernet cable I re-establish connection and can request the webpage from the server no problem.

This is not the prettiest solution but for now it works for my application. Now I can proceed again. Maybe with this information one of you can figure out what has to be done to make it work and can update the code/documentation. Thanks for the help!