Latest Version: 2.0.0
This library class enables Dynamic Host Configuration Protocol (DHCP) functionality for the Wiznet W5500 chip. It also requires the Wiznet W5500 driver.
You can view the library’s source code on GitHub. Click here to see information on other versions of this library.
To include this library in your project, add the following at the top of your device code:
#require "W5500.device.lib.nut:2.2.0"
#require "W5500.DHCP.device.lib.nut:2.0.0"
Instantiates a new W5500.DHCP object using the main Wiznet driver.
Parameter | Data Type | Required? | Description |
---|---|---|---|
wiz | Wiznet driver | Yes | The driver controlling the W5500 |
The instance.
// Setup for an imp005
// Initialize the SPI port
interruptPin <- hardware.pinXC;
resetPin <- hardware.pinXA;
spiSpeed <- 1000;
spi <- hardware.spi0;
spi.configure(CLOCK_IDLE_LOW | MSB_FIRST | USE_CS_L, spiSpeed);
// Initialise Wiznet and DHCP
wiz <- W5500(interruptPin, spi, null, resetPin);
dhcp <- W5500.DHCP(wiz);
This method registers the function that will be called when an IP address is leased.
Parameter | Data Type | Required? | Description |
---|---|---|---|
callback | Function | Yes | The callback has one parameter of its own, error, which informs the user of timeouts and errors, or will be null |
Error Message | Description |
---|---|
"Offer Timeout" |
Discovery message sent with no response |
"Ack Timeout " |
Request message sent with no response |
"Renewal Timeout" |
Maximum Renewal attempts reached before a restart |
"Renewal Failed" |
Lease renewal failed |
"Request Declined" |
Requested IP not able to be leased |
"IP in use" |
Offered IP is currently in use |
"All connections in use" |
All Wiznet sockets are in use |
dhcp.onLease(function(error) {
if (error) return server.error(error);
server.log("DHCP lease obtained");
});
This method renews the lease or requests a new lease. When this is complete, the callback function registered with onLease() will be executed.
Parameter | Data Type | Required? | Description |
---|---|---|---|
timeout | Integer | No | The number of seconds to allow for the lease to try to be renewed before giving up. If set to 0, the timeout will be infinite. Default: 0 |
The instance (this).
dhcp.onLease(function(error) {
// Run this code when IP address is obtained
if (error) return server.error(error);
server.log("DHCP lease obtained");
});
dhcp.renewLease();
This method retrieves the leased IP address.
Array — The leased IP address as four integers.
local ip = dhcp.getIP();
server.log(format("IP address = %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));
This method retrieves the network subnet mask.
Array — The subnet mask as four integers.
local subnetMask = dhcp.getSubnetMask();
server.log(format("Subnet mask = %d.%d.%d.%d", subnetMask[0], subnetMask[1], subnetMask[2], subnetMask[3]));
This method retrieves the network gateway address.
Array — The gateway address as four integers.
local router = dhcp.getRouterAddress();
server.log(format("Router IP address = %d.%d.%d.%d", router[0], router[1], router[2], router[3]));
This method retrieves the lease duration in seconds.
Integer — the lease duration (seconds).
local leaseTime = dhcp.getLeaseTime();
server.log("IP address lease time: " + leaseTime + "s");
This method retrieves a set of DNS entries.
Array — The DNS entries, each of which is an array of four integers.
local dns = dhcp.getDNS();
foreach (index, server in dns) {
server.log(format("DNS #%d address = %d.%d.%d.%d", (index + 1), server[0], server[1], server[2], server[3]));
}
The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.
Version | Source Code | Notes |
---|---|---|
1.0.0 | GitHub | Initial release |
2.0.0 | GitHub | Version update to match WS5500 library |
This library is licensed under the MIT License.