Skip to content

Commit 70d0d46

Browse files
copercinime-no-dev
authored andcommitted
Enable static IP via ethernet (espressif#924)
1 parent cfbb730 commit 70d0d46

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

libraries/WiFi/src/ETH.cpp

+30-14
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
@@ -110,26 +110,43 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
110110
return false;
111111
}
112112

113-
/*
114113
bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
115114
{
116-
if(!initialized){
115+
esp_err_t err = ESP_OK;
116+
tcpip_adapter_ip_info_t info;
117+
118+
if(local_ip != (uint32_t)0x00000000){
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+
} else {
123+
info.ip.addr = 0;
124+
info.gw.addr = 0;
125+
info.netmask.addr = 0;
126+
}
127+
128+
err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
129+
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED){
130+
log_e("DHCP could not be stopped! Error: %d", err);
117131
return false;
118132
}
119133

120-
tcpip_adapter_ip_info_t info;
121-
info.ip.addr = static_cast<uint32_t>(local_ip);
122-
info.gw.addr = static_cast<uint32_t>(gateway);
123-
info.netmask.addr = static_cast<uint32_t>(subnet);
124-
125-
if(!staticIP){
126-
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
127-
}
128-
if(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info) == ESP_OK) {
134+
err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
135+
if(err != ERR_OK){
136+
log_e("STA IP could not be configured! Error: %d", err);
137+
return false;
138+
}
139+
if(info.ip.addr){
129140
staticIP = true;
130141
} else {
131-
return false;
142+
err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH);
143+
if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED){
144+
log_w("DHCP could not be started! Error: %d", err);
145+
return false;
146+
}
147+
staticIP = false;
132148
}
149+
133150
ip_addr_t d;
134151
d.type = IPADDR_TYPE_V4;
135152

@@ -147,7 +164,6 @@ bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, I
147164

148165
return true;
149166
}
150-
*/
151167

152168
IPAddress ETHClass::localIP()
153169
{

libraries/WiFi/src/ETH.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ class ETHClass {
6262

6363
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, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
6464

65-
// NOT WORKING YET!
66-
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
65+
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);
6766

6867
const char * getHostname();
6968
bool setHostname(const char * hostname);

0 commit comments

Comments
 (0)