Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Last active February 22, 2020 16:02
Show Gist options
  • Save FoamyGuy/6e71cc54e2ae40fe56902effe7cc97b1 to your computer and use it in GitHub Desktop.
Save FoamyGuy/6e71cc54e2ae40fe56902effe7cc97b1 to your computer and use it in GitHub Desktop.
# adafruit_requests usage with an esp32spi_socket
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
from secrets import secrets
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets['ssid'], secrets['password'])
except RuntimeError as e:
print("could not connect to AP, retrying: ",e)
continue
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)
URL = "https://docs.google.com/forms/u/0/d/e/1FAIpQLScoUhFA1BIJjXF2-QM_mxNMN62Y2BCJ--gwJIwWcwJPvWDlwQ/formResponse"
form_data = {
"entry.2004206042": "Hello",
"entry.487209416": "Blinka"
}
post_data = ""
for entry in form_data.keys():
post_data = "{}&{}={}".format(post_data, entry, form_data[entry])
post_data = post_data[1:]
print(post_data)
response = requests.post(URL, data=post_data, content_type="application/x-www-form-urlencoded")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment