adafruit_wiznet5k

Pure-Python interface for WIZNET 5k ethernet modules.

  • Author(s): WIZnet, Arduino LLC, Bjoern Hartmann, Paul Stoffregen, Brent Rubell, Patrick Van Oosterwijck

Implementation Notes

Software and Dependencies:

class adafruit_wiznet5k.adafruit_wiznet5k.WIZNET5K(spi_bus, cs, reset=None, is_dhcp=True, mac=(222, 173, 190, 239, 254, 237), hostname=None, dhcp_timeout=30, debug=False)

Interface for WIZNET5K module. :param ~busio.SPI spi_bus: The SPI bus the Wiznet module is connected to. :param ~digitalio.DigitalInOut cs: Chip select pin. :param ~digitalio.DigitalInOut rst: Optional reset pin. :param bool is_dhcp: Whether to start DHCP automatically or not. :param list mac: The Wiznet’s MAC Address. :param str hostname: The desired hostname, with optional {} to fill in MAC. :param int dhcp_timeout: Timeout in seconds for DHCP response. :param bool debug: Enable debugging output.

chip

Returns the chip type.

detect_w5500()

Detects W5500 chip.

get_host_by_name(hostname)

Convert a hostname to a packed 4-byte IP Address. Returns a 4 bytearray.

get_socket()

Requests, allocates and returns a socket from the W5k chip. Returned socket number may not exceed max_sockets.

ifconfig

Returns the network configuration as a tuple.

ip_address

Returns the configured IP address.

“Returns if the PHY is connected.

mac_address

Returns the hardware’s MAC address.

maintain_dhcp_lease()

Maintain DHCP lease

max_sockets

Returns max number of sockets supported by chip.

pretty_ip(ip)

Converts a bytearray IP address to a dotted-quad string for printing

pretty_mac(mac)

Converts a bytearray MAC address to a dotted-quad string for printing

read(addr, callback, length=1, buffer=None)

Reads data from a register address. :param int addr: Register address.

read_udp(socket_num, length)

Read UDP socket’s remaining bytes.

remote_ip(socket_num)

Returns the IP address of the host who sent the current incoming packet. :param int socket num: Desired socket.

remote_port(socket_num)

Returns the port of the host who sent the current incoming packet.

set_dhcp(hostname=None, response_timeout=30)

Initializes the DHCP client and attempts to retrieve and set network configuration from the DHCP server. Returns 0 if DHCP configured, -1 otherwise. :param str hostname: The desired hostname, with optional {} to fill in MAC. :param int response_timeout: Time to wait for server to return packet, in seconds.

socket_accept(socket_num)

Gets the dest IP and port from an incoming connection. Returns the next socket number so listening can continue :parm int socket_num: socket number

socket_available(socket_num, sock_type=33)

Returns the amount of bytes to be read from the socket.

Parameters:
  • socket_num (int) – Desired socket to return bytes from.
  • sock_type (int) – Socket type, defaults to TCP.
socket_close(socket_num)

Closes a socket.

socket_connect(socket_num, dest, port, conn_mode=33)

Open and verify we’ve connected a socket to a dest IP address or hostname. By default, we use ‘conn_mode’= SNMR_TCP but we may also use SNMR_UDP.

socket_disconnect(socket_num)

Disconnect a TCP connection.

socket_listen(socket_num, port, conn_mode=33)

Start listening on a socket (default TCP mode). :parm int socket_num: socket number :parm int port: port to listen on :parm int conn_mode: connection mode SNMR_TCP (default) or SNMR_UDP

socket_open(socket_num, conn_mode=33)

Opens a TCP or UDP socket. By default, we use ‘conn_mode’=SNMR_TCP but we may also use SNMR_UDP.

socket_read(socket_num, length)

Reads data from a socket into a buffer. Returns buffer.

socket_status(socket_num)

Returns the socket connection status. Can be: SNSR_SOCK_CLOSED, SNSR_SOCK_INIT, SNSR_SOCK_LISTEN, SNSR_SOCK_SYNSENT, SNSR_SOCK_SYNRECV, SNSR_SYN_SOCK_ESTABLISHED, SNSR_SOCK_FIN_WAIT, SNSR_SOCK_CLOSING, SNSR_SOCK_TIME_WAIT, SNSR_SOCK_CLOSE_WAIT, SNSR_LAST_ACK, SNSR_SOCK_UDP, SNSR_SOCK_IPRAW, SNSR_SOCK_MACRAW, SNSR_SOCK_PPOE.

socket_write(socket_num, buffer, timeout=0)

Writes a bytearray to a provided socket.

sw_reset()

Performs a soft-reset on a Wiznet chip by writing to its MR register reset bit.

udp_remaining()

Returns amount of bytes remaining in a udp socket.

unpretty_ip(ip)

Converts a dotted-quad string to a bytearray IP address

write(addr, callback, data)

Write data to a register address. :param int addr: Destination address. :param int callback: Callback reference. :param int data: Data to write, as an integer. :param bytearray data: Data to write, as a bytearray.

