Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

ESP32 arduino with PlatformIO built in ETH.h library with ETH_LAN8720 #1

Closed
hitecSmartHome opened this issue Aug 16, 2021 · 5 comments
Labels
invalid This doesn't seem right Support Library support

Comments

@hitecSmartHome
Copy link

Hi!. I try to use my esp32 with ETH_LAN8720 board.

My ethernet connection is working properly, iam using it for a while now.
I'm trying the upnp lib for this but it isn't working yet.

This is the built in eth library that iam using with ethernet so far: ETH.h GITHUB

My current simplified code from what i try to do here:

// Debug Level from 0 to 4
#define _DDNS_GENERIC_LOGLEVEL_     1
#define _UPNP_LOGLEVEL_             3

// Select DDNS_USING_WIFI for boards using built-in WiFi, such as Nano-33-IoT
#define DDNS_USING_WIFI             false
#define DDNS_USING_ETHERNET         true

#include <WiFi.h>
#include <ETH.h>
#include <UPnP_Generic.h>



void ETHEvent(WiFiEvent_t event){
        if( event == SYSTEM_EVENT_ETH_START ){
            ETH.setHostname(hostName);
        }else if( event == SYSTEM_EVENT_ETH_GOT_IP ){
            ETH_Got_IP = true;
            CurrentIP = ETH.localIP();
            displayETH();
            canScrollNoties  = true;
            if(!ETH_Timeout_Task_Deleted){ vTaskDelete(ETH_Timeout_Task_Handle); }
            gotIpMillis = millis();
            canSendRestartEmail = true;
            WiFi.softAPdisconnect(true);
            Ota_Begin();
            canSetupUPNP = true;
        }else if( event == SYSTEM_EVENT_ETH_DISCONNECTED ){
            ETH_Got_IP = false;
            displayNoIp("eth");
            ETH_Ip_Timeout_Start();
        }
 }

static const inline void ethernetBegin(){
// this is working perfectly well.

        ETH.begin(
            ETH_PHY_ADDR,
            PIN_PHY_POWER,
            PIN_SMI_MDC,
            PIN_SMI_MDIO,
            ETH_PHY_LAN8720,
            ETH_CLK_MODE
        );
        delay(10);
        if(!DHCP_Status){ // flag from a config file...
            ETH.config(ip, sub, gw);
        }
        WiFi.onEvent(ETHEvent);
 }

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

void loop(){
 startUPNP_Task();
}





/*********** UPNP THINGS FROM HERE IN DIFFERENT TASK ***********/



void onUpdateCallback(const char* oldIP, const char* newIP){
  (void) oldIP;
  Serial.print(F("DDNSGeneric - IP Change Detected: "));
  Serial.println(newIP);
}

static const inline void setupUPNP(){
    uPnP = new UPnP(30000);
    if (uPnP){
        uPnP->addPortMappingConfig(CurrentIP, 80, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME);
        bool portMappingAdded = false;
        #define RETRY_TIMES 4
        int retries         = 0;
        while (!portMappingAdded && (retries < RETRY_TIMES)){
            Serial.println("Add Port Forwarding, Try # " + String(++retries));
            int result = uPnP->commitPortMappings();
            portMappingAdded = ( (result == PORT_MAP_SUCCESS) || (result == ALREADY_MAPPED) );
            if (!portMappingAdded){
                if (retries < RETRY_TIMES)
                vTaskDelay(10000);
            }
        }
        uPnP->printAllPortMappings();
        Serial.println(F("\nUPnP done"));
    }
}

static const inline void upnp_loop(){
    uPnP->updatePortMappings(600000);
}

void UPNP_Task( void * parameter ){
    setupUPNP();
    for ever{
        upnp_loop();
        vTaskDelay(1000);
    }
}

