I am writing a sketch where I want my micro to connect via DHCP, with a fallback option of using a programmed IP if not connected to DHCP.
I wrote the following:
if (!m_Settings.UseDHCP || (m_Settings.UseDHCP && !Ethernet.begin(mac)))
{
Ethernet.begin(mac, m_Settings.ip);
}
The problem is that when DHCP is not working and my code enters the if statement (which it will, after a few seconds, verified) the Ethernet.begin(mac, ip) won't work for some reason.
The problem is that when DHCP is not working and my code enters the if statement (which it will, after a few seconds, verified) the Ethernet.begin(mac, ip) won't work for some reason.
What do you mean by "enters the if statement"? An if statement doesn't have an entry point.
An if statement is evaluated. The result is either true or false. If true, the body of the if statement is executed.
As to what happens in this if statement, m_Setting.UseDHCP is a bool in a struct. When true, I want to try Ethernet.begin(mac). If m_Setting.UseDHCP == false, or if m_Setting.UseDHCP == true and Ethernet.begin(mac) returns 0, I want to execute Ethernet.begin(mac, ip).
You test the return code from the begin() method that uses DHCP, but not the one that doesn't. Is there a reason that you assume that the second method will always work?