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}")
37response = requests.get(TEXT_URL)
38print(f" | ✅ GET Response: {response.text}")
39response.close()
40print(f" | ✂️ Disconnected from {TEXT_URL}")
41print("-" * 80)
42
43print(f" | GET Full Response Test: {JSON_GET_URL}")
44response = requests.get(JSON_GET_URL)
45print(f" | ✅ Unparsed Full JSON Response: {response.json()}")
46response.close()
47print(f" | ✂️ Disconnected from {JSON_GET_URL}")
48print("-" * 80)
49
50DATA = "This is an example of a JSON value"
51print(f" | ✅ JSON 'value' POST Test: {JSON_POST_URL} {DATA}")
52response = requests.post(JSON_POST_URL, data=DATA)
53json_resp = response.json()
54# Parse out the 'data' key from json_resp dict.
55print(f" | ✅ JSON 'value' Response: {json_resp['data']}")
56response.close()
57print(f" | ✂️ Disconnected from {JSON_POST_URL}")
58print("-" * 80)
59
60json_data = {"Date": "January 1, 1970"}
61print(f" | ✅ JSON 'key':'value' POST Test: {JSON_POST_URL} {json_data}")
62response = requests.post(JSON_POST_URL, json=json_data)
63json_resp = response.json()
64# Parse out the 'json' key from json_resp dict.
65print(f" | ✅ JSON 'key':'value' Response: {json_resp['json']}")
66response.close()
67print(f" | ✂️ Disconnected from {JSON_POST_URL}")
68print("-" * 80)
69
70print("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)
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()