Created
March 14, 2021 02:47
-
-
Save rsbohn/08365eff8f98a97f81b8060186987a5d to your computer and use it in GitHub Desktop.
Demo TCP for CircuitPython
This file contains hidden or 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
| # tcp socket recv demo | |
| # Randall Bohn 2021-03-13 | |
| import time | |
| import board | |
| import busio | |
| from digitalio import DigitalInOut, Direction | |
| from adafruit_esp32spi import adafruit_esp32spi | |
| import adafruit_esp32spi.adafruit_esp32spi_socket as socket | |
| import adafruit_requests as requests | |
| from secrets import secrets | |
| # AirLift Shield | |
| # Adafruit CircuitPython 6.1.0 on 2021-01-21; Adafruit Metro M4 Express with samd51j19 | |
| # esp32spi | |
| esp32_cs = DigitalInOut(board.D10) | |
| esp32_ready = DigitalInOut(board.D7) | |
| esp32_reset = DigitalInOut(board.D5) | |
| # connect to AP using secrets.py | |
| spi = busio.SPI(board.SCK, board.MOSI, board.MISO) | |
| esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) | |
| esp.connect(secrets) | |
| esp._debug=True | |
| socket.set_interface(esp) | |
| # your router or other HTTP server | |
| peer = "192.168.0.1" | |
| port = 80 | |
| def demo_recv(host=peer, port=port, n=1024): | |
| addr_info = socket.getaddrinfo(host, port) | |
| # get the connection particulars | |
| particulars = addr_info[0][4] | |
| print(f"connecting to {particulars[0]}:{particulars[1]}") | |
| sock = socket.socket() | |
| sock.connect(particulars) | |
| # a not very good HTTP request -- just so we can get a response | |
| sock.send("GET / HTTP1.0\r\n\r\n".encode()) | |
| response = sock.recv(n).decode() | |
| return response | |
| # print(demo_recv()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment