Skip to content

Commit 2b5fd71

Browse files
committed
Secrets Cleanup: P Part 2
1 parent 071c95b commit 2b5fd71

File tree

31 files changed

+349
-265
lines changed

31 files changed

+349
-265
lines changed

PyPortal/PyPortal_LIFX_Controller/code.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
1010
Brent Rubell for Adafruit Industries, 2019
1111
"""
12-
import os
12+
13+
from os import getenv
1314

1415
import board
1516
import displayio
@@ -26,19 +27,26 @@
2627
# import lifx library
2728
import adafruit_lifx
2829

29-
secrets = {
30-
"ssid" : os.getenv("CIRCUITPY_WIFI_SSID"),
31-
"password" : os.getenv("CIRCUITPY_WIFI_PASSWORD"),
32-
}
30+
# Get WiFi details, ensure these are setup in settings.toml
31+
ssid = getenv("CIRCUITPY_WIFI_SSID")
32+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
33+
34+
if None in [ssid, password]:
35+
raise RuntimeError(
36+
"WiFi settings are kept in settings.toml, "
37+
"please add them there. The settings file must contain "
38+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
39+
"at a minimum."
40+
)
3341

3442
# ESP32 SPI
3543
esp32_cs = DigitalInOut(board.ESP_CS)
3644
esp32_ready = DigitalInOut(board.ESP_BUSY)
3745
esp32_reset = DigitalInOut(board.ESP_RESET)
3846
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3947
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
40-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
41-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
48+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
49+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
4250

4351
# These pins are used as both analog and digital! XL, XR and YU must be analog
4452
# and digital capable. YD just need to be digital
@@ -49,7 +57,7 @@
4957

5058
# Set this to your LIFX personal access token in settings.toml
5159
# (to obtain a token, visit: https://cloud.lifx.com/settings)
52-
lifx_token = os.getenv("LIFX_TOKEN")
60+
lifx_token = getenv("LIFX_TOKEN")
5361

5462
# Initialize the LIFX API Helper
5563
lifx = adafruit_lifx.LIFX(wifi, lifx_token)

PyPortal/PyPortal_LastFM/code.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,35 @@
77
and display it on a screen
88
If you can find something that spits out JSON data, we can display it!
99
"""
10+
11+
from os import getenv
1012
import time
1113
import board
1214
from adafruit_pyportal import PyPortal
1315

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
16+
# Get WiFi details, ensure these are setup in settings.toml
17+
ssid = getenv("CIRCUITPY_WIFI_SSID")
18+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
19+
20+
if None in [ssid, password]:
21+
raise RuntimeError(
22+
"WiFi settings are kept in settings.toml, "
23+
"please add them there. The settings file must contain "
24+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
25+
"at a minimum."
26+
)
2027

2128
# Set up where we'll be fetching data from
2229
DATA_SOURCE = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&format=json"
2330
CAPTION = "www.last.fm/user"
2431
# If we have an access token, we can query more often
25-
if 'lfm_username' in secrets:
26-
DATA_SOURCE += "&user="+secrets['lfm_username']
27-
CAPTION += "/"+secrets['lfm_username']
28-
if 'lfm_key' in secrets:
29-
DATA_SOURCE += "&api_key="+secrets['lfm_key']
32+
lfm_username = getenv("lfm_username")
33+
lfm_key = getenv("lfm_key")
34+
if lfm_username:
35+
DATA_SOURCE += "&user=" + lfm_username
36+
CAPTION += "/" + lfm_username
37+
if lfm_key:
38+
DATA_SOURCE += "&api_key=" + lfm_key
3039
print(DATA_SOURCE)
3140

3241
# Total number of plays

PyPortal/PyPortal_LeagueLevel/code.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,33 @@
55
"""
66
This project will access the League of Legends API, grab a Summoner's Level
77
and display it on a screen.
8-
You'll need a Riot API key in your secrets.py file
8+
You'll need a Riot API key in your settings.toml file
99
If you can find something that spits out JSON data, we can display it!
1010
"""
11-
import os
11+
12+
from os import getenv
1213
import time
1314
import board
1415
from adafruit_pyportal import PyPortal
1516

17+
# Get WiFi details, ensure these are setup in settings.toml
18+
ssid = getenv("CIRCUITPY_WIFI_SSID")
19+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
20+
21+
if None in [ssid, password]:
22+
raise RuntimeError(
23+
"WiFi settings are kept in settings.toml, "
24+
"please add them there. The settings file must contain "
25+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
26+
"at a minimum."
27+
)
28+
1629
#Choose a valid Summoner name
1730
SUMMONER_NAME = "RiotSchmick"
1831

1932
# Set up where we'll be fetching data from
2033
DATA_SOURCE = "https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+SUMMONER_NAME
21-
DATA_SOURCE += "?api_key=" + os.getenv("LEAGUE_TOKEN")
34+
DATA_SOURCE += "?api_key=" + getenv("LEAGUE_TOKEN")
2235
DATA_LOCATION = ["summonerLevel"]
2336
CAPTION = "SUMMONER "+SUMMONER_NAME
2437

PyPortal/PyPortal_MQTT_Control/code.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
from os import getenv
56
import board
67
import displayio
78
import busio
@@ -18,14 +19,19 @@
1819
import adafruit_touchscreen
1920
import adafruit_minimqtt.adafruit_minimqtt as MQTT
2021

21-
# ------------- WiFi ------------- #
22+
# Get WiFi details, ensure these are setup in settings.toml
23+
ssid = getenv("CIRCUITPY_WIFI_SSID")
24+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
25+
26+
if None in [ssid, password]:
27+
raise RuntimeError(
28+
"WiFi settings are kept in settings.toml, "
29+
"please add them there. The settings file must contain "
30+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
31+
"at a minimum."
32+
)
2233

23-
# Get wifi details and more from a secrets.py file
24-
try:
25-
from secrets import secrets
26-
except ImportError:
27-
print("WiFi secrets are kept in secrets.py, please add them there!")
28-
raise
34+
# ------------- WiFi ------------- #
2935

3036
# If you are using a board with pre-defined ESP32 Pins:
3137
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -34,8 +40,8 @@
3440

3541
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3642
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
37-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
38-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
43+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
44+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
3945

4046
# ------- Sensor Setup ------- #
4147
# init. the temperature sensor
@@ -234,10 +240,10 @@ def message(client, topic, message):
234240

235241
# Set up a MiniMQTT Client
236242
client = MQTT.MQTT(
237-
broker=secrets["broker"],
243+
broker=getenv("mqtt_broker"),
238244
port=1883,
239-
username=secrets["user"],
240-
password=secrets["pass"],
245+
username=getenv("mqtt_username"),
246+
password=getenv("mqtt_password"),
241247
socket_pool=pool,
242248
ssl_context=ssl_context,
243249
)

PyPortal/PyPortal_MQTT_Control/secrets.py

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: 2020 Anne Barela for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# This file is where you keep private settings, passwords, and tokens!
6+
# If you put them in the code you risk committing that info or sharing it
7+
8+
CIRCUITPY_WIFI_SSID="your-wifi-ssid"
9+
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
10+
mqtt_broker="your-mqtt-broker-url-or-ip"
11+
mqtt_username="your-mqtt-broker-username"
12+
mqtt_password="your-mqtt-broker-password"

PyPortal/PyPortal_Mirror_Display/code.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
from os import getenv
56
import sys
67
import time
78
import board
@@ -11,20 +12,25 @@
1112
sys.path.append(cwd)
1213
import openweather_graphics # pylint: disable=wrong-import-position
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, ensure these are setup in settings.toml
16+
ssid = getenv("CIRCUITPY_WIFI_SSID")
17+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
18+
19+
if None in [ssid, password]:
20+
raise RuntimeError(
21+
"WiFi settings are kept in settings.toml, "
22+
"please add them there. The settings file must contain "
23+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
24+
"at a minimum."
25+
)
2026

