Skip to content

Commit

Permalink
Merge pull request #7 from brentru/update-for-requests
Browse files Browse the repository at this point in the history
Add Adafruit_CircuitPython_Requests Compatibility
  • Loading branch information
brentru authored Mar 6, 2020
2 parents 1b289b3 + f510eb2 commit 5cd1e5b
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 181 deletions.
65 changes: 35 additions & 30 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,45 +57,50 @@ wifitest.adafruit.com.

.. code-block:: python
import time
import board
import busio
import digitalio
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET
import adafruit_requests as requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
print("Wiznet5k WebClient Test")
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
cs = digitalio.DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Initialize ethernet interface with DHCP
eth = WIZNET5K(spi_bus, cs, debug=True)
print("DHCP Assigned IP: ", eth.pretty_ip(eth.ip_address))
socket.set_interface(eth)
host = 'wifitest.adafruit.com'
port = 80
addr_info = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
sock = socket.socket(addr_info[0], addr_info[1], addr_info[2])
print("Connected to ", sock.getpeername())
# Make a HTTP Request
sock.send(b"GET /testwifi/index.html HTTP/1.1\n")
sock.send(b"Host: 104.236.193.178\n")
sock.send(b"Connection: close\n\n")
bytes_avail = 0
while not bytes_avail:
bytes_avail = sock.available()
if bytes_avail > 0:
data = sock.recv(bytes_avail)
print(data)
break
time.sleep(0.05)
eth = WIZNET5K(spi_bus, cs)
# Initialize a requests object with a socket and ethernet interface
requests.set_socket(socket, eth)
print("Chip Version:", eth.chip)
print("MAC Address:", [hex(i) for i in eth.mac_address])
print("My IP address is:", eth.pretty_ip(eth.ip_address))
print("IP lookup adafruit.com: %s" %eth.pretty_ip(eth.get_host_by_name("adafruit.com")))
#eth._debug = True
print("Fetching text from", TEXT_URL)
r = requests.get(TEXT_URL)
print('-'*40)
print(r.text)
print('-'*40)
r.close()
print()
print("Fetching json from", JSON_URL)
r = requests.get(JSON_URL)
print('-'*40)
print(r.json())
print('-'*40)
r.close()
print("Done!")
Contributing
============
Expand Down
93 changes: 48 additions & 45 deletions adafruit_wiznet5k/adafruit_wiznet5k.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""

from random import randint
import time
from micropython import const

Expand Down Expand Up @@ -138,7 +138,7 @@
'remote_ip': 0,
'remote_port': 0}

class WIZNET5K:
class WIZNET5K: # pylint: disable=too-many-public-methods
"""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.
Expand All @@ -149,7 +149,11 @@ class WIZNET5K:
"""

# pylint: disable=too-many-arguments, too-many-public-methods
TCP_MODE = const(0x21)
UDP_MODE = const(0x02)
TLS_MODE = const(0x03) # This is NOT currently implemented