adafruit_wiznet5k_socket

A socket compatible interface with the Wiznet5k module.

  • Author(s): ladyada, Brent Rubell, Patrick Van Oosterwijck, Adam Cummick
adafruit_wiznet5k.adafruit_wiznet5k_socket.getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0)

Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service.

adafruit_wiznet5k.adafruit_wiznet5k_socket.gethostbyname(hostname)

Translate a host name to IPv4 address format. The IPv4 address is returned as a string. :param str hostname: Desired hostname.

adafruit_wiznet5k.adafruit_wiznet5k_socket.htonl(x)

Convert 32-bit positive integers from host to network byte order.

adafruit_wiznet5k.adafruit_wiznet5k_socket.htons(x)

Convert 16-bit positive integers from host to network byte order.

adafruit_wiznet5k.adafruit_wiznet5k_socket.is_ipv4(host)

Checks if a host string is an IPv4 address. :param str host: host’s name or ip

adafruit_wiznet5k.adafruit_wiznet5k_socket.set_interface(iface)

Helper to set the global internet interface.

class adafruit_wiznet5k.adafruit_wiznet5k_socket.socket(family=3, type=33, proto=0, fileno=None, socknum=None)

A simplified implementation of the Python ‘socket’ class for connecting to a Wiznet5k module. :param int family: Socket address (and protocol) family. :param int type: Socket type.

accept()

Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.

available()

Returns how many bytes of data are available to be read from the socket.

bind(address)

Bind the socket to the listen port, if host is specified the interface will be reconfigured to that IP. :param tuple address: local socket as a (host, port) tuple.

close()

Closes the socket.

connect(address, conntype=None)

Connect to a remote socket at address. :param tuple address: Remote socket as a (host, port) tuple.

connected

Returns whether or not we are connected to the socket.

disconnect()

Disconnects a TCP socket.

getpeername()

Return the remote address to which the socket is connected.

gettimeout()

Return the timeout in seconds (float) associated with socket operations, or None if no timeout is set.

inet_aton(ip_string)

Convert an IPv4 address from dotted-quad string format. :param str ip_string: IP Address, as a dotted-quad string.

listen(backlog=None)

Listen on the port specified by bind. :param backlog: For compatibility but ignored.

readline()

Attempt to return as many bytes as we can up to but not including ‘ ‘.

recv(bufsize=0, flags=0)

Reads some bytes from the connected remote address. :param int bufsize: Maximum number of bytes to receive. :param int flags: ignored, present for compatibility.

recv_into(buf, nbytes=0, flags=0)

Reads some bytes from the connected remote address info the provided buffer. :param bytearray buf: Data buffer :param nbytes: Maximum number of bytes to receive :param int flags: ignored, present for compatibility. :returns: the number of bytes received

recvfrom(bufsize=0, flags=0)

Reads some bytes from the connected remote address. :param int bufsize: Maximum number of bytes to receive. :param int flags: ignored, present for compatibility. :returns: a tuple (bytes, address) where address is a tuple (ip, port)

recvfrom_into(buf, nbytes=0, flags=0)

Reads some bytes from the connected remote address info the provided buffer. :param bytearray buf: Data buffer :param nbytes: Maximum number of bytes to receive :param int flags: ignored, present for compatibility. :returns a tuple (nbytes, address) where address is a tuple (ip, port)

send(data)

Send data to the socket. The socket must be connected to a remote socket. :param bytearray data: Desired data to send to the socket.

sendto(data, address)

Send data to the socket. The socket must be connected to a remote socket. :param bytearray data: Desired data to send to the socket. :param tuple address: Remote socket as a (host, port) tuple.

settimeout(value)

Sets socket read timeout. :param int value: Socket read timeout, in seconds.

socknum

Returns the socket object’s socket number.

status

Returns the status of the socket

adafruit_wiznet5k_dhcp

Pure-Python implementation of Jordan Terrell’s DHCP library v0.3

  • Author(s): Jordan Terrell, Brent Rubell
class adafruit_wiznet5k.adafruit_wiznet5k_dhcp.DHCP(eth, mac_address, hostname=None, response_timeout=30, debug=False)

W5k DHCP Client implementation. :param eth: Wiznet 5k object :param list mac_address: Hardware MAC. :param str hostname: The desired hostname, with optional {} to fill in MAC. :param int response_timeout: DHCP Response timeout. :param bool debug: Enable debugging output.

maintain_dhcp_lease()

Maintain DHCP lease

parse_dhcp_response()

Parse DHCP response from DHCP server. Returns DHCP packet type.

request_dhcp_lease()

Request to renew or acquire a DHCP lease.

send_dhcp_message(state, time_elapsed, renew=False)

Assemble and send a DHCP message packet to a socket. :param int state: DHCP Message state. :param float time_elapsed: Number of seconds elapsed since DHCP process started :param bool renew: Set True for renew and rebind