2127
# Use cityname, country code where countrycode is ISO3166 format.
2228
# E.g. "New York, US" or "London, GB"
2329
LOCATION = "New York, US"
2430

2531
# Set up where we'll be fetching data from
2632
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION
27-
DATA_SOURCE += "&appid="+secrets['openweather_token']
33+
DATA_SOURCE += "&appid=" + getenv('openweather_token')
2834
# You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22'
2935
DATA_LOCATION = []
3036

PyPortal/PyPortal_OpenWeather/code.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
weather for your location... and display it on a screen!
88
if you can find something that spits out JSON data, we can display it
99
"""
10+
11+
from os import getenv
1012
import sys
1113
import time
1214
import board
@@ -15,20 +17,25 @@
1517
sys.path.append(cwd)
1618
import openweather_graphics # pylint: disable=wrong-import-position
1719

18-
# Get wifi details and more from a secrets.py file
19-
try:
20-
from secrets import secrets
21-
except ImportError:
22-
print("WiFi secrets are kept in secrets.py, please add them there!")
23-
raise
20+
# Get WiFi details, ensure these are setup in settings.toml
21+
ssid = getenv("CIRCUITPY_WIFI_SSID")
22+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
23+
24+
if None in [ssid, password]:
25+
raise RuntimeError(
26+
"WiFi settings are kept in settings.toml, "
27+
"please add them there. The settings file must contain "
28+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
29+
"at a minimum."
30+
)
2431

2532
# Use cityname, country code where countrycode is ISO3166 format.
2633
# E.g. "New York, US" or "London, GB"
2734
LOCATION = "Manhattan, US"
2835

2936
# Set up where we'll be fetching data from
3037
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION
31-
DATA_SOURCE += "&appid="+secrets['openweather_token']
38+
DATA_SOURCE += "&appid=" + getenv('openweather_token')
3239
# You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22'
3340
DATA_LOCATION = []
3441

PyPortal/PyPortal_Philips_Hue_Controller/code.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
88
Brent Rubell for Adafruit Industries, 2019
99
"""
10-
import os
10+
11+
from os import getenv
1112

1213
import board
1314
import displayio
@@ -23,23 +24,31 @@
2324
# Import Philips Hue Bridge
2425
from adafruit_hue import Bridge
2526

26-
secrets = dict()
27-
secrets["ssid"] = os.getenv("CIRCUITPY_WIFI_SSID")
28-
secrets["password"] = os.getenv("CIRCUITPY_WIFI_PASSWORD")
27+
# Get WiFi details, ensure these are setup in settings.toml
28+
ssid = getenv("CIRCUITPY_WIFI_SSID")
29+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
30+
31+
if None in [ssid, password]:
32+
raise RuntimeError(
33+
"WiFi settings are kept in settings.toml, "
34+
"please add them there. The settings file must contain "
35+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
36+
"at a minimum."
37+
)
2938

3039
# ESP32 SPI
3140
esp32_cs = DigitalInOut(board.ESP_CS)
3241
esp32_ready = DigitalInOut(board.ESP_BUSY)
3342
esp32_reset = DigitalInOut(board.ESP_RESET)
3443
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3544
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
36-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
37-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
45+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
46+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
3847

39-
# Attempt to load bridge username and IP address from secrets.py
48+
# Attempt to load bridge username and IP address from settings.toml
4049
try:
41-
username = os.getenv("HUE_USERNAME")
42-
bridge_ip = os.getenv("BRIDGE_IP")
50+
username = getenv("HUE_USERNAME")
51+
bridge_ip = getenv("BRIDGE_IP")
4352
my_bridge = Bridge(wifi, bridge_ip, username)
4453
except:
4554
# Perform first-time bridge setup

0 commit comments

Comments
 (0)