Ethernet Library for Arduino http://arduino.cc/
  • C++ 95.3%
  • C 4.7%
Find a file
vratk0529-laptop-arch e66d2e54aa Add Ethernet.maintainNonBlocking(), fix DHCP retry and static fallback
maintain() blocks for up to the begin() timeout (60 s) on every renewal,
and while the DHCP server is unreachable it does so on every call.
maintainNonBlocking() runs the same exchange as a state machine: each
call sends or checks for at most one packet and returns 0 while the
renewal is in flight, then the usual 1-4 when it ends. The in-flight
exchange holds the DHCP socket until then. Blocking maintain() and
begin() now drive the same state machine in a loop.

A failed renew or rebind is now retried, as a rebind, DHCP_RETRY_SEC
(60 s) later rather than on every call. Previously a failed renew could
also leave the client stuck in DISCOVER, never retrying at all. A failed
begin() still lets the next maintain() retry at once, as upstream.

The static-IP begin() now drops the DHCP client, so a later maintain()
no longer overwrites the static address with a renewed lease or keeps
retrying a DHCP begin() that failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:16:07 +01:00
docs Add Ethernet.maintainNonBlocking(), fix DHCP retry and static fallback 2026-11-11 12:16:07 +01:00
examples Correct typos in documentation 2022-02-21 20:41:09 -08:00
src Add Ethernet.maintainNonBlocking(), fix DHCP retry and static fallback 2026-11-11 12:16:07 +01:00
AUTHORS change links to https if possible (#66) 2021-10-04 11:18:42 +02:00
keywords.txt Add Ethernet.maintainNonBlocking(), fix DHCP retry and static fallback 2026-11-11 12:16:07 +01:00
library.properties Add Ethernet.maintainNonBlocking(), fix DHCP retry and static fallback 2026-11-11 12:16:07 +01:00
README.md Add Ethernet.maintainNonBlocking(), fix DHCP retry and static fallback 2026-11-11 12:16:07 +01:00

Ethernet

A fork of arduino-libraries/Ethernet 2.0.2 for W5100, W5200 and W5500 based boards and shields, used by SmartLib. It is a drop-in replacement: same library name, same API, plus the additions below.

Changes in this fork

  • No more hangs when the peer disappears. Upstream spins forever in socketSend(), socketSendUDP() and EthernetClient::flush() if the cable is pulled or the peer moves networks: the socket stays ESTABLISHED, the chip never transmits, so it never times out either. Those waits now give up after 250 ms (a timed-out send returns 0, which callers already treat as failure), and the two "read until stable" register loops give up after 16 reads.
  • DHCP hostname. Ethernet.setHostname("KitchenLight") before begin() sends a real DHCP hostname (option 12) instead of WIZnetXXXXXX, so the router's lease list says what the device is. Letters, digits and - only, at most 32 characters.
  • NTP server from DHCP. DHCP asks for NTP servers (option 42); Ethernet.ntpServerIP() returns the first one, or 0.0.0.0 if the lease has none.
  • Non-blocking DHCP renewal. Ethernet.maintainNonBlocking() does what maintain() does without ever waiting for the DHCP server: each call sends or checks for at most one packet, returns 0 while a renewal is in flight and 1-4 once it is done. Upstream's maintain() blocks for up to 60 s on every renewal, and on every call while the server is unreachable.
  • DHCP retry and static fallback fixes (affect maintain() too). A failed renew or rebind is retried 60 s later instead of on every call, and calling the static-IP begin(mac, ip, ...) after begin(mac) stops DHCP, so maintain() no longer replaces the static address or keeps retrying a failed DHCP begin.
  • Zephyr (Arduino UNO Q) compiles: EthernetClient::connect() uses IPAddress constructors that are unambiguous there.
  • ETHERNET_FORK_VRATK0529 is defined, so code can detect this fork.

Usage

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(9600);
  Ethernet.setHostname("KitchenLight");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("DHCP failed");
    return;
  }
  Serial.print("IP:  ");
  Serial.println(Ethernet.localIP());
  Serial.print("NTP: ");
  Serial.println(Ethernet.ntpServerIP());
}

void loop() {
  Ethernet.maintainNonBlocking();  // or Ethernet.maintain() to renew synchronously
}

Install by cloning this repository into your Arduino libraries folder (removing or shadowing the stock Ethernet library), or point PlatformIO's lib_deps at it.

Documentation

License

The source files carry their own licences, unchanged from upstream:

Files Licence
src/Ethernet.*, src/EthernetClient.cpp, src/EthernetServer.cpp, src/socket.cpp MIT (Paul Stoffregen, 2018)
src/EthernetUdp.* MIT (Bjoern Hartmann, 2008)
src/Dns.* Apache 2.0 (MCQN Ltd., 2009-2010)
src/utility/w5100.* GPL 2.0 or LGPL 2.1 (Paul Stoffregen, Cristian Maglie)
src/Dhcp.* No licence stated (Jordan Terrell, 2009)
src/EthernetClient.h, src/EthernetServer.h Public domain

Changes made in this fork are released under the licence of the file they are in. Contributors are listed in AUTHORS.