ESP32 DHCP server lan8720 ethernet interface using arduino SDK

aelsayedhamouda
Posts: 6
Joined: Mon Jul 06, 2020 11:28 pm

ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby aelsayedhamouda » Mon Jul 06, 2020 11:56 pm

hello everyone,
i need to implement a DHCP server on the ESP32 like the one on wifiAccessPoit.ino, but using the ethernet interface not the wifi interface.
i am using lan8720 and i used the eth_lan8720_internal_clock example successfully and now i can ping google with it or ping it from any one in my network.
but what i am going to do is connecting an ethernet camera to it and then access that camera through the esp. so i need the camera to take an ip when i connect it to the esp.

I searched a lot, but all i can find is the dhcp client not server. I tried to change the config method in the to include the dhcp server option as the one on the wifiAccessPoit.ino , but no luck. :( :( . knowing that i have to work using the arduino sdk not the idf decause all the project is using the arduino sdk.

My code is simply calling the config method after eth.begin() method, but i added the dhcp server option block from wifiAccessPoit.ino.

Code: Select all

bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
{
    esp_err_t err = ESP_OK;
    tcpip_adapter_ip_info_t info;

    if(local_ip != (uint32_t)0x00000000){
        info.ip.addr = static_cast<uint32_t>(local_ip);
        info.gw.addr = static_cast<uint32_t>(gateway);
        info.netmask.addr = static_cast<uint32_t>(subnet);
    } else {
        info.ip.addr = 0;
        info.gw.addr = 0;
        info.netmask.addr = 0;
        }

    err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
    if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED){
        log_e("DHCP could not be stopped! Error: %d", err);
        return false;
    }

    err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
    if(err != ERR_OK){
        log_e("STA IP could not be configured! Error: %d", err);
        return false;
}
//++++++++++++++++++++++++++++++++++++++++++++++++
        else{
        dhcps_lease_t lease;
        lease.enable = true;
        lease.start_ip.addr = static_cast<uint32_t>(local_ip) + (1 << 24);
        lease.end_ip.addr = static_cast<uint32_t>(local_ip) + (11 << 24);

        tcpip_adapter_dhcps_option(
            (tcpip_adapter_option_mode_t)TCPIP_ADAPTER_OP_SET,
            (tcpip_adapter_option_id_t)REQUESTED_IP_ADDRESS,
            (void*)&lease, sizeof(dhcps_lease_t)
        );
    }
//++++++++++++++++++++++++++++++++++++++++++++++++++    
    if(info.ip.addr){
        staticIP = true;
    } else {
        err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH);
        if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED){
            log_w("DHCP could not be started! Error: %d", err);
            return false;
        }
        staticIP = false;
    }

    ip_addr_t d;
    d.type = IPADDR_TYPE_V4;

    if(dns1 != (uint32_t)0x00000000) {
        // Set DNS1-Server
        d.u_addr.ip4.addr = static_cast<uint32_t>(dns1);
        dns_setserver(0, &d);
    }

    if(dns2 != (uint32_t)0x00000000) {
        // Set DNS2-Server
        d.u_addr.ip4.addr = static_cast<uint32_t>(dns2);
        dns_setserver(1, &d);
    }

    return true;
}

IPAddress ETHClass::localIP()
{
    tcpip_adapter_ip_info_t ip;
    if(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip)){
        return IPAddress();
    }
    return IPAddress(ip.ip.addr);
}

my setup function is

Code: Select all

void setup() {
  Serial.begin(115200);
  WiFi.onEvent(WiFiEvent);

  ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
  ETH.config(local_ip, gateway, subnet);
}
and i declared the IPs as followed:

Code: Select all

IPAddress local_ip = IPAddress(192,168,0,1);
  IPAddress gateway = IPAddress(192,168,0,1);
  IPAddress subnet = IPAddress(255,255,255,0);
note: i using the laptop as a dhcp client "the other end of the ethernet cable which connected to the esp", and i can ping the esp if i used static ip for the laptop, but not dhcp client.

any help, thanks :D

aelsayedhamouda
Posts: 6
Joined: Mon Jul 06, 2020 11:28 pm

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby aelsayedhamouda » Tue Jul 07, 2020 3:41 pm

another thing i noticed.
In the DHCP APIs start and stop in tcpip_adapter.h, it says that it is bind to softAP interface, is this means that it is only working on wifi not ethernet adapter

Code: Select all


/**
 * @brief  Start DHCP server
 *
 * @note   Currently DHCP server is bind to softAP interface.
 *
 * @param[in]  tcpip_if: the interface which we will start DHCP server
 *
 * @return ESP_OK
 *         ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS
 *         ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED
 */
esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if);


PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby PeterR » Wed Jul 08, 2020 10:46 am

Depends which version of the IDF you use.
DHCP server on wired is available in v4.1-beta1-317 but not earlier versions (well maybe 4.0 but not 3.x).

