From 00825c8229faf5bea2cd3a1c7ae49dd8f510ff83 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Mon, 15 Mar 2021 10:45:43 -0500 Subject: [PATCH 1/3] 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 --- examples/esp32spi_tcp_client.py | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/esp32spi_tcp_client.py diff --git a/examples/esp32spi_tcp_client.py b/examples/esp32spi_tcp_client.py new file mode 100644 index 0000000..caf95a8 --- /dev/null +++ b/examples/esp32spi_tcp_client.py @@ -0,0 +1,43 @@ +# SPDX-FileCopyrightText: 2021 Adafruit Industries +# SPDX-License-Identifier: MIT + +import board +from digitalio import DigitalInOut +from adafruit_esp32spi import adafruit_esp32spi +import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_requests as requests +from secrets import secrets + + +TIMEOUT = 5 +# adjust host and port to match server +HOST = "192.168.10.179" +PORT = 5000 + +# PyPortal or similar; adjust pins as needed +spi = board.SPI() +esp32_cs = DigitalInOut(board.ESP_CS) +esp32_ready = DigitalInOut(board.ESP_BUSY) +esp32_reset = DigitalInOut(board.ESP_RESET) +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) + +# connect to wifi AP +esp.connect(secrets) + +# test for connectivity to server +print("Server ping:", esp.ping(HOST), "ms") + +# create the socket +socket.set_interface(esp) +socketaddr = socket.getaddrinfo(HOST, PORT)[0][4] +s = socket.socket() +s.settimeout(TIMEOUT) + +print("Connecting") +s.connect(socketaddr) + +print("Sending") +s.send(b'Hello, world') + +print("Receiving") +print(s.recv(128)) From 643b462868252de6dc51cba618a2b63ea9b5382c Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Mon, 15 Mar 2021 10:54:40 -0500 Subject: [PATCH 2/3] black --- examples/esp32spi_tcp_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/esp32spi_tcp_client.py b/examples/esp32spi_tcp_client.py index caf95a8..4ef79bb 100644 --- a/examples/esp32spi_tcp_client.py +++ b/examples/esp32spi_tcp_client.py @@ -37,7 +37,7 @@ s.connect(socketaddr) print("Sending") -s.send(b'Hello, world') +s.send(b"Hello, world") print("Receiving") print(s.recv(128)) From 25a3e504efa96e194155317521e6ebb38820ee45 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Mon, 15 Mar 2021 17:53:31 -0500 Subject: [PATCH 3/3] host and socket that yields a response I'm open to any other suggestions. The following is received back: `b'HTTP/1.1 403 Forbidden\r\nServer: nginx/1.10.3 (Ubuntu)\r\nDate: Mon, 15 Mar 2021 22:52:10 GMT\r\nContent-Type: text/html\r\nContent-Length: 178\r\nConnection: close\r\n\r\n'` --- examples/esp32spi_tcp_client.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/esp32spi_tcp_client.py b/examples/esp32spi_tcp_client.py index 4ef79bb..0663a52 100644 --- a/examples/esp32spi_tcp_client.py +++ b/examples/esp32spi_tcp_client.py @@ -10,11 +10,11 @@ TIMEOUT = 5 -# adjust host and port to match server -HOST = "192.168.10.179" -PORT = 5000 +# edit host and port to match server +HOST = "wifitest.adafruit.com" +PORT = 80 -# PyPortal or similar; adjust pins as needed +# PyPortal or similar; edit pins as needed spi = board.SPI() esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -37,7 +37,7 @@ s.connect(socketaddr) print("Sending") -s.send(b"Hello, world") +s.send(b"HEAD / HTTP/1.0\r\n\r\n") print("Receiving") -print(s.recv(128)) +print(s.recv(1024))