static const inline void startUPNP_Task(){
    if(canSetupUPNP){
        canSetupUPNP = false;
        xTaskCreatePinnedToCore(UPNP_Task,"UPNP_Task",5000,NULL,3,&UPNP_Task_Handle,0);
    }
}

Serial OUTPUT :

Time configured from NTP server was successful.
[UPnP] UDP_TX_PACKET_MAX_SIZE= 1000
[UPnP] UDP_TX_RESPONSE_MAX_SIZE= 8192
[UPnP] newUpnpRule= 192.168.10.110
Add Port Forwarding, Try # 1
[UPnP] Testing WiFi connection for IP : 0.0.0.0
.Generated admin Code: 95100816
....................[W][AsyncTCP.cpp:969] _poll(): rx timeout 4
...............................................................................................................................[UPnP]  ==> Timeout verify WiFi connection
[UPnP] ERROR: not connected to WiFi, can't continue
Add Port Forwarding, Try # 2
[UPnP] Testing WiFi connection for IP : 0.0.0.0
....................................................................................................................................................[UPnP]  ==> Timeout verify WiFi connection        
[UPnP] ERROR: not connected to WiFi, can't continue
Add Port Forwarding, Try # 3
[UPnP] Testing WiFi connection for IP : 0.0.0.0
....................................................................................................................................................[UPnP]  ==> Timeout verify WiFi connection        
[UPnP] ERROR: not connected to WiFi, can't continue
Add Port Forwarding, Try # 4
[UPnP] Testing WiFi connection for IP : 0.0.0.0
....................................................................................................................................................[UPnP]  ==> Timeout verify WiFi connection        
[UPnP] ERROR: not connected to WiFi, can't continue
[UPnP] isGatewayInfoValid : 0.0.0.0 , port : 0
[UPnP] Path :  , actionPort : 0
[UPnP] actionPath :  , serviceTypeName :
[UPnP] Gateway info not valid
[UPnP] Invalid router info, can't continue

UPnP done
[UPnP] Updating port mapping
[UPnP] Testing WiFi connection for IP : 0.0.0.0
....................................................................................................................................................[UPnP]  ==> Timeout verify WiFi connection        
[UPnP] ERROR: not connected to WiFi, can't continue
[UPnP] ERROR: While updating UPnP port mapping

UPNP can't see my gateway IP for some reason.

@hitecSmartHome
Copy link
Author

I can see that the WT32_ETH01 is using my LAN_8720 chip.

If i define it like this:

#define UPNP_USING_WT32_ETH01 true

I got a compiler error:
.pio\build\esp32dev\src\main.cpp.o:(.literal._ZN4UPnP16testConnectivityEm+0x4): undefined reference toWT32_ETH01_eth_connected`

@hitecSmartHome
Copy link
Author

hitecSmartHome commented Aug 16, 2021

Managed to compile by removed the ETH.h lib from my includes and replaced with

//#include <ETH.h>
#include <WebServer_WT32_ETH01.h>

after that i got an other compile error because iam using an other webserver lib from Me_no_dev and i had to comment out the
webserver lib like this : //#include <WebServer.h> // Introduce corresponding libraries from the WebServer_WT32_ETH01.h header.

Now it is compiled but can't make it to work.

Serial outout:

[UPnP] UDP_TX_PACKET_MAX_SIZE= 1000
[UPnP] UDP_TX_RESPONSE_MAX_SIZE= 8192
[UPnP] newUpnpRule= 0.0.0.0
Add Port Forwarding, Try # 1
[UPnP] Testing WT32_ETH01 Ethernet connection for IP : 192.168.10.110
....................................................................................................................................................[UPnP]  ==> Timeout verify WT32_ETH01 Ethernet connection
[UPnP] ERROR: not connected to Ethernet Network, can't continue
Add Port Forwarding, Try # 2
[UPnP] Testing WT32_ETH01 Ethernet connection for IP : 192.168.10.110
.........................................................................................................E (67767) esp_littlefs: Failed to opendir "/users.json". Error  (-2)
............................Generated admin Code: 95100816
....[W][esp32-hal-i2c.c:1419] i2cCheckLineState(): invalid state sda(32)=0, scl(33)=1
...........[UPnP]  ==> Timeout verify WT32_ETH01 Ethernet connection
[UPnP] ERROR: not connected to Ethernet Network, can't continue     
Add Port Forwarding, Try # 3
[UPnP] Testing WT32_ETH01 Ethernet connection for IP : 192.168.10.110
....................................................................................................................................................[UPnP]  ==> Timeout verify WT32_ETH01 Ethernet connection
[UPnP] ERROR: not connected to Ethernet Network, can't continue
Add Port Forwarding, Try # 4
[UPnP] Testing WT32_ETH01 Ethernet connection for IP : 192.168.10.110
....................................................................................................................................................[UPnP]  ==> Timeout verify WT32_ETH01 Ethernet connection
[UPnP] ERROR: not connected to Ethernet Network, can't continue
[UPnP] isGatewayInfoValid : 0.0.0.0 , port : 0
[UPnP] Path :  , actionPort : 0
[UPnP] actionPath :  , serviceTypeName :
[UPnP] Gateway info not valid
[UPnP] Invalid router info, can't continue

UPnP done

it says that the esp not connected to Ethernet network. But this is not true because i can reach my webserver and everything else fine.

@hitecSmartHome
Copy link
Author

Okay. I found the issue.
It checked a boolean variable from the lib's callback. I added this boolean to my ETH callback and it is working. I also had to increase the stack depth of my task because it restarted my esp every time it tried to connect.

Now it is failing and restarting my esp.

Last Serial outputs:

[UPnP] UDP_TX_PACKET_MAX_SIZE= 1000
[UPnP] UDP_TX_RESPONSE_MAX_SIZE= 8192
[UPnP] newUpnpRule= 0.0.0.0
Add Port Forwarding, Try # 1
[UPnP] Testing WT32_ETH01 Ethernet connection for IP : 192.168.10.110
 ==> GOOD
[UPnP] Internet connection GOOD
[UPnP] isGatewayInfoValid : 0.0.0.0 , port : 0
[UPnP] Path :  , actionPort : 0
[UPnP] actionPath :  , serviceTypeName :
[UPnP] Gateway info not valid

[UPnP] GW IP = 192.168.10.1
[UPnP] Sending M-SEARCH to : 239.255.255.250 , Port : 1900
[UPnP] Received packet, size = 335 , IP = 192.168.10.1
[UPnP] Port = 1900
[UPnP] IGD found
[UPnP] IGD location found : http://192.168.10.1:1900/igd.xml
[UPnP] 192.168.10.1
[UPnP] 1900
[UPnP] /igd.xml
[UPnP] Connecting to IGD with host : 192.168.10.1 , port : 1900
[UPnP] Connected to IGD
[UPnP] called getIGDEventURLs
[UPnP] deviceInfo->actionPath :  , deviceInfo->path : /igd.xml
[UPnP] urn:schemas-upnp-org:service:WANIPConnection:1 service found!
[UPnP] controlURL tag found! Setting actionPath to : /ipc
[UPnP] Port : 1900 , actionPort : 1900
[UPnP] isGatewayInfoValid : 192.168.10.1 , port : 1900
[UPnP] Path : /igd.xml , actionPort : 1900
[UPnP] actionPath : /ipc , serviceTypeName : urn:schemas-upnp-org:service:WANIPConnection:1
[UPnP] Gateway info valid
[UPnP] Verify port mapping for rule : Hitec Smart Home
[UPnP] Apply action : GetSpecificPortMappingEntry  on port mapping : Hitec Smart Home
[UPnP] Connecting to IGD with host : 192.168.10.1 , port : 1900
[UPnP] Connected to IGD
[UPnP] Host: 192.168.10.1:1900
[E][WiFiClient.cpp:395] write(): fail on fd 59, errno: 104, "Connection reset by peer"
[UPnP] Called addPortMappingEntry
[UPnP] Connecting to IGD with host : 192.168.10.1 , port : 1900
[UPnP] Connected to IGD
[UPnP] deviceInfo->actionPath : /ipc , deviceInfo->serviceTypeName : urn:schemas-upnp-org:service:WANIPConnection:1
[E][WiFiClient.cpp:439] read(): fail on fd 60, errno: 104, "Connection reset by peer"
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.Core 1 register dump:
PC      : 0x400f172c  PS      : 0x00060130  A0      : 0x800f1b3c  A1      : 0x3ffec990
A2      : 0x00000000  A3      : 0x3ffec9ef  A4      : 0x00000001  A5      : 0x00000001
A6      : 0x3ffc6464  A7      : 0x00000068  A8      : 0x00000000  A9      : 0x00000000
A10     : 0x00000000  A11     : 0x40089a04  A12     : 0x00000050  A13     : 0x00000000
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000008  LBEG    : 0x4008ca60  LEND    : 0x4008ca7c  LCOUNT  : 0xffffffff

ELF file SHA256: 0000000000000000

Backtrace: 0x400f172c:0x3ffec990 0x400f1b39:0x3ffec9b0 0x401c4c3d:0x3ffec9e0 0x40113fe9:0x3ffeca10 0x40114089:0x3ffeca30 0x400edc97:0x3ffeca50 0x400ef50d:0x3ffecab0 0x400ef69d:0x3ffecae0 0x40090912:0x3ffecb30
  #0  0x400f172c:0x3ffec990 in WiFiClientRxBuffer::read(unsigned char*, unsigned int) at C:\Users\Pc\.platformio\packages\framework-arduinoespressif32\libraries\WiFi\src/WiFiClient.cpp:572
  #1  0x400f1b39:0x3ffec9b0 in WiFiClient::read(unsigned char*, unsigned int) at C:\Users\Pc\.platformio\packages\framework-arduinoespressif32\libraries\WiFi\src/WiFiClient.cpp:572
  #2  0x401c4c3d:0x3ffec9e0 in WiFiClient::read() at C:\Users\Pc\.platformio\packages\framework-arduinoespressif32\libraries\WiFi\src/WiFiClient.cpp:572
  #3  0x40113fe9:0x3ffeca10 in Stream::timedRead() at C:\Users\Pc\.platformio\packages\framework-arduinoespressif32\cores\esp32/Stream.cpp:76
  #4  0x40114089:0x3ffeca30 in Stream::readStringUntil(char) at C:\Users\Pc\.platformio\packages\framework-arduinoespressif32\cores\esp32/Stream.cpp:76
  #5  0x400edc97:0x3ffeca50 in UPnP::addPortMappingEntry(_gatewayInfo*, _upnpRule*) at src/Own_Headers/upnp.h:7
  #6  0x400ef50d:0x3ffecab0 in UPnP::commitPortMappings() at src/Own_Headers/upnp.h:7
  #7  0x400ef69d:0x3ffecae0 in UPNP_Task(void*) at src/Own_Headers/upnp.h:7       
      (inlined by) UPNP_Task(void*) at src/Own_Headers/upnp.h:36
  #8  0x40090912:0x3ffecb30 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...
ets Jul 29 2019 12:21:46

@hitecSmartHome
Copy link
Author

hitecSmartHome commented Aug 16, 2021

Finally i'm at a stage where nothing is rebooting and it's just trying the UPNP to my router.

So far it's always timeout or connection reset by peer..

Serial output:

[UPnP] UDP_TX_PACKET_MAX_SIZE= 1000
[UPnP] UDP_TX_RESPONSE_MAX_SIZE= 8192
[UPnP] newUpnpRule= 0.0.0.0
[UPnP] Testing WT32_ETH01 Ethernet connection for IP : 192.168.10.110
 ==> GOOD
[UPnP] Internet connection GOOD
[UPnP] isGatewayInfoValid : 0.0.0.0 , port : 0
[UPnP] Path :  , actionPort : 0
[UPnP] actionPath :  , serviceTypeName :
[UPnP] Gateway info not valid

[UPnP] GW IP = 192.168.10.1
[UPnP] Sending M-SEARCH to : 239.255.255.250 , Port : 1900
[UPnP] Received packet, size = 335 , IP = 192.168.10.1
[UPnP] Port = 1900
[UPnP] IGD found
[UPnP] IGD location found : http://192.168.10.1:1900/igd.xml
[UPnP] 192.168.10.1
[UPnP] 1900
[UPnP] /igd.xml
[UPnP] Connecting to IGD with host : 192.168.10.1 , port : 1900
[UPnP] Connected to IGD
[UPnP] called getIGDEventURLs
[UPnP] deviceInfo->actionPath :  , deviceInfo->path : /igd.xml
[UPnP] urn:schemas-upnp-org:service:WANIPConnection:1 service found!
[UPnP] controlURL tag found! Setting actionPath to : /ipc
[UPnP] Port : 1900 , actionPort : 1900
[UPnP] isGatewayInfoValid : 192.168.10.1 , port : 1900
[UPnP] Path : /igd.xml , actionPort : 1900
[UPnP] actionPath : /ipc , serviceTypeName : urn:schemas-upnp-org:service:WANIPConnection:1
[UPnP] Gateway info valid
[UPnP] Verify port mapping for rule : Hitec Smart Home
[UPnP] Apply action : GetSpecificPortMappingEntry  on port mapping : Hitec Smart Home
[UPnP] Connecting to IGD with host : 192.168.10.1 , port : 1900
[UPnP] Connected to IGD
[UPnP] Host: 192.168.10.1:1900
[E][WiFiClient.cpp:395] write(): fail on fd 59, errno: 104, "Connection reset by peer"
[UPnP] Timeout adding a port mapping

[UPnP] Internet connection GOOD
[UPnP] isGatewayInfoValid : 192.168.10.1 , port : 1900
[UPnP] Path : /igd.xml , actionPort : 1900
[UPnP] actionPath : /ipc , serviceTypeName : urn:schemas-upnp-org:service:WANIPConnection:1
[UPnP] Gateway info valid
[UPnP] Port : 1900 , actionPort : 1900
[UPnP] isGatewayInfoValid : 192.168.10.1 , port : 1900
[UPnP] Path : /igd.xml , actionPort : 1900
[UPnP] actionPath : /ipc , serviceTypeName : urn:schemas-upnp-org:service:WANIPConnection:1
[UPnP] Gateway info valid
[UPnP] Verify port mapping for rule : Hitec Smart Home
[UPnP] Apply action : GetSpecificPortMappingEntry  on port mapping : Hitec Smart Home
[UPnP] Connecting to IGD with host : 192.168.10.1 , port : 1900
[UPnP] Connected to IGD
[UPnP] Host: 192.168.10.1:1900
[E][WiFiClient.cpp:395] write(): fail on fd 57, errno: 104, "Connection reset by peer"

Tried on port 80 , 8080, 1900. Once it was successfull with port 80. But nothing ever since.

@khoih-prog
Copy link
Owner

I just can only suggest you start over from the examples written specifically for WT32-ETH01 in WT32_ETH01 examples and move forward gradually from there.

Check SimpleServer_WT32_ETH01 on WT32-ETH01 with ETH_PHY_LAN8720 and see it has been working OK.

I'm closing the issue now unless you can prove there is a bug of the library after correctly following the examples.

@khoih-prog khoih-prog added invalid This doesn't seem right Support Library support labels Aug 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right Support Library support
Projects
None yet
Development

No branches or pull requests

2 participants