Skip to content

Add Adafruit_CircuitPython_Requests Compatibility #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 41 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
eae6025
add some updates for Adafruit_Requests
Mar 3, 2020
af4a485
return buffer and firstline properly
Mar 3, 2020
3b0a686
only add to buffer if bytes are avail on the socket
Mar 3, 2020
2da4616
fix incorrect read within dns, add correct join in socket
Mar 3, 2020
b0a1cdc
add query count
Mar 3, 2020
28d1c7b
parse based on answer instead of fixed buff size lazy parsing
Mar 3, 2020
d17a9ff
lintin
Mar 4, 2020
be5eac4
rebuild docx!
Mar 4, 2020
29eb7b5
ESP32SPI_Simpletest-style-test
Mar 4, 2020
bc82d38
correct parse, TODO: guards
Mar 4, 2020
32f18e0
lintin
Mar 4, 2020
4ca248d
add checks for types
Mar 4, 2020
ea7f98d
modify readme for new example, spnx test buildin docs
Mar 4, 2020
39385ad
throw assertionerror if dns was failed to resolve
Mar 4, 2020
5d96a2d
add resolved ip for debugging
Mar 4, 2020
42a2faf
debugging for DHCP server
Mar 4, 2020
6dc07ae
add debug iface to DNS
Mar 4, 2020
4580e6d
add way more verbose debugging within dns
Mar 4, 2020
d65fc58
trying a different approach for parsing out the query name and valida…
Mar 4, 2020
2043d7d
Better parsing for answer
Mar 4, 2020
08bb7c9
remove gc
Mar 4, 2020
cb352dd
add ip to socket begin
Mar 4, 2020
9ec9ed0
add debug to dhcp
Mar 4, 2020
ec49329
add old params
Mar 4, 2020
e0b3d23
remove cruft, new impl
Mar 4, 2020
7d7ef0e
run buf til pointer is at end
Mar 5, 2020
7db5a2c
add more verbose output for parsing DHCP requests if debug
Mar 5, 2020
a028c34
if initialized, dhcp failing to configure should Raise. Handle invali…
Mar 5, 2020
30c9f0a
break on padding opt
Mar 5, 2020
088421e
add TLS_mode conntype, assertion for connecting to HTTPS
Mar 5, 2020
406c7fb
use dynamic port numbers 41952-65535, randomize to prevent reuse!
Mar 5, 2020
4467ffe
switch to wait for sncr to clear?
Mar 5, 2020
1eb1a51
wait less time between checking sn_sr
Mar 5, 2020
ebf2d0e
read and write to/from subnet registers better in ifconfig setter/getter
Mar 5, 2020
7df14f1
testing port configuration tweaks
Mar 5, 2020
74e6d7b
prune extra from get_socket, just allocate a socket from the chipset
Mar 5, 2020
4030144
continue separating functionality: socket_open should only issue OPEN…
Mar 5, 2020
dab3551
fix indentation for lintin
Mar 5, 2020
55be280
add Adafruit IO example!
Mar 6, 2020
30569bd
add example for manually setting the network configuration
Mar 6, 2020
f510eb2
fix newline
Mar 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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