Skip to content

Commit 590b4d3

Browse files
authored
Enable static IP via ethernet
1 parent 79e5d4c commit 590b4d3

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

libraries/WiFi/src/ETH.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
ETH.h - espre ETH PHY support.
3-
Based on WiFi.h from Ardiono WiFi shield library.
3+
Based on WiFi.h from Arduino WiFi shield library.
44
Copyright (c) 2011-2014 Arduino. All right reserved.
55
66
This library is free software; you can redistribute it and/or
@@ -108,26 +108,43 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
108108
return false;
109109
}
110110

111-
/*
112111
bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
113112
{
114-
if(!initialized){
113+
esp_err_t err = ESP_OK;
114+
tcpip_adapter_ip_info_t info;
115+
116+
if(local_ip != (uint32_t)0x00000000){
117+
info.ip.addr = static_cast<uint32_t>(local_ip);
118+
info.gw.addr = static_cast<uint32_t>(gateway);
119+
info.netmask.addr = static_cast<uint32_t>(subnet);
120+
} else {
121+
info.ip.addr = 0;
122+
info.gw.addr = 0;
123+
info.netmask.addr = 0;
124+
}
125+
126+
err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
127+
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED){
128+
log_e("DHCP could not be stopped! Error: %d", err);
115129
return false;
116130
}
117131

118-
tcpip_adapter_ip_info_t info;
119-
info.ip.addr = static_cast<uint32_t>(local_ip);
120-
info.gw.addr = static_cast<uint32_t>(gateway);
121-
info.netmask.addr = static_cast<uint32_t>(subnet);
122-
123-
if(!staticIP){
124-
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
125-
}
126-
if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info) == ESP_OK) {
132+
err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
133+
if(err != ERR_OK){
134+
log_e("STA IP could not be configured! Error: %d", err);
135+
return false;
136+
}
137+
if(info.ip.addr){
127138
staticIP = true;
128139
} else {
129-
return false;
140+
err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH);
141+
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED){
142+
log_w("DHCP could not be started! Error: %d", err);
143+
return false;
144+
}
145+
staticIP = false;
130146
}
147+
131148
ip_addr_t d;
132149
d.type = IPADDR_TYPE_V4;
133150

@@ -145,7 +162,6 @@ bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, I
145162

146163
return true;
147164
}
148-
*/
149165

150166
IPAddress ETHClass::localIP()
151167
{

libraries/WiFi/src/ETH.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ class ETHClass {
5858

5959
bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, eth_phy_type_t type=ETH_PHY_TYPE);
6060

61-
// NOT WORKING YET!
62-
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
61+
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
6362

6463
const char * getHostname();
6564
bool setHostname(const char * hostname);

0 commit comments

Comments
 (0)