Skip to content

Commit dbb0357

Browse files
committed
Modify examples using settings.toml
1 parent e778633 commit dbb0357

13 files changed

+255
-93
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def __init__(
4242
"""
4343
:param ESP_SPIcontrol esp: The ESP object we are using
4444
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
45+
The use of secrets.py to populate the secrets dict is depreciated
46+
in favor of using settings.toml.
4547
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
4648
or RGB LED (default=None). The status LED, if given, turns red when
4749
attempting to connect to a Wi-Fi network or create an access point,
@@ -65,18 +67,10 @@ def __init__(
6567
self._ap_index = 0
6668

6769
# Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
68-
if secrets.get("ent_ssid"):
69-
self.ent_ssid = secrets["ent_ssid"]
70-
else:
71-
self.ent_ssid = secrets["ssid"]
72-
if secrets.get("ent_ident"):
73-
self.ent_ident = secrets["ent_ident"]
74-
else:
75-
self.ent_ident = ""
76-
if secrets.get("ent_user"):
77-
self.ent_user = secrets["ent_user"]
78-
if secrets.get("ent_password"):
79-
self.ent_password = secrets["ent_password"]
70+
self.ent_ssid = secrets.get("ent_ssid",secrets["ssid"])
71+
self.ent_ident = secrets.get("ent_ident","")
72+
self.ent_user = secrets.get("ent_user")
73+
self.ent_password = secrets.get("ent_password")
8074

8175
# pylint: enable=too-many-arguments
8276

examples/esp32spi_aio_post.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@
44
import time
55
import board
66
import busio
7+
from os import getenv
78
from digitalio import DigitalInOut
89
import neopixel
910
from adafruit_esp32spi import adafruit_esp32spi
1011
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1112

1213
print("ESP32 SPI webclient test")
1314

14-
# Get wifi details and more from a secrets.py file
15-
try:
16-
from secrets import secrets
17-
except ImportError:
18-
print("WiFi secrets are kept in secrets.py, please add them there!")
19-
raise
15+
# Get wifi details and more from a settings.toml file
16+
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
17+
# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY
18+
secrets = {}
19+
for token in ["ssid","password"]:
20+
if getenv("CIRCUITPY_WIFI_"+token.upper()):
21+
secrets[token] = getenv("CIRCUITPY_WIFI_"+token.upper())
22+
for token in ["aio_username","aio_key"]:
23+
if getenv("CIRCUITPY_"+token.upper()):
24+
secrets[token] = getenv("CIRCUITPY_"+token.upper())
25+
26+
if secrets == {}:
27+
try:
28+
# Fallback on secrets.py until depreciation is over and option is removed
29+
from secrets import secrets
30+
except ImportError:
31+
print("WiFi secrets are kept in settings.toml, please add them there!")
32+
raise
2033

2134
# If you are using a board with pre-defined ESP32 Pins:
2235
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -28,21 +41,26 @@
2841
# esp32_ready = DigitalInOut(board.D10)
2942
# esp32_reset = DigitalInOut(board.D5)
3043

31-
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
44+
# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
45+
if 'SCK1' in dir(board):
46+
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
47+
else:
48+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3249
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3350
"""Use below for Most Boards"""
3451
status_light = neopixel.NeoPixel(
3552
board.NEOPIXEL, 1, brightness=0.2
36-
) # Uncomment for Most Boards
53+
)
3754
"""Uncomment below for ItsyBitsy M4"""
3855
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
39-
# Uncomment below for an externally defined RGB LED
56+
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
4057
# import adafruit_rgbled
4158
# from adafruit_esp32spi import PWMOut
4259
# RED_LED = PWMOut.PWMOut(esp, 26)
4360
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4461
# BLUE_LED = PWMOut.PWMOut(esp, 25)
4562
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
63+
4664
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
4765

4866
counter = 0

examples/esp32spi_cheerlights.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import board
66
import busio
7+
from os import getenv
78
from digitalio import DigitalInOut
89

910
import neopixel
@@ -12,29 +13,54 @@
1213
from adafruit_esp32spi import adafruit_esp32spi
1314
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1415

15-
# Get wifi details and more from a secrets.py file
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
16+
# Get wifi details and more from a settings.toml file
17+
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
18+
secrets = {
19+
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
20+
"password": getenv("CIRCUITPY_WIFI_PASSWORD")
21+
}
22+
if secrets == {"ssid": None, "password": None}:
23+
try:
24+
# Fallback on secrets.py until depreciation is over and option is removed
25+
from secrets import secrets
26+
except ImportError:
27+
print("WiFi secrets are kept in settings.toml, please add them there!")
28+
raise
2129

2230
print("ESP32 SPI webclient test")
2331

2432
DATA_SOURCE = "https://api.thingspeak.com/channels/1417/feeds.json?results=1"
2533
DATA_LOCATION = ["feeds", 0, "field2"]
2634

27-
esp32_cs = DigitalInOut(board.D9)
28-
esp32_ready = DigitalInOut(board.D10)
29-
esp32_reset = DigitalInOut(board.D5)
30-
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
35+
# If you are using a board with pre-defined ESP32 Pins:
36+
esp32_cs = DigitalInOut(board.ESP_CS)
37+
esp32_ready = DigitalInOut(board.ESP_BUSY)
38+
esp32_reset = DigitalInOut(board.ESP_RESET)
39+
40+
# If you have an externally connected ESP32:
41+
# esp32_cs = DigitalInOut(board.D9)
42+
# esp32_ready = DigitalInOut(board.D10)
43+
# esp32_reset = DigitalInOut(board.D5)
44+
45+
# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
46+
if 'SCK1' in dir(board):
47+
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
48+
else:
49+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3150
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3251
"""Use below for Most Boards"""
3352
status_light = neopixel.NeoPixel(
3453
board.NEOPIXEL, 1, brightness=0.2
35-
) # Uncomment for Most Boards
54+
)
3655
"""Uncomment below for ItsyBitsy M4"""
3756
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
57+
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
58+
# import adafruit_rgbled
59+
# from adafruit_esp32spi import PWMOut
60+
# RED_LED = PWMOut.PWMOut(esp, 26)
61+
# GREEN_LED = PWMOut.PWMOut(esp, 27)
62+
# BLUE_LED = PWMOut.PWMOut(esp, 25)
63+
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
3864
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
3965

4066
# neopixels

examples/esp32spi_ipconfig.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44
import time
55
import board
66
import busio
7+
from os import getenv
78
from digitalio import DigitalInOut
89
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
910
from adafruit_esp32spi import adafruit_esp32spi
1011

11-
# Get wifi details and more from a secrets.py file
12-
try:
13-
from secrets import secrets
14-
except ImportError:
15-
print("WiFi secrets are kept in secrets.py, please add them there!")
16-
raise
12+
# Get wifi details and more from a settings.toml file
13+
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
14+
secrets = {
15+
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
16+
"password": getenv("CIRCUITPY_WIFI_PASSWORD")
17+
}
18+
if secrets == {"ssid": None, "password": None}:
19+
try:
20+
# Fallback on secrets.py until depreciation is over and option is removed
21+
from secrets import secrets
22+
except ImportError:
23+
print("WiFi secrets are kept in settings.toml, please add them there!")
24+
raise
1725

1826
HOSTNAME = "esp32-spi-hostname-test"
1927

@@ -26,11 +34,21 @@
2634

2735
UDP_TIMEOUT = 20
2836

29-
esp32_cs = DigitalInOut(board.CS1)
37+
# If you are using a board with pre-defined ESP32 Pins:
38+
esp32_cs = DigitalInOut(board.ESP_CS)
3039
esp32_ready = DigitalInOut(board.ESP_BUSY)
3140
esp32_reset = DigitalInOut(board.ESP_RESET)
3241

33-
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
42+
# If you have an externally connected ESP32:
43+
# esp32_cs = DigitalInOut(board.D9)
44+
# esp32_ready = DigitalInOut(board.D10)
45+
# esp32_reset = DigitalInOut(board.D5)
46+
47+
# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
48+
if 'SCK1' in dir(board):
49+
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
50+
else:
51+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3452

3553
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3654
socket.set_interface(esp)

examples/esp32spi_localtime.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,62 @@
44
import time
55
import board
66
import busio
7+
from os import getenv
78
from digitalio import DigitalInOut
89
import neopixel
910
import rtc
1011
from adafruit_esp32spi import adafruit_esp32spi
1112
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1213

13-
# Get wifi details and more from a secrets.py file
14-
try:
15-
from secrets import secrets
16-
except ImportError:
17-
print("WiFi secrets are kept in secrets.py, please add them there!")
18-
raise
14+
# Get wifi details and more from a settings.toml file
15+
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
16+
secrets = {
17+
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
18+
"password": getenv("CIRCUITPY_WIFI_PASSWORD")
19+
}
20+
if secrets == {"ssid": None, "password": None}:
21+
try:
22+
# Fallback on secrets.py until depreciation is over and option is removed
23+
from secrets import secrets
24+
except ImportError:
25+
print("WiFi secrets are kept in settings.toml, please add them there!")
26+
raise
1927

2028
print("ESP32 local time")
2129

2230
TIME_API = "http://worldtimeapi.org/api/ip"
2331

32+
# If you are using a board with pre-defined ESP32 Pins:
2433
esp32_cs = DigitalInOut(board.ESP_CS)
2534
esp32_ready = DigitalInOut(board.ESP_BUSY)
2635
esp32_reset = DigitalInOut(board.ESP_RESET)
27-
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
36+
37+
# If you have an externally connected ESP32:
38+
# esp32_cs = DigitalInOut(board.D9)
39+
# esp32_ready = DigitalInOut(board.D10)
40+
# esp32_reset = DigitalInOut(board.D5)
41+
42+
# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
43+
if 'SCK1' in dir(board):
44+
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
45+
else:
46+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
2847
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
48+
2949
"""Use below for Most Boards"""
3050
status_light = neopixel.NeoPixel(
3151
board.NEOPIXEL, 1, brightness=0.2
32-
) # Uncomment for Most Boards
52+
)
3353
"""Uncomment below for ItsyBitsy M4"""
3454
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
55+
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
56+
# import adafruit_rgbled
57+
# from adafruit_esp32spi import PWMOut
58+
# RED_LED = PWMOut.PWMOut(esp, 26)
59+
# GREEN_LED = PWMOut.PWMOut(esp, 27)
60+
# BLUE_LED = PWMOut.PWMOut(esp, 25)
61+
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
62+
3563
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
3664

3765
the_rtc = rtc.RTC()

examples/esp32spi_secrets.py

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

examples/esp32spi_simpletest.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33

44
import board
55
import busio
6+
from os import getenv
67
from digitalio import DigitalInOut
78
import adafruit_requests as requests
89
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
910
from adafruit_esp32spi import adafruit_esp32spi
1011

11-
# Get wifi details and more from a secrets.py file
12-
try:
13-
from secrets import secrets
14-
except ImportError:
15-
print("WiFi secrets are kept in secrets.py, please add them there!")
16-
raise
12+
# Get wifi details and more from a settings.toml file
13+
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
14+
secrets = {
15+
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
16+
"password": getenv("CIRCUITPY_WIFI_PASSWORD")
17+
}
18+
if secrets == {"ssid": None, "password": None}:
19+
try:
20+
# Fallback on secrets.py until depreciation is over and option is removed
21+
from secrets import secrets
22+
except ImportError:
23+
print("WiFi secrets are kept in settings.toml, please add them there!")
24+
raise
1725

1826
print("ESP32 SPI webclient test")
1927

@@ -42,7 +50,11 @@
4250
# esp32_ready = DigitalInOut(board.D10)
4351
# esp32_reset = DigitalInOut(board.D5)
4452

45-
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
53+
# Secondary (SCK1) SPI used to connect to WiFi board on Arduino Nano Connect RP2040
54+
if 'SCK1' in dir(board):
55+
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
56+
else:
57+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
4658
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
4759

4860
requests.set_socket(socket, esp)

examples/esp32spi_simpletest_rp2040.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33

44
import board
55
import busio
6+
from os import getenv
67
from digitalio import DigitalInOut
78
import adafruit_requests as requests
89
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
910
from adafruit_esp32spi import adafruit_esp32spi
1011

11-
# Get wifi details and more from a secrets.py file
12-
try:
13-
from secrets import secrets
14-
except ImportError:
15-
print("WiFi secrets are kept in secrets.py, please add them there!")
16-
raise
12+
# Get wifi details and more from a settings.toml file
13+
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
14+
secrets = {
15+
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
16+
"password": getenv("CIRCUITPY_WIFI_PASSWORD")
17+
}
18+
if secrets == {"ssid": None, "password": None}:
19+
try:
20+
# Fallback on secrets.py until depreciation is over and option is removed
21+
from secrets import secrets
22+
except ImportError:
23+
print("WiFi secrets are kept in settings.toml, please add them there!")
24+
raise
1725

1826
print("Raspberry Pi RP2040 - ESP32 SPI webclient test")
1927

0 commit comments

Comments
 (0)