# pylint: disable=too-many-arguments
def __init__(self, spi_bus, cs, reset=None,
is_dhcp=True, mac=DEFAULT_MAC, debug=False):
self._debug = debug
Expand Down Expand Up @@ -180,7 +184,8 @@ def __init__(self, spi_bus, cs, reset=None,
self._dns = 0
# Set DHCP
if is_dhcp:
self.set_dhcp()
ret = self.set_dhcp()
assert ret == 0, "Failed to configure DHCP Server!"

def set_dhcp(self, response_timeout=1):
"""Initializes the DHCP client and attempts to retrieve
Expand All @@ -193,11 +198,9 @@ def set_dhcp(self, response_timeout=1):
print("* Initializing DHCP")
self._src_port = 68
# Return IP assigned by DHCP
_dhcp_client = dhcp.DHCP(self, self.mac_address, response_timeout)
_dhcp_client = dhcp.DHCP(self, self.mac_address, response_timeout, debug=self._debug)
ret = _dhcp_client.request_dhcp_lease()
if ret == 1:
if self._debug:
print("* Found DHCP server - setting configuration...")
_ip = (_dhcp_client.local_ip[0], _dhcp_client.local_ip[1],
_dhcp_client.local_ip[2], _dhcp_client.local_ip[3])

Expand All @@ -210,9 +213,15 @@ def set_dhcp(self, response_timeout=1):
self._dns = (_dhcp_client.dns_server_ip[0], _dhcp_client.dns_server_ip[1],
_dhcp_client.dns_server_ip[2], _dhcp_client.dns_server_ip[3])
self.ifconfig = ((_ip, _subnet_mask, _gw_addr, self._dns))
if self._debug:
print("* Found DHCP Server:")
print("IP: {}\nSubnet Mask: {}\nGW Addr: {}\nDNS Server: {}".format(_ip,
_subnet_mask,
_gw_addr,
self._dns))
self._src_port = 0
return 0
self._src_port = 0
return 1
return -1

def get_host_by_name(self, hostname):
"""Convert a hostname to a packed 4-byte IP Address.
Expand All @@ -224,8 +233,11 @@ def get_host_by_name(self, hostname):
hostname = bytes(hostname, 'utf-8')
self._src_port = int(time.monotonic())
# Return IP assigned by DHCP
_dns_client = dns.DNS(self, self._dns)
_dns_client = dns.DNS(self, self._dns, debug=self._debug)
ret = _dns_client.gethostbyname(hostname)
if self._debug:
print("* Resolved IP: ", ret)
assert ret != -1, "Failed to resolve hostname!"
self._src_port = 0
return ret

Expand Down Expand Up @@ -303,15 +315,8 @@ def remote_port(self):
@property
def ifconfig(self):
"""Returns the network configuration as a tuple."""
# set subnet and gateway addresses
self._pbuff = bytearray(8)
for octet in range(0, 4):
self._pbuff += self.read(REG_SUBR+octet, 0x04)
for octet in range(0, 4):
self._pbuff += self.read(REG_GAR+octet, 0x04)

params = (self.ip_address, self._pbuff[0:3], self._pbuff[3:7], self._dns)
return params
return (self.ip_address, self.read(REG_SUBR, 0x00, 4),
self.read(REG_GAR, 0x00, 4), self._dns)

@ifconfig.setter
def ifconfig(self, params):
Expand All @@ -321,14 +326,10 @@ def ifconfig(self, params):
"""
ip_address, subnet_mask, gateway_address, dns_server = params

# Set IP Address
self.write(REG_SIPR, 0x04, ip_address)
self.write(REG_SUBR, 0x04, subnet_mask)
self.write(REG_GAR, 0x04, gateway_address)

# set subnet and gateway addresses
for octet in range(0, 4):
self.write(REG_SUBR+octet, 0x04, subnet_mask[octet])
self.write(REG_GAR+octet, 0x04, gateway_address[octet])
# set dns server address
self._dns = dns_server

def _w5100_init(self):
Expand Down Expand Up @@ -492,26 +493,36 @@ def socket_connect(self, socket_num, dest, port, conn_mode=SNMR_TCP):
"""
assert self.link_status, "Ethernet cable disconnected!"
if self._debug:
print("*** Connecting: Socket# {}, conn_mode: {}".format(socket_num, conn_mode))

print("* w5k socket connect, protocol={}, port={}, ip={}".format(conn_mode, port,
self.pretty_ip(dest)))
# initialize a socket and set the mode
res = self.socket_open(socket_num, dest, port, conn_mode=conn_mode)
res = self.socket_open(socket_num, conn_mode=conn_mode)
if res == 1:
raise RuntimeError('Failed to initalize a connection with the socket.')

# set socket destination IP and port
self._write_sndipr(socket_num, dest)
self._write_sndport(socket_num, port)
self._send_socket_cmd(socket_num, CMD_SOCK_CONNECT)

if conn_mode == SNMR_TCP:
# TCP client - connect socket
self._write_sncr(socket_num, CMD_SOCK_CONNECT)
self._read_sncr(socket_num)
# wait for tcp connection establishment
while self.socket_status(socket_num)[0] != SNSR_SOCK_ESTABLISHED:
time.sleep(0.001)
if self._debug:
print("SN_SR:", self.socket_status(socket_num)[0])
if self.socket_status(socket_num)[0] == SNSR_SOCK_CLOSED:
raise RuntimeError('Failed to establish connection.')
time.sleep(1)
elif conn_mode == SNMR_UDP:
UDP_SOCK['bytes_remaining'] = 0
return 1

def _send_socket_cmd(self, socket, cmd):
self._write_sncr(socket, cmd)
while self._read_sncr(socket) != b'\x00':
if self._debug:
print("waiting for sncr to clear...")

def get_socket(self, sockets):
"""Requests, allocates and returns a socket from the W5k
chip. Returned socket number may not exceed max_sockets.
Expand All @@ -528,23 +539,20 @@ def get_socket(self, sockets):
sock = _sock
break

if self._src_port == 0:
self._src_port = 1024

if self._debug:
print("Allocated socket #{}:{}".format(sock, self._src_port))
print("Allocated socket #{}".format(sock))
return sock

def socket_open(self, socket_num, dest, port, conn_mode=SNMR_TCP):
"""Opens a socket to a destination IP address or hostname. By default, we use
def socket_open(self, socket_num, conn_mode=SNMR_TCP):
"""Opens a TCP or UDP socket. By default, we use
'conn_mode'=SNMR_TCP but we may also use SNMR_UDP.
"""
assert self.link_status, "Ethernet cable disconnected!"
if self._debug:
print("*** Opening socket %d"%socket_num)
if self._read_snsr(socket_num)[0] == SNSR_SOCK_CLOSED:
if self._debug:
print("w5k socket begin, protocol={}, port={}".format(conn_mode, port))
print("* Opening W5k Socket, protocol={}".format(conn_mode))
time.sleep(0.00025)

self._write_snmr(socket_num, conn_mode)
Expand All @@ -554,12 +562,7 @@ def socket_open(self, socket_num, dest, port, conn_mode=SNMR_TCP):
# write to socket source port
self._write_sock_port(socket_num, self._src_port)
else:
# if source port is not set, set the local port number
self._write_sock_port(socket_num, LOCAL_PORT)

# set socket destination IP and port
self._write_sndipr(socket_num, dest)
self._write_sndport(socket_num, port)
self._write_sock_port(socket_num, randint(49152, 65535))

# open socket
self._write_sncr(socket_num, CMD_SOCK_OPEN)
Expand Down
Loading

0 comments on commit 5cd1e5b

Please sign in to comment.