Simple test

Ensure your device works with this simple test.

examples/requests_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# adafruit_requests usage with an esp32spi_socket
 5import board
 6import busio
 7from digitalio import DigitalInOut
 8import adafruit_esp32spi.adafruit_esp32spi_socket as socket
 9from adafruit_esp32spi import adafruit_esp32spi
10import adafruit_requests as requests
11
12# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
13# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
14# source control.
15# pylint: disable=no-name-in-module,wrong-import-order
16try:
17    from secrets import secrets
18except ImportError:
19    print("WiFi secrets are kept in secrets.py, please add them there!")
20    raise
21
22# If you are using a board with pre-defined ESP32 Pins:
23esp32_cs = DigitalInOut(board.ESP_CS)
24esp32_ready = DigitalInOut(board.ESP_BUSY)
25esp32_reset = DigitalInOut(board.ESP_RESET)
26
27# If you have an externally connected ESP32:
28# esp32_cs = DigitalInOut(board.D9)
29# esp32_ready = DigitalInOut(board.D10)
30# esp32_reset = DigitalInOut(board.D5)
31
32# If you have an AirLift Featherwing or ItsyBitsy Airlift:
33# esp32_cs = DigitalInOut(board.D13)
34# esp32_ready = DigitalInOut(board.D11)
35# esp32_reset = DigitalInOut(board.D12)
36
37spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
38esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
39
40print("Connecting to AP...")
41while not esp.is_connected:
42    try:
43        esp.connect_AP(secrets["ssid"], secrets["password"])
44    except RuntimeError as e:
45        print("could not connect to AP, retrying: ", e)
46        continue
47print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
48
49# Initialize a requests object with a socket and esp32spi interface
50socket.set_interface(esp)
51requests.set_socket(socket, esp)
52
53TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
54JSON_GET_URL = "https://httpbin.org/get"
55JSON_POST_URL = "https://httpbin.org/post"
56
57print("Fetching text from %s" % TEXT_URL)
58response = requests.get(TEXT_URL)
59print("-" * 40)
60
61print("Text Response: ", response.text)
62print("-" * 40)
63response.close()
64
65print("Fetching JSON data from %s" % JSON_GET_URL)
66response = requests.get(JSON_GET_URL)
67print("-" * 40)
68
69print("JSON Response: ", response.json())
70print("-" * 40)
71response.close()
72
73data = "31F"
74print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
75response = requests.post(JSON_POST_URL, data=data)
76print("-" * 40)
77
78json_resp = response.json()
79# Parse out the 'data' key from json_resp dict.
80print("Data received from server:", json_resp["data"])
81print("-" * 40)
82response.close()
83
84json_data = {"Date": "July 25, 2019"}
85print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
86response = requests.post(JSON_POST_URL, json=json_data)
87print("-" * 40)
88
89json_resp = response.json()
90# Parse out the 'json' key from json_resp dict.
91print("JSON Data received from server:", json_resp["json"])
92print("-" * 40)
93response.close()