Last active
May 18, 2024 14:16
-
-
Save anecdata/b959170c069cd8936b16bb0be55d4ed2 to your computer and use it in GitHub Desktop.
CircuitPython Static IP WIZnet TCP Server & Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import traceback | |
import board | |
import digitalio | |
import adafruit_connection_manager | |
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K | |
time.sleep(3) # wait for serial | |
# WIZnet W5500-EVB-Pico | |
spi = board.SPI() | |
cs = digitalio.DigitalInOut(board.GP17) | |
rst = digitalio.DigitalInOut(board.GP20) | |
radio = WIZNET5K(spi, cs, reset=rst, mac='de:ad:be:ef:fe:dd', is_dhcp=False, debug=False) | |
ipv4_address = radio.pretty_ip(radio.ip_address) | |
pool = adafruit_connection_manager.get_radio_socketpool(radio) | |
ip_address: str = "172.16.1.5" | |
subnet_mask: str = "255.255.255.0" | |
gateway_address: str = "10.0.0.1" # does not exist | |
dns_server: str = "10.0.0.1" # does not exist | |
radio.ifconfig = tuple(pool.inet_aton(num) for num in (ip_address, subnet_mask, gateway_address, dns_server)) | |
HOST = "" | |
PORT = 5000 | |
TIMEOUT = None | |
MAXBUF = 256 | |
print("Create TCP Server Socket") | |
with pool.socket(pool.AF_INET, pool.SOCK_STREAM) as s: | |
s.settimeout(TIMEOUT) | |
s.bind((HOST, PORT)) | |
s.listen() | |
print(f"Listening on {HOST}, {PORT}") | |
buf = bytearray(MAXBUF) | |
while True: | |
print("Accepting connections") | |
try: | |
conn, addr = s.accept() | |
conn.settimeout(TIMEOUT) | |
print("Accepted from", addr) | |
size = conn.recv_into(buf, MAXBUF) | |
print("Received", buf[:size], size, "bytes") | |
conn.send(buf[:size]) | |
print("Sent", buf[:size], size, "bytes") | |
conn.close() | |
except Exception as ex: | |
traceback.print_exception(ex, ex, ex.__traceback__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Server output:
Client output: