Skip to main content

W5500 DHCP

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"

Class Usage

Constructor: W5500.DHCP(wiz)

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

Returns

The instance.

Example

// 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);

Class Methods

onLease(callback)

This method registers the function that will be called when an IP address is leased.

Parameters

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 Messages

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

Example

dhcp.onLease(function(error) {
    if (error) return server.error(error);
    server.log("DHCP lease obtained");
});

renewLease([timeout])

This method renews the lease or requests a new lease. When this is complete, the callback function registered with onLease() will be executed.

Parameters

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

Returns

The instance (this).

Example

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();

getIP()

This method retrieves the leased IP address.

Returns

Array — The leased IP address as four integers.

Example

local ip = dhcp.getIP();
server.log(format("IP address = %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]));

getSubnetMask()

This method retrieves the network subnet mask.

Returns

Array — The subnet mask as four integers.

Example

local subnetMask = dhcp.getSubnetMask();
server.log(format("Subnet mask = %d.%d.%d.%d", subnetMask[0], subnetMask[1], subnetMask[2], subnetMask[3]));

getRouterAddress()

This method retrieves the network gateway address.

Returns

Array — The gateway address as four integers.

Example

local router = dhcp.getRouterAddress();
server.log(format("Router IP address = %d.%d.%d.%d", router[0], router[1], router[2], router[3]));

getLeaseTime()

This method retrieves the lease duration in seconds.

Returns

Integer — the lease duration (seconds).

Example

local leaseTime = dhcp.getLeaseTime();
server.log("IP address lease time: " + leaseTime + "s");

getDNS()

This method retrieves a set of DNS entries.

Returns

Array — The DNS entries, each of which is an array of four integers.

Example

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]));
}

Release History

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

License

This library is licensed under the MIT License.