Skip to content

Use setting.toml instead of secrets.py #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions adafruit_esp32spi/adafruit_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
"""

import struct
import os
import time
from micropython import const
from adafruit_bus_device.spi_device import SPIDevice
from digitalio import Direction


__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI.git"

Expand Down Expand Up @@ -568,10 +570,10 @@ def disconnect(self):
if resp[0][0] != 1:
raise OSError("Failed to disconnect")

def connect(self, secrets):
def connect(self):
"""Connect to an access point using a secrets dictionary
that contains a 'ssid' and 'password' entry"""
self.connect_AP(secrets["ssid"], secrets["password"])
self.connect_AP(os.getenv("ssid"), os.getenv("password"))

def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-name
"""Connect to an access point with given name and password.
Expand All @@ -587,6 +589,12 @@ def connect_AP(self, ssid, password, timeout_s=10): # pylint: disable=invalid-n
f"Connect to AP: {ssid=}, password=\
{repr(password if self._debug_show_secrets else '*' * len(password))}"
)

if ssid is None or password is None:
raise RuntimeError(
"Must provide ssid and password in settings.toml."
"Please add them there and try again."
)
if isinstance(ssid, str):
ssid = bytes(ssid, "utf-8")
if password:
Expand Down
26 changes: 12 additions & 14 deletions adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

# pylint: disable=no-name-in-module

import os
from time import sleep
from micropython import const
import adafruit_requests as requests
Expand All @@ -33,15 +33,13 @@ class ESPSPI_WiFiManager:
def __init__(
self,
esp,
secrets,
status_pixel=None,
attempts=2,
connection_type=NORMAL,
debug=False,
):
"""
:param ESP_SPIcontrol esp: The ESP object we are using
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
or RGB LED (default=None). The status LED, if given, turns red when
attempting to connect to a Wi-Fi network or create an access point,
Expand All @@ -55,8 +53,8 @@ def __init__(
# Read the settings
self.esp = esp
self.debug = debug
self.ssid = secrets["ssid"]
self.password = secrets.get("password", None)
self.ssid = os.getenv("ssid")
self.password = os.getenv("password")
self.attempts = attempts
self._connection_type = connection_type
requests.set_socket(socket, esp)
Expand All @@ -65,18 +63,18 @@ def __init__(
self._ap_index = 0

# Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
if secrets.get("ent_ssid"):
self.ent_ssid = secrets["ent_ssid"]
if os.getenv("ent_ssid") is not None:
self.ent_ssid = os.getenv("ent_ssid")
else:
self.ent_ssid = secrets["ssid"]
if secrets.get("ent_ident"):
self.ent_ident = secrets["ent_ident"]
self.ent_ssid = os.getenv("ssid")
if os.getenv("ent_ident") is not None:
self.ent_ident = os.getenv("ent_ident")
else:
self.ent_ident = ""
if secrets.get("ent_user"):
self.ent_user = secrets["ent_user"]
if secrets.get("ent_password"):
self.ent_password = secrets["ent_password"]
if os.getenv("ent_user") is not None:
self.ent_user = os.getenv("ent_user")
if os.getenv("ent_password") is not None:
self.ent_password = os.getenv("ent_password")

# pylint: enable=too-many-arguments

Expand Down
25 changes: 16 additions & 9 deletions examples/esp32spi_aio_post.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
import board
import busio
Expand All @@ -11,12 +12,18 @@

print("ESP32 SPI webclient test")

# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
if (
os.getenv("ssid") is None
or os.getenv("password") is None
or os.getenv("aio_username") is None
or os.getenv("aio_key") is None
):
raise RuntimeError(
"Must provide 'ssid', 'password', 'aio_username' and 'aio_key'"
" in settings.toml. Please add them there and try again."
)

# Set wifi details and more from a settings.toml file

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
Expand All @@ -43,7 +50,7 @@
# GREEN_LED = PWMOut.PWMOut(esp, 27)
# BLUE_LED = PWMOut.PWMOut(esp, 25)
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, status_light)

counter = 0

Expand All @@ -55,12 +62,12 @@
payload = {"value": data}
response = wifi.post(
"https://io.adafruit.com/api/v2/"
+ secrets["aio_username"]
+ os.getenv("aio_username")
+ "/feeds/"
+ feed
+ "/data",
json=payload,
headers={"X-AIO-KEY": secrets["aio_key"]},
headers={"X-AIO-KEY": os.getenv("aio_key")},
)
print(response.json())
response.close()
Expand Down
22 changes: 12 additions & 10 deletions examples/esp32spi_cheerlights.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager

# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Set wifi details and more from a settings.toml file

print("ESP32 SPI webclient test")

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

esp32_cs = DigitalInOut(board.D9)
esp32_ready = DigitalInOut(board.D10)
esp32_reset = DigitalInOut(board.D5)
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
Expand All @@ -35,7 +37,7 @@
) # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, status_light)

# neopixels
pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3)
Expand Down
15 changes: 8 additions & 7 deletions examples/esp32spi_ipconfig.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi

# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Set wifi details and more from a settings.toml file
if os.getenv("ssid") is None or os.getenv("password") is None:
raise RuntimeError(
"Must provide 'ssid', 'password' in settings.toml. "
"Please add them there and try again."
)

HOSTNAME = "esp32-spi-hostname-test"

Expand Down Expand Up @@ -48,7 +49,7 @@
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
esp.connect_AP(os.getenv("ssid"), os.getenv("password"))
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
Expand Down
15 changes: 8 additions & 7 deletions examples/esp32spi_localtime.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
import board
import busio
Expand All @@ -10,12 +11,12 @@
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager

# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Set wifi details and more from a settings.toml file
if os.getenv("ssid") is None or os.getenv("password") is None:
raise RuntimeError(
"Must provide 'ssid', 'password' in settings.toml. "
"Please add them there and try again."
)

print("ESP32 local time")

Expand All @@ -32,7 +33,7 @@
) # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, status_light)

the_rtc = rtc.RTC()

Expand Down
3 changes: 3 additions & 0 deletions examples/esp32spi_secrets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# THIS FILE IS DEPRECATED.
# Use settings.toml instead.

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

Expand Down
14 changes: 14 additions & 0 deletions examples/esp32spi_settings.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

# The file should be renamed to `settings.toml` and saved in the root of
# the CIRCUITPY drive.

ssid="yourssid"
password="yourpassword"
timezone="America/New_York" # Check http://worldtimeapi.org/timezones
aio_username="youraiousername"
aio_key="youraiokey"
15 changes: 8 additions & 7 deletions examples/esp32spi_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import board
import busio
from digitalio import DigitalInOut
import adafruit_requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi

# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Set wifi details and more from a settings.toml file
if os.getenv("ssid") is None or os.getenv("password") is None:
raise RuntimeError(
"Must provide 'ssid', 'password' in settings.toml. "
"Please add them there and try again."
)

print("ESP32 SPI webclient test")

Expand Down Expand Up @@ -58,7 +59,7 @@
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
esp.connect_AP(os.getenv("ssid"), os.getenv("password"))
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
Expand Down
15 changes: 8 additions & 7 deletions examples/esp32spi_simpletest_rp2040.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import board
import busio
from digitalio import DigitalInOut
import adafruit_requests as requests
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi

# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Set wifi details and more from a settings.toml file
if os.getenv("ssid") is None or os.getenv("password") is None:
raise RuntimeError(
"Must provide 'ssid', 'password' in settings.toml. "
"Please add them there and try again."
)

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

Expand Down Expand Up @@ -41,7 +42,7 @@
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
esp.connect_AP(os.getenv("ssid"), os.getenv("password"))
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
Expand Down
4 changes: 2 additions & 2 deletions examples/esp32spi_tcp_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2021 Adafruit Industries
# SPDX-License-Identifier: MIT

from secrets import secrets # pylint: disable=no-name-in-module

import board
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
Expand All @@ -21,7 +21,7 @@
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

# connect to wifi AP
esp.connect(secrets)
esp.connect()

# test for connectivity to server
print("Server ping:", esp.ping(HOST), "ms")
Expand Down
Loading