The tcp-ip initialisation interface has changed but other than that it all works. One of my earlier posts has the initialisation code in it.

PS
The key is that you have to create the interface with DHCP server capabilitity. You may then go on to initialise and start DHCPS.
& I also believe that IDF CAN should be fixed.

aelsayedhamouda
Posts: 6
Joined: Mon Jul 06, 2020 11:28 pm

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby aelsayedhamouda » Wed Jul 08, 2020 4:53 pm

PeterR wrote:
Wed Jul 08, 2020 10:46 am
Depends which version of the IDF you use.
DHCP server on wired is available in v4.1-beta1-317 but not earlier versions (well maybe 4.0 but not 3.x).

The tcp-ip initialisation interface has changed but other than that it all works. One of my earlier posts has the initialisation code in it.
thanks for replying.
i understand that the arduino sdk is using 3.x idf version. and saw that the idf newest releas is 4.2, which probably has the changes i need.
but the problem is, functions like tcpip_adapter_dhcps_start and stop, i can't reach their source code to replace the old with new, i huess it is not open source in this part. as you can see i can't replace all the idf package in my project.
could you please guide me to how switch these parts?

for
PeterR wrote:
Wed Jul 08, 2020 10:46 am
PS
The key is that you have to create the interface with DHCP server capabilitity. You may then go on to initialise and start DHCPS.
do you mean that i should do ETH.config before ETH.begin ? because i tried that and didn't work.

thanks for helping :) .

aelsayedhamouda
Posts: 6
Joined: Mon Jul 06, 2020 11:28 pm

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby aelsayedhamouda » Wed Jul 08, 2020 6:54 pm

thanks for replying
PeterR wrote:
Wed Jul 08, 2020 10:46 am
Depends which version of the IDF you use.
DHCP server on wired is available in v4.1-beta1-317 but not earlier versions (well maybe 4.0 but not 3.x).

The tcp-ip initialisation interface has changed but other than that it all works. One of my earlier posts has the initialisation code in it.
i understand that the arduino sdk uses 3.x version of the idf, but how do i replace functions like tcpip_adapter_dhcps_start or tcpip_adapter_dhcps_stop , or upgrade so to speak. I tried to get the source code for this functions but i couldn't. It seems that this part is not open source.
PeterR wrote:
Wed Jul 08, 2020 10:46 am
PS
The key is that you have to create the interface with DHCP server capabilitity. You may then go on to initialise and start DHCPS.
do you mean that I write ETH.config before ETH.begin ? because i tried that and it didn't work. Knowing that the config method perform the tcpip_adapter_dhcps_start function and also the options function that i use to set the interface as dhcp server.

thanks for you help :)

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby PeterR » Thu Jul 09, 2020 11:23 am

The ESP Ethernet is all open source. Some parts of BT and Wifi might be closed.

I don't know about Arduino. If Arduino is based on 3.1 then you will have a lot of work to do - unless you can find an Arduino DHCP server, there tend to be more 3rd party libraries for the Arduino so you might be lucky.

The IDF TCPIP adaptor/interface stack was very much reworked in 4.x such that you could state which interface(s) to enable DHCPS on (previously only Wifi). For the most part its just initialisation code changes but I picked up at least two major device driver issues along the way as other drivers were extensively reworked.

IJDK what state Arduino is at.
& I also believe that IDF CAN should be fixed.

aelsayedhamouda
Posts: 6
Joined: Mon Jul 06, 2020 11:28 pm

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby aelsayedhamouda » Fri Jul 17, 2020 12:59 am

thanks you very much peter, i decided to move from Arduino SDK to idf to avoid the whole problem.
but to use this feature "dhcp server", i had to work on release branch "release/v4.2" not a stable version tag "v4.0" at the time of this post. i thinks the v4.1 idf has the feature also but i decided to go with the newer one as i am using a release branch anyway.

thanks again :D

Whiti11s
Posts: 1
Joined: Sat Jun 20, 2020 11:52 am

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby Whiti11s » Sat Jul 18, 2020 10:49 am

The tcp-ip initialisation interface has changed however other than that everything works. One of my prior posts has the initialisation code in it mykfcexperience.

johnshelbyy
Posts: 2
Joined: Wed Sep 30, 2020 5:14 am

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby johnshelbyy » Wed Sep 30, 2020 5:25 am

Whiti11s wrote:
Sat Jul 18, 2020 10:49 am
The tcp-ip initialisation interface has changed however other than that everything works. One of my prior posts has the initialisation code in it mykfcexperience.
i dont like the new interface at all, your views?

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: ESP32 DHCP server lan8720 ethernet interface using arduino SDK

Postby PeterR » Wed Sep 30, 2020 11:33 pm

i dont like the new interface
Ok; I bite. What's not to like?
& I also believe that IDF CAN should be fixed.

Who is online

Users browsing this forum: No registered users and 102 guests