Leonardo ETH - Detect if Ethernet is not working? Ethernet optional for device..

Hello,

I am building a device using a Leonardo ETH and it should be able to work also without ethernet connection (it can receive commands either via the the digital pins or via ethernet, the latter being optional), meaning that it wouldn't be connected to any router/network.

So I have the following in the setup():

if (Ethernet.begin(mac) == 0)
{
	// initialize the Ethernet device not using DHCP:
	Ethernet.begin(mac, ip); // Also possible: Ethernet.begin(mac, ip, myDns, gateway, subnet);
}
server.begin();

And in the loop() repeatedly something like that is called:

EthernetClient client = server.available();

/* client.available() will be only evaluated if the first is OK*/
/* Handle commands to the unit. */
if ((client) && (client.available()) >= 4)
{
	// We waited for a 4 byte command...
client.write(...)
}
	

	Ethernet.maintain();
}

And elsewhere in the loop():

server.write(...)

Now, as I am quite busy debugging (my other) code, I noticed that taking out all my network code seems to make my program work (there is not network cable attached for now). Therefore, I am wondering: Is it just the DHCP taking time (I didn't wait for 1-2 minutes until it can fail) or do I have a bigger problem?

Because I don't seem to have a chance of finding out if my network is blocking because it is not available, right?

Thanks!

Best,
Andreas

This will not wait for 4 bytes. If the 4 bytes are not there immediately, the section of code is skipped.

if ((client) && (client.available()) >= 4)
{
	// We waited for a 4 byte command...
client.write(...)
}

Thank you for your comment. It's executed in the loop(), so the code will return there until we there are 4 bytes. That part does what I expect it to do (I know it from another project where I use that design). The code does not stop there to wait, that is true... But that is not the problem.

So my question really is: What happens, if there is no LAN/Ethernet cable connected? Will I have troubles?

That depends on the rest of your sketch. If you are expecting 4 bytes over the ethernet connection, and it isn't connected, I'd say you are going to have a problem.

You could also have problems depending on how the client sends the 4 bytes.

Thanks for the answers. I figured it out:

Basically, the sketch execution stops for 1-2 minutes (probably waiting for DHCP) and then it proceeds. Please note that some pins (remember: I work with Leonardo ETH) might show unexpected behavior, e.g. Pin 10 will go to high during that (therefore do not put anything sensitive there, that should not experience that behavior).