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
 4import os
 5import ssl
 6
 7import socketpool
 8import wifi
 9
10import adafruit_requests
11
12# Get WiFi details, ensure these are setup in settings.toml
13ssid = os.getenv("CIRCUITPY_WIFI_SSID")
14password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
15
16# Initialize WiFi Pool (There can be only 1 pool & top of script)
17radio = wifi.radio
18pool = socketpool.SocketPool(radio)
19
20print("Connecting to AP...")
21while not wifi.radio.ipv4_address:
22    try:
23        wifi.radio.connect(ssid, password)
24    except ConnectionError as e:
25        print("could not connect to AP, retrying: ", e)
26print("Connected to", str(radio.ap_info.ssid, "utf-8"), "\tRSSI:", radio.ap_info.rssi)
27
28# Initialize a requests session
29ssl_context = ssl.create_default_context()
30requests = adafruit_requests.Session(pool, ssl_context)
31
32TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
33JSON_GET_URL = "https://httpbin.org/get"
34JSON_POST_URL = "https://httpbin.org/post"
35
36print("Fetching text from %s" % TEXT_URL)
37response = requests.get(TEXT_URL)
38print("-" * 40)
39
40print("Text Response: ", response.text)
41print("-" * 40)
42response.close()
43
44print("Fetching JSON data from %s" % JSON_GET_URL)
45response = requests.get(JSON_GET_URL)
46print("-" * 40)
47
48print("JSON Response: ", response.json())
49print("-" * 40)
50response.close()
51
52data = "31F"
53print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
54response = requests.post(JSON_POST_URL, data=data)
55print("-" * 40)
56
57json_resp = response.json()
58# Parse out the 'data' key from json_resp dict.
59print("Data received from server:", json_resp["data"])
60print("-" * 40)
61response.close()
62
63json_data = {"Date": "July 25, 2019"}
64print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
65response = requests.post(JSON_POST_URL, json=json_data)
66print("-" * 40)
67
68json_resp = response.json()
69# Parse out the 'json' key from json_resp dict.
70print("JSON Data received from server:", json_resp["json"])
71print("-" * 40)
72response.close()

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)
55response = requests.get(TEXT_URL)
56print("-" * 40)
57
58print("Text Response: ", response.text)
59print("-" * 40)
60response.close()
61
62print("Fetching JSON data from %s" % JSON_GET_URL)
63response = requests.get(JSON_GET_URL)
64print("-" * 40)
65
66print("JSON Response: ", response.json())
67print("-" * 40)
68response.close()
69
70data = "31F"
71print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
72response = requests.post(JSON_POST_URL, data=data)
73print("-" * 40)
74
75json_resp = response.json()
76# Parse out the 'data' key from json_resp dict.
77print("Data received from server:", json_resp["data"])
78print("-" * 40)
79response.close()
80
81json_data = {"Date": "July 25, 2019"}
82print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
83response = requests.post(JSON_POST_URL, json=json_data)
84print("-" * 40)
85
86json_resp = response.json()
87# Parse out the 'json' key from json_resp dict.
88print("JSON Data received from server:", json_resp["json"])
89print("-" * 40)
90response.close()

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)
28response = requests.get(TEXT_URL)
29print("-" * 40)
30
31print("Text Response: ", response.text)
32print("-" * 40)
33response.close()
34
35print("Fetching JSON data from %s" % JSON_GET_URL)
36response = requests.get(JSON_GET_URL)
37print("-" * 40)
38
39print("JSON Response: ", response.json())
40print("-" * 40)
41response.close()
42
43data = "31F"
44print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
45response = requests.post(JSON_POST_URL, data=data)
46print("-" * 40)
47
48json_resp = response.json()
49# Parse out the 'data' key from json_resp dict.
50print("Data received from server:", json_resp["data"])
51print("-" * 40)
52response.close()
53
54json_data = {"Date": "July 25, 2019"}
55print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
56response = requests.post(JSON_POST_URL, json=json_data)
57print("-" * 40)
58
59json_resp = response.json()
60# Parse out the 'json' key from json_resp dict.
61print("JSON Data received from server:", json_resp["json"])
62print("-" * 40)
63response.close()

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)
56response = requests.get(TEXT_URL)
57print("-" * 40)
58
59print("Text Response: ", response.text)
60print("-" * 40)
61response.close()
62
63print("Fetching JSON data from %s" % JSON_GET_URL)
64response = requests.get(JSON_GET_URL)
65print("-" * 40)
66
67print("JSON Response: ", response.json())
68print("-" * 40)
69response.close()
70
71data = "31F"
72print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
73response = requests.post(JSON_POST_URL, data=data)
74print("-" * 40)
75
76json_resp = response.json()
77# Parse out the 'data' key from json_resp dict.
78print("Data received from server:", json_resp["data"])
79print("-" * 40)
80response.close()
81
82json_data = {"Date": "July 25, 2019"}
83print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
84response = requests.post(JSON_POST_URL, json=json_data)
85print("-" * 40)
86
87json_resp = response.json()
88# Parse out the 'json' key from json_resp dict.
89print("JSON Data received from server:", json_resp["json"])
90print("-" * 40)
91response.close()