Ethernet Library for Arduino
http://arduino.cc/
- C++ 95.3%
- C 4.7%
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> |
||
|---|---|---|
| docs | ||
| examples | ||
| src | ||
| AUTHORS | ||
| keywords.txt | ||
| library.properties | ||
| README.md | ||
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()andEthernetClient::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")beforebegin()sends a real DHCP hostname (option 12) instead ofWIZnetXXXXXX, 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, or0.0.0.0if the lease has none. - Non-blocking DHCP renewal.
Ethernet.maintainNonBlocking()does whatmaintain()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'smaintain()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-IPbegin(mac, ip, ...)afterbegin(mac)stops DHCP, somaintain()no longer replaces the static address or keeps retrying a failed DHCP begin. - Zephyr (Arduino UNO Q) compiles:
EthernetClient::connect()usesIPAddressconstructors that are unambiguous there. ETHERNET_FORK_VRATK0529is 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
- docs/readme.md: wiring and supported hardware
- docs/api.md: API reference (upstream; fork additions are listed above)
- examples/: example sketches
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.