Examples

Below are a few examples, for use with common boards. There are more in the examples folder of the library

On-board WiFi

examples/wifi/requests_wifi_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3# Updated for Circuit Python 9.0
 4""" WiFi Simpletest """
 5
 6import os
 7
 8import adafruit_connection_manager
 9import wifi
10
11import adafruit_requests
12
13# Get WiFi details, ensure these are setup in settings.toml
14ssid = os.getenv("CIRCUITPY_WIFI_SSID")
15password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
16
17TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
18JSON_GET_URL = "https://httpbin.org/get"
19JSON_POST_URL = "https://httpbin.org/post"
20
21# Initalize Wifi, Socket Pool, Request Session
22pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
23ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
24requests = adafruit_requests.Session(pool, ssl_context)
25rssi = wifi.radio.ap_info.rssi
26
27print(f"\nConnecting to {ssid}...")
28print(f"Signal Strength: {rssi}")
29try:
30    # Connect to the Wi-Fi network
31    wifi.radio.connect(ssid, password)
32except OSError as e:
33    print(f"❌ OSError: {e}")
34print("✅ Wifi!")
35
36print(f" | GET Text Test: {TEXT_URL}")
37with requests.get(TEXT_URL) as response:
38    print(f" | ✅ GET Response: {response.text}")
39print("-" * 80)
40
41print(f" | GET Full Response Test: {JSON_GET_URL}")
42with requests.get(JSON_GET_URL) as response:
43    print(f" | ✅ Unparsed Full JSON Response: {response.json()}")
44print("-" * 80)
45
46DATA = "This is an example of a JSON value"
47print(f" | ✅ JSON 'value' POST Test: {JSON_POST_URL} {DATA}")
48with requests.post(JSON_POST_URL, data=DATA) as response:
49    json_resp = response.json()
50    # Parse out the 'data' key from json_resp dict.
51    print(f" | ✅ JSON 'value' Response: {json_resp['data']}")
52print("-" * 80)
53
54json_data = {"Date": "January 1, 1970"}
55print(f" | ✅ JSON 'key':'value' POST Test: {JSON_POST_URL} {json_data}")
56with requests.post(JSON_POST_URL, json=json_data) as response:
57    json_resp = response.json()
58    # Parse out the 'json' key from json_resp dict.
59    print(f" | ✅ JSON 'key':'value' Response: {json_resp['json']}")
60print("-" * 80)
61
62print("Finished!")

ESP32SPI

examples/esp32spi/requests_esp32spi_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import os
 5
 6import adafruit_connection_manager
 7import board
 8import busio
 9from adafruit_esp32spi import adafruit_esp32spi
10from digitalio import DigitalInOut
11
12import adafruit_requests
13
14# Get WiFi details, ensure these are setup in settings.toml
15ssid = os.getenv("CIRCUITPY_WIFI_SSID")
16password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
17
18# If you are using a board with pre-defined ESP32 Pins:
19esp32_cs = DigitalInOut(board.ESP_CS)
20esp32_ready = DigitalInOut(board.ESP_BUSY)
21esp32_reset = DigitalInOut(board.ESP_RESET)
22
23# If you have an externally connected ESP32:
24# esp32_cs = DigitalInOut(board.D9)
25# esp32_ready = DigitalInOut(board.D10)
26# esp32_reset = DigitalInOut(board.D5)
27
28# If you have an AirLift Featherwing or ItsyBitsy Airlift:
29# esp32_cs = DigitalInOut(board.D13)
30# esp32_ready = DigitalInOut(board.D11)
31# esp32_reset = DigitalInOut(board.D12)
32
33spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
34radio = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
35
36print("Connecting to AP...")
37while not radio.is_connected:
38    try:
39        radio.connect_AP(ssid, password)
40    except RuntimeError as e:
41        print("could not connect to AP, retrying: ", e)
42        continue
43print("Connected to", str(radio.ssid, "utf-8"), "\tRSSI:", radio.rssi)
44
45# Initialize a requests session
46pool = adafruit_connection_manager.get_radio_socketpool(radio)
47ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
48requests = adafruit_requests.Session(pool, ssl_context)
49
50TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
51JSON_GET_URL = "https://httpbin.org/get"
52JSON_POST_URL = "https://httpbin.org/post"
53
54print("Fetching text from %s" % TEXT_URL)
55with requests.get(TEXT_URL) as response:
56    print("-" * 40)
57    print("Text Response: ", response.text)
58    print("-" * 40)
59
60print("Fetching JSON data from %s" % JSON_GET_URL)
61with requests.get(JSON_GET_URL) as response:
62    print("-" * 40)
63    print("JSON Response: ", response.json())
64    print("-" * 40)
65
66data = "31F"
67print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
68with requests.post(JSON_POST_URL, data=data) as response:
69    print("-" * 40)
70    json_resp = response.json()
71    # Parse out the 'data' key from json_resp dict.
72    print("Data received from server:", json_resp["data"])
73    print("-" * 40)
74
75json_data = {"Date": "July 25, 2019"}
76print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
77with requests.post(JSON_POST_URL, json=json_data) as response:
78    print("-" * 40)
79    json_resp = response.json()
80    # Parse out the 'json' key from json_resp dict.
81    print("JSON Data received from server:", json_resp["json"])
82    print("-" * 40)

WIZNET5K

examples/wiznet5k/requests_wiznet5k_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import adafruit_connection_manager
 5import board
 6import busio
 7from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
 8from digitalio import DigitalInOut
 9
10import adafruit_requests
11
12cs = DigitalInOut(board.D10)
13spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
14
15# Initialize ethernet interface with DHCP
16radio = WIZNET5K(spi_bus, cs)
17
18# Initialize a requests session
19pool = adafruit_connection_manager.get_radio_socketpool(radio)
20ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
21requests = adafruit_requests.Session(pool, ssl_context)
22
23TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
24JSON_GET_URL = "http://httpbin.org/get"
25JSON_POST_URL = "http://httpbin.org/post"
26
27print("Fetching text from %s" % TEXT_URL)
28with requests.get(TEXT_URL) as response:
29    print("-" * 40)
30    print("Text Response: ", response.text)
31    print("-" * 40)
32
33print("Fetching JSON data from %s" % JSON_GET_URL)
34with requests.get(JSON_GET_URL) as response:
35    print("-" * 40)
36    print("JSON Response: ", response.json())
37    print("-" * 40)
38
39data = "31F"
40print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
41with requests.post(JSON_POST_URL, data=data) as response:
42    print("-" * 40)
43    json_resp = response.json()
44    # Parse out the 'data' key from json_resp dict.
45    print("Data received from server:", json_resp["data"])
46    print("-" * 40)
47
48json_data = {"Date": "July 25, 2019"}
49print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
50with requests.post(JSON_POST_URL, json=json_data) as response:
51    print("-" * 40)
52    json_resp = response.json()
53    # Parse out the 'json' key from json_resp dict.
54    print("JSON Data received from server:", json_resp["json"])
55    print("-" * 40)

Fona

examples/fona/requests_fona_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import os
 5import time
 6
 7import adafruit_connection_manager
 8import adafruit_fona.adafruit_fona_network as network
 9import adafruit_fona.adafruit_fona_socket as pool
10import board
11import busio
12import digitalio
13from adafruit_fona.adafruit_fona import FONA  # pylint: disable=unused-import
14from adafruit_fona.fona_3g import FONA3G  # pylint: disable=unused-import
15
16import adafruit_requests
17
18# Get GPRS details, ensure these are setup in settings.toml
19apn = os.getenv("APN")
20apn_username = os.getenv("APN_USERNAME")
21apn_password = os.getenv("APN_PASSWORD")
22
23# Create a serial connection for the FONA connection
24uart = busio.UART(board.TX, board.RX)
25rst = digitalio.DigitalInOut(board.D4)
26
27# Use this for FONA800 and FONA808
28radio = FONA(uart, rst)
29
30# Use this for FONA3G
31# radio = FONA3G(uart, rst)
32
33# Initialize cellular data network
34network = network.CELLULAR(radio, (apn, apn_username, apn_password))
35
36while not network.is_attached:
37    print("Attaching to network...")
38    time.sleep(0.5)
39print("Attached!")
40
41while not network.is_connected:
42    print("Connecting to network...")
43    network.connect()
44    time.sleep(0.5)
45print("Network Connected!")
46
47# Initialize a requests session
48ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, radio)
49requests = adafruit_requests.Session(pool, ssl_context)
50
51TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
52JSON_GET_URL = "http://httpbin.org/get"
53JSON_POST_URL = "http://httpbin.org/post"
54
55print("Fetching text from %s" % TEXT_URL)
56with requests.get(TEXT_URL) as response:
57    print("-" * 40)
58    print("Text Response: ", response.text)
59    print("-" * 40)
60
61print("Fetching JSON data from %s" % JSON_GET_URL)
62with requests.get(JSON_GET_URL) as response:
63    print("-" * 40)
64    print("JSON Response: ", response.json())
65    print("-" * 40)
66
67data = "31F"
68print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
69with requests.post(JSON_POST_URL, data=data) as response:
70    print("-" * 40)
71    json_resp = response.json()
72    # Parse out the 'data' key from json_resp dict.
73    print("Data received from server:", json_resp["data"])
74    print("-" * 40)
75
76json_data = {"Date": "July 25, 2019"}
77print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
78with requests.post(JSON_POST_URL, json=json_data) as response:
79    print("-" * 40)
80    json_resp = response.json()
81    # Parse out the 'json' key from json_resp dict.
82    print("JSON Data received from server:", json_resp["json"])
83    print("-" * 40)