ESP32 DHCP server lan8720 ethernet interface using arduino SDK
Posted: 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.
my setup function is
and i declared the IPs as followed:
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
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);
}
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);
}
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);
any help, thanks