Skip to content

Commit 18892f7

Browse files
authored
Merge pull request #182 from RetiredWizard/main
Another settings.toml update, slightly different approach
2 parents e778633 + 6092ea0 commit 18892f7

13 files changed

+264
-100
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

+6-12
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

+27-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
from os import getenv
56
import board
67
import busio
78
from digitalio import DigitalInOut
@@ -11,12 +12,24 @@
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 not 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,24 @@
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"""
34-
status_light = neopixel.NeoPixel(
35-
board.NEOPIXEL, 1, brightness=0.2
36-
) # Uncomment for Most Boards
51+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
3752
"""Uncomment below for ItsyBitsy M4"""
3853
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
39-
# Uncomment below for an externally defined RGB LED
54+
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
4055
# import adafruit_rgbled
4156
# from adafruit_esp32spi import PWMOut
4257
# RED_LED = PWMOut.PWMOut(esp, 26)
4358
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4459
# BLUE_LED = PWMOut.PWMOut(esp, 25)
4560
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
61+
4662
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
4763

4864
counter = 0

examples/esp32spi_cheerlights.py

+37-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
from os import getenv
56
import board
67
import busio
78
from digitalio import DigitalInOut
@@ -12,29 +13,52 @@
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"""
33-
status_light = neopixel.NeoPixel(
34-
board.NEOPIXEL, 1, brightness=0.2
35-
) # Uncomment for Most Boards
52+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
3653
"""Uncomment below for ItsyBitsy M4"""
3754
# 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)
3862
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
3963

4064
# neopixels

examples/esp32spi_ipconfig.py

+26-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
from os import getenv
56
import board
67
import busio
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

+36-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5+
from os import getenv
56
import board
67
import busio
78
from digitalio import DigitalInOut
@@ -10,28 +11,53 @@
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"""
30-
status_light = neopixel.NeoPixel(
31-
board.NEOPIXEL, 1, brightness=0.2
32-
) # Uncomment for Most Boards
50+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
3351
"""Uncomment below for ItsyBitsy M4"""
3452
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
53+
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
54+
# import adafruit_rgbled
55+
# from adafruit_esp32spi import PWMOut
56+
# RED_LED = PWMOut.PWMOut(esp, 26)
57+
# GREEN_LED = PWMOut.PWMOut(esp, 27)
58+
# BLUE_LED = PWMOut.PWMOut(esp, 25)
59+
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
60+
3561
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
3662

3763
the_rtc = rtc.RTC()

examples/esp32spi_secrets.py

-13
This file was deleted.

examples/esp32spi_settings.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# This file is where you keep secret settings, passwords, and tokens!
5+
# If you put them in the code you risk committing that info or sharing it
6+
7+
# The file should be renamed to `settings.toml` and saved in the root of
8+
# the CIRCUITPY drive.
9+
10+
CIRCUITPY_WIFI_SSID="yourssid"
11+
CIRCUITPY_WIFI_PASSWORD="yourpassword"
12+
CIRCUITPY_TIMEZONE="America/New_York"
13+
CIRCUITPY_AIO_USERNAME="youraiousername"
14+
CIRCUITPY_AIO_KEY="youraiokey"

examples/esp32spi_simpletest.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
from os import getenv
45
import board
56
import busio
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)

0 commit comments

Comments
 (0)