Skip to content

Commit 4f98554

Browse files
committed
Standardize simple and advanced examples
1 parent 3d3e303 commit 4f98554

12 files changed

+169
-291
lines changed
Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
# pylint: disable=unused-import
4+
import os
55
import time
66

7+
import adafruit_connection_manager
78
import adafruit_fona.adafruit_fona_network as network
8-
import adafruit_fona.adafruit_fona_socket as cellular_socket
9+
import adafruit_fona.adafruit_fona_socket as pool
910
import board
1011
import busio
1112
import digitalio
12-
from adafruit_fona.adafruit_fona import FONA
13-
from adafruit_fona.fona_3g import FONA3G
13+
from adafruit_fona.adafruit_fona import FONA # pylint: disable=unused-import
14+
from adafruit_fona.fona_3g import FONA3G # pylint: disable=unused-import
1415

15-
import adafruit_requests as requests
16+
import adafruit_requests
1617

17-
# Get GPRS details and more from a secrets.py file
18-
try:
19-
from secrets import secrets
20-
except ImportError:
21-
print("GPRS secrets are kept in secrets.py, please add them there!")
22-
raise
18+
# Get GPRS details, ensure these are setup in settings.toml
19+
apn = os.getenv("APN")
20+
apn_username = os.getenv("APN_USERNAME")
21+
apn_password = os.getenv("APN_PASSWORD")
2322

2423
# Create a serial connection for the FONA connection
2524
uart = busio.UART(board.TX, board.RX)
26-
rst = digitalio.DigitalInOut(board.D9)
25+
rst = digitalio.DigitalInOut(board.D4)
2726

2827
# Use this for FONA800 and FONA808
29-
# fona = FONA(uart, rst)
28+
radio = FONA(uart, rst)
3029

3130
# Use this for FONA3G
32-
fona = FONA3G(uart, rst)
31+
# radio = FONA3G(uart, rst)
3332

3433
# Initialize cellular data network
35-
network = network.CELLULAR(
36-
fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])
37-
)
34+
network = network.CELLULAR(radio, (apn, apn_username, apn_password))
3835

3936
while not network.is_attached:
4037
print("Attaching to network...")
@@ -47,8 +44,9 @@
4744
time.sleep(0.5)
4845
print("Network Connected!")
4946

50-
# Initialize a requests object with a socket and cellular interface
51-
requests.set_socket(cellular_socket, fona)
47+
# Initialize a requests session
48+
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, radio)
49+
requests = adafruit_requests.Session(pool, ssl_context)
5250

5351
JSON_GET_URL = "http://httpbin.org/get"
5452

@@ -68,8 +66,5 @@
6866
print("Response HTTP Status Code: ", response.status_code)
6967
print("-" * 60)
7068

71-
# Read Response, as raw bytes instead of pretty text
72-
print("Raw Response: ", response.content)
73-
7469
# Close, delete and collect the response data
7570
response.close()

examples/requests_cellular_simpletest.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
# pylint: disable=unused-import
4+
import os
55
import time
66

7+
import adafruit_connection_manager
78
import adafruit_fona.adafruit_fona_network as network
8-
import adafruit_fona.adafruit_fona_socket as cellular_socket
9+
import adafruit_fona.adafruit_fona_socket as pool
910
import board
1011
import busio
1112
import digitalio
12-
from adafruit_fona.adafruit_fona import FONA
13-
from adafruit_fona.fona_3g import FONA3G
13+
from adafruit_fona.adafruit_fona import FONA # pylint: disable=unused-import
14+
from adafruit_fona.fona_3g import FONA3G # pylint: disable=unused-import
1415

15-
import adafruit_requests as requests
16+
import adafruit_requests
1617

17-
# Get GPRS details and more from a secrets.py file
18-
try:
19-
from secrets import secrets
20-
except ImportError:
21-
print("GPRS secrets are kept in secrets.py, please add them there!")
22-
raise
18+
# Get GPRS details, ensure these are setup in settings.toml
19+
apn = os.getenv("APN")
20+
apn_username = os.getenv("APN_USERNAME")
21+
apn_password = os.getenv("APN_PASSWORD")
2322

2423
# Create a serial connection for the FONA connection
2524
uart = busio.UART(board.TX, board.RX)
2625
rst = digitalio.DigitalInOut(board.D4)
2726

2827
# Use this for FONA800 and FONA808
29-
fona = FONA(uart, rst)
28+
radio = FONA(uart, rst)
3029

3130
# Use this for FONA3G
32-
# fona = FONA3G(uart, rst)
31+
# radio = FONA3G(uart, rst)
3332

3433
# Initialize cellular data network
35-
network = network.CELLULAR(
36-
fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])
37-
)
34+
network = network.CELLULAR(radio, (apn, apn_username, apn_password))
3835

3936
while not network.is_attached:
4037
print("Attaching to network...")
@@ -47,8 +44,9 @@
4744
time.sleep(0.5)
4845
print("Network Connected!")
4946

50-
# Initialize a requests object with a socket and cellular interface
51-
requests.set_socket(cellular_socket, fona)
47+
# Initialize a requests session
48+
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, radio)
49+
requests = adafruit_requests.Session(pool, ssl_context)
5250

5351
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
5452
JSON_GET_URL = "http://httpbin.org/get"
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import socket
4+
import socket as pool
5+
import ssl
56

67
import adafruit_requests
78

8-
http = adafruit_requests.Session(socket)
9+
# Initialize a requests session
10+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
911

10-
JSON_GET_URL = "http://httpbin.org/get"
12+
JSON_GET_URL = "https://httpbin.org/get"
1113

1214
# Define a custom header as a dict.
1315
headers = {"user-agent": "blinka/1.0.0"}
1416

1517
print("Fetching JSON data from %s..." % JSON_GET_URL)
16-
response = http.get(JSON_GET_URL, headers=headers)
18+
response = requests.get(JSON_GET_URL, headers=headers)
1719
print("-" * 60)
1820

1921
json_data = response.json()
@@ -25,8 +27,5 @@
2527
print("Response HTTP Status Code: ", response.status_code)
2628
print("-" * 60)
2729

28-
# Read Response, as raw bytes instead of pretty text
29-
print("Raw Response: ", response.content)
30-
3130
# Close, delete and collect the response data
3231
response.close()

examples/requests_cpython_github.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/requests_cpython_https.py

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
# adafruit_requests usage with a CPython socket
5-
import socket
4+
import socket as pool
5+
import ssl
66

77
import adafruit_requests
88

9-
http = adafruit_requests.Session(socket)
9+
# Initialize a requests session
10+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
1011

1112
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
12-
JSON_GET_URL = "http://httpbin.org/get"
13-
JSON_POST_URL = "http://httpbin.org/post"
14-
REDIRECT_URL = "http://httpbingo.org/redirect/1"
15-
RELATIVE_REDIRECT_URL = "http://httpbingo.org/relative-redirect/1"
16-
ABSOLUTE_REDIRECT_URL = "http://httpbingo.org/absolute-redirect/1"
13+
JSON_GET_URL = "https://httpbin.org/get"
14+
JSON_POST_URL = "https://httpbin.org/post"
1715

1816
print("Fetching text from %s" % TEXT_URL)
19-
response = http.get(TEXT_URL)
17+
response = requests.get(TEXT_URL)
2018
print("-" * 40)
2119

2220
print("Text Response: ", response.text)
2321
print("-" * 40)
22+
response.close()
2423

2524
print("Fetching JSON data from %s" % JSON_GET_URL)
26-
response = http.get(JSON_GET_URL)
25+
response = requests.get(JSON_GET_URL)
2726
print("-" * 40)
2827

2928
print("JSON Response: ", response.json())
@@ -32,43 +31,22 @@
3231

3332
data = "31F"
3433
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
35-
response = http.post(JSON_POST_URL, data=data)
34+
response = requests.post(JSON_POST_URL, data=data)
3635
print("-" * 40)
3736

3837
json_resp = response.json()
3938
# Parse out the 'data' key from json_resp dict.
4039
print("Data received from server:", json_resp["data"])
4140
print("-" * 40)
41+
response.close()
4242

4343
json_data = {"Date": "July 25, 2019"}
4444
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
45-
response = http.post(JSON_POST_URL, json=json_data)
45+
response = requests.post(JSON_POST_URL, json=json_data)
4646
print("-" * 40)
4747

4848
json_resp = response.json()
4949
# Parse out the 'json' key from json_resp dict.
5050
print("JSON Data received from server:", json_resp["json"])
5151
print("-" * 40)
52-
53-
print("Fetching JSON data from redirect url %s" % REDIRECT_URL)
54-
response = http.get(REDIRECT_URL)
55-
print("-" * 40)
56-
57-
print("JSON Response: ", response.json())
58-
print("-" * 40)
59-
60-
print("Fetching JSON data from relative redirect url %s" % RELATIVE_REDIRECT_URL)
61-
response = http.get(RELATIVE_REDIRECT_URL)
62-
print("-" * 40)
63-
64-
print("JSON Response: ", response.json())
65-
print("-" * 40)
66-
67-
print("Fetching JSON data from aboslute redirect url %s" % ABSOLUTE_REDIRECT_URL)
68-
response = http.get(ABSOLUTE_REDIRECT_URL)
69-
print("-" * 40)
70-
71-
print("JSON Response: ", response.json())
72-
print("-" * 40)
73-
7452
response.close()

examples/requests_esp32spi_advanced.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
4+
import os
5+
6+
import adafruit_connection_manager
7+
import adafruit_esp32spi.adafruit_esp32spi_socket as pool
58
import board
69
import busio
710
from adafruit_esp32spi import adafruit_esp32spi
811
from digitalio import DigitalInOut
912

10-
import adafruit_requests as requests
13+
import adafruit_requests
1114

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
16-
try:
17-
from secrets import secrets
18-
except ImportError:
19-
print("WiFi secrets are kept in secrets.py, please add them there!")
20-
raise
15+
# Get WiFi details, ensure these are setup in settings.toml
16+
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
17+
appw = os.getenv("CIRCUITPY_WIFI_PASSWORD")
2118

2219
# If you are using a board with pre-defined ESP32 Pins:
2320
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -29,23 +26,28 @@
2926
# esp32_ready = DigitalInOut(board.D10)
3027
# esp32_reset = DigitalInOut(board.D5)
3128

29+
# If you have an AirLift Featherwing or ItsyBitsy Airlift:
30+
# esp32_cs = DigitalInOut(board.D13)
31+
# esp32_ready = DigitalInOut(board.D11)
32+
# esp32_reset = DigitalInOut(board.D12)
33+
3234
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
33-
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
35+
radio = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3436

3537
print("Connecting to AP...")
36-
while not esp.is_connected:
38+
while not radio.is_connected:
3739
try:
38-
esp.connect_AP(secrets["ssid"], secrets["password"])
40+
radio.connect_AP(ssid, appw)
3941
except RuntimeError as e:
4042
print("could not connect to AP, retrying: ", e)
4143
continue
42-
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
44+
print("Connected to", str(radio.ssid, "utf-8"), "\tRSSI:", radio.rssi)
4345

44-
# Initialize a requests object with a socket and esp32spi interface
45-
socket.set_interface(esp)
46-
requests.set_socket(socket, esp)
46+
# Initialize a requests session
47+
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, radio)
48+
requests = adafruit_requests.Session(pool, ssl_context)
4749

48-
JSON_GET_URL = "http://httpbin.org/get"
50+
JSON_GET_URL = "https://httpbin.org/get"
4951

5052
# Define a custom header as a dict.
5153
headers = {"user-agent": "blinka/1.0.0"}

0 commit comments

Comments
 (0)