Skip to content

Commit 00825c8

Browse files
authored
Create esp32spi_tcp_client.py
A simple TCP client example using ESP32SPI. Requires running a TCP server listening on the same port. For example, CPython on a general-purpose computer: https://github.com/adafruit/circuitpython/blob/main/tests/circuitpython-manual/socketpool/client/host-server.py or CircuitPython native sockets: https://github.com/adafruit/circuitpython/blob/main/tests/circuitpython-manual/socketpool/server/cpy-server.py
1 parent 2870f72 commit 00825c8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

examples/esp32spi_tcp_client.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2021 Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
from digitalio import DigitalInOut
6+
from adafruit_esp32spi import adafruit_esp32spi
7+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
8+
import adafruit_requests as requests
9+
from secrets import secrets
10+
11+
12+
TIMEOUT = 5
13+
# adjust host and port to match server
14+
HOST = "192.168.10.179"
15+
PORT = 5000
16+
17+
# PyPortal or similar; adjust pins as needed
18+
spi = board.SPI()
19+
esp32_cs = DigitalInOut(board.ESP_CS)
20+
esp32_ready = DigitalInOut(board.ESP_BUSY)
21+
esp32_reset = DigitalInOut(board.ESP_RESET)
22+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
23+
24+
# connect to wifi AP
25+
esp.connect(secrets)
26+
27+
# test for connectivity to server
28+
print("Server ping:", esp.ping(HOST), "ms")
29+
30+
# create the socket
31+
socket.set_interface(esp)
32+
socketaddr = socket.getaddrinfo(HOST, PORT)[0][4]
33+
s = socket.socket()
34+
s.settimeout(TIMEOUT)
35+
36+
print("Connecting")
37+
s.connect(socketaddr)
38+
39+
print("Sending")
40+
s.send(b'Hello, world')
41+
42+
print("Receiving")
43+
print(s.recv(128))

0 commit comments

Comments
 (0)