I can't get this program to run on ESP32 with PlatformIO

Can you post the whole serial output as text? I’m missing everything before 1.5 seconds.

Sure,

 *  Executing task: C:\Users\Oz\.platformio\penv\Scripts\platformio.exe device monitor --environment eth 

--- Terminal on COM19 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x16 (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:13104
load:0x40080400,len:3036
entry 0x400805e4
[     6][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
starting wifi onevent
ending wifi onevent
starting eth begin
[  1595][V][WiFiGeneric.cpp:437] _arduino_event_cb(): Ethernet Started
[  1596][D][WiFiGeneric.cpp:931] _eventCallback(): Arduino Event: 18 - ETH_START
[  1598][V][WiFiGeneric.cpp:430] _arduino_event_cb(): Ethernet Link Up
[  1604][D][WiFiGeneric.cpp:931] _eventCallback(): Arduino Event: 20 - ETH_CONNECTED
[  1608][V][WiFiGeneric.cpp:445] _arduino_event_cb(): Ethernet got newip:192.168.0.108
[  1619][D][WiFiGeneric.cpp:931] _eventCallback(): Arduino Event: 22 - ETH_GOT_IP
[  1626][D][WiFiGeneric.cpp:1032] _eventCallback(): ETH IP: 192.168.0.108, MASK: 255.255.255.0, GW: 192.168.0.1
ending eth begin
testing client
testing client
testing client

That output should be literally impossible according to everything I’m reading in the source code. The WiFi.onEvent() should add your handler function to the std::vector cbEventList. The _eventCallback() code should go through all entries in the std::vector and execute the callback. But somehow it doesn’t call the registered callback, or the event callback list has emptied itself.

However, I am seeing one possibly critical PR in 2.0.7 that has to do with ethernet and events

Let’s quickly eliminate whether the error is due to PlatformIO using 2.0.6 and not the latest 2.0.7 yet. Add to the platformio.ini

platform_packages = framework-arduinoespressif32@https://github.com/espressif/arduino-esp32/archive/refs/tags/2.0.7.zip

after saving, wait for it to complete downloading and updating, then upload + monitor the example again.

Ok I added this to the ini as follows;

[env:eth]
platform = espressif32
platform_packages = framework-arduinoespressif32@https://github.com/espressif/arduino-esp32/archive/refs/tags/2.0.7.zip
;platform = espressif32@6.0.1
;platform = espressif32@3.5.0
board = esp32dev
framework = arduino
monitor_speed = 115200
build_unflags = -Werror=reorder
build_flags = -DCORE_DEBUG_LEVEL=5
;lib_archive = no

Is this correct ?

I tried this now and it still loops on testing client, suggesting that ethernet is not recognized.

Your .ini is correct, yes.

Absolutely mind-boggling that it still doesn’t work. We’re now on “99% sure this is an Arduino-ESP32 2.x bug or an extremely weird behavior” level.

  1. After ETH.begin();, add delay(1000);
  2. As first thing in loop(), add WiFi.disconnect(true);

Is the output different?

Im afraid not…


--- Terminal on COM19 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x16 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
[  1603][D][WiFiGeneric.cpp:931] _eventCallback(): Arduino Event: 20 - ETH_CONNECTED     
[  3597][V][WiFiGeneric.cpp:434] _arduino_event_cb(): Ethernet Link Down
[  3598][D][WiFiGeneric.cpp:931] _eventCallback(): Arduino Event: 21 - ETH_DISCONNECTED  
ending eth begin
testing client
[  5597][V][WiFiGeneric.cpp:430] _arduino_event_cb(): Ethernet Link Up
[  5598][D][WiFiGeneric.cpp:931] _eventCallback(): Arduino Event: 20 - ETH_CONNECTED     [  5602][V][WiFiGeneric.cpp:445] _arduino_event_cb(): Ethernet got newip:192.168.0.108   
[  5608][D][WiFiGeneric.cpp:931] _eventCallback(): Arduino Event: 22 - ETH_GOT_IP        
[  5615][D][WiFiGeneric.cpp:1032] _eventCallback(): ETH IP: 192.168.0.108, MASK: 255.255.255.0, GW: 192.168.0.1
testing client
testing client
testing client

Again, as a sanity check, I reverted the ini to this:

[env:eth]
;platform = espressif32
;platform_packages = framework-arduinoespressif32@https://github.com/espressif/arduino-esp32/archive/refs/tags/2.0.7.zip
;platform = espressif32@6.0.1
platform = espressif32@3.5.0
board = esp32dev
framework = arduino
monitor_speed = 115200
build_unflags = -Werror=reorder
build_flags = -DCORE_DEBUG_LEVEL=5
;lib_archive = no

and again, I get a successful eth connect:

 *  Executing task: C:\Users\Oz\.platformio\penv\Scripts\platformio.exe device monitor --environment eth 

--- Terminal on COM19 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x16 (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:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
starting wifi onevent
ending wifi onevent
starting eth begin
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 21 - ETH_START
ETH Started
ending eth begin
testing client
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 23 - ETH_CONNECTED
ETH Connected
[D][WiFiGeneric.cpp:374] _eventCallback(): Event: 25 - ETH_GOT_IP
[D][WiFiGeneric.cpp:455] _eventCallback(): ETH IP: 192.168.0.108, MASK: 255.255.255.0, GW: 192.168.0.1
E8:68:E7:11:B2:5F, IPv4: 192.168.0.108, FULL_DUPLEX, 100Mbps
testing client
starting testclient

connecting to google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Fri, 03 Mar 2023 20:38:40 GMT
Expires: Sun, 02 Apr 2023 20:38:40 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
closing connection

Revert these two changes again.

Change the event function to just

void WiFiEvent(WiFiEvent_t event)
{
  ESP_LOGI("EVENT: %d\n", (int) event);
}

what does the output say?

It doesn’t compile (apologies I am new to C++)

I am getting the following error:

src/main.cpp:47:28: error: expected primary-expression before 'int'

main.cpp :

#include <ETH.h>

static bool eth_connected = false;
//static volatile bool eth_connected = false;

/*
void WiFiEvent(WiFiEvent_t event) {
  switch (event) {
    case SYSTEM_EVENT_ETH_START:
      Serial.println("ETH Started");
      //set eth hostname here
      ETH.setHostname("esp32-ethernet");
      break;
    case SYSTEM_EVENT_ETH_CONNECTED:
      Serial.println("ETH Connected");
      break;
    case SYSTEM_EVENT_ETH_GOT_IP:
      Serial.print("ETH MAC: ");
      Serial.print(ETH.macAddress());
      Serial.print(", IPv4: ");
      Serial.print(ETH.localIP());
      if (ETH.fullDuplex()) {
        Serial.print(", FULL_DUPLEX");
      }
      Serial.print(", ");
      Serial.print(ETH.linkSpeed());
      Serial.println("Mbps");
      eth_connected = true;
      break;
    case SYSTEM_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case SYSTEM_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    default:
      break;
  }
}
*/

void WiFiEvent(WiFiEvent_t event)
{
  ESP_LOGI("EVENT: %d\n", (int) event);
}

void testClient(const char * host, uint16_t port) {
  Serial.println("starting testclient");
  Serial.print("\nconnecting to ");
  Serial.println(host);

  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    return;
  }
  client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
  while (client.connected() && !client.available());
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("closing connection\n");
  client.stop();
}

void setup() {
  Serial.begin(115200);
  Serial.println("starting wifi onevent");
  WiFi.onEvent(WiFiEvent);
  Serial.println("ending wifi onevent");
  //ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
  Serial.println("starting eth begin");
  ETH.begin(0, 2, 23, 18, ETH_PHY_LAN8720, ETH_CLOCK_GPIO17_OUT);
  Serial.println("ending eth begin");
  ETH.dnsIP();
  
}


void loop() {
  Serial.println("testing client");
  if (eth_connected) {
    testClient("google.com", 80);
    //testClient("208.67.222.222", 80);
  }
  delay(10000);
}

I copy-pasted your exact code with config

[env:esp32dev]
platform = espressif32@6.0.1
board = esp32dev
framework = arduino
platform_packages = framework-arduinoespressif32@https://github.com/espressif/arduino-esp32/archive/refs/tags/2.0.7.zip

and it does compile.

My apologies, with build_flags = -DCORE_DEBUG_LEVEL=5 it wouldn’t compile. Now it does,

Where can I see the output from ESP_LOGI ?

The serial shows this now:

rst:0x1 (POWERON_RESET),boot:0x16 (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:13192
load:0x40080400,len:3028
entry 0x400805e4
starting wifi onevent
ending wifi onevent
starting eth begin
ending eth begin
testing client
testing client
testing client

the highest DCORE DEBUG LEVEL I could compile with is 2, but no additional info in the serial output from above.

Hi everybody,
the solution with platform = espressif32@3.5.0 works for me, but only for the ethernet part of my application. Unfortunately I have also the need for CAN communication and within this version (3.5.0) the TWAIN library is not present.
So the question for me is, how I can stay with version 3.5.0 for the ethernet part and for the rest with something like 6.0.2, so I can use the TWAIN library? I am also happy with other solutions.
Can anybody help me? Thank you in advance.

If you can create a minimal program that fails and just uses the Arduino-ESP32 and its built-in libraries, please immediately file a report on Issues · espressif/arduino-esp32 · GitHub to get it looked at and fixed.

Thank you, I just opened a new issue.

Great, I’ll keep a watch then at what people say in

1 Like

I think that troubleshooting on github will take a longer time. So I’m thinking about an interim solution.
Is it possible to use the latest platform version, but for the ethernet part, the library version 3.5.0?

I think the answer is here:

I changed my event function to switch on numeric results and it works as it did with earlier versions.

void WiFiEventHandler(WiFiEvent_t event)
{  
 // Serial.println(event);
  switch (event) {
    case SYSTEM_EVENT_STA_START:
      Serial.println("WiFi Started");
      break;
    case SYSTEM_EVENT_STA_CONNECTED:
      Serial.println("WiFi Connected");
      break;
    case SYSTEM_EVENT_STA_GOT_IP:
      Serial.print("WiFi  IP: ");
      Serial.print(WiFi.localIP());
      Serial.print(", SSID: ");
      Serial.print(WiFi.SSID());
      Serial.print(", MAC: ");
      Serial.print(WiFi.macAddress());
      Serial.print(", RSSI: ");
      Serial.print(WiFi.RSSI());
      Serial.println();
      break;
    case 18:
      Serial.println("ETH Started");
      //set eth hostname here
      ETH.setHostname("esp32-ethernet");
      break;
    case 19:
      Serial.println("ETH Stopped");
      eth_connected = false;
      break;
    case 20:
      Serial.println("ETH Connected");
      break;
    case 21:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      break;
    case 22:
      Serial.printf("ETH IPv4: ");
      Serial.print(ETH.localIP());
      Serial.printf("\nSpeed: %d Mbps\n", ETH.linkSpeed());
      eth_connected = true;
      break;
    default:
      break;
  }
}

I had the same problem some days ago.
You can use only the numbers or simply change all statements with: SYSTEM_EVENT_xxx to ARDUINO_EVENT_xxx.
(E.g. SYSTEM_EVENT_ETH_GOT_IP change to ARDUINO_EVENT_ETH_GOT_IP)
That’s it.