Secrets Cleanup: P Part 2

This commit is contained in:
Justin Myers 2025-04-09 10:16:06 -07:00
parent 071c95b5b7
commit 2b5fd71ab0
31 changed files with 349 additions and 265 deletions

View file

@ -9,7 +9,8 @@ https://learn.adafruit.com/pyportal-smart-lighting-controller
Brent Rubell for Adafruit Industries, 2019 Brent Rubell for Adafruit Industries, 2019
""" """
import os
from os import getenv
import board import board
import displayio import displayio
@ -26,10 +27,17 @@ from adafruit_esp32spi import adafruit_esp32spi_wifimanager
# import lifx library # import lifx library
import adafruit_lifx import adafruit_lifx
secrets = { # Get WiFi details, ensure these are setup in settings.toml
"ssid" : os.getenv("CIRCUITPY_WIFI_SSID"), ssid = getenv("CIRCUITPY_WIFI_SSID")
"password" : os.getenv("CIRCUITPY_WIFI_PASSWORD"), password = getenv("CIRCUITPY_WIFI_PASSWORD")
}
if None in [ssid, password]:
raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# ESP32 SPI # ESP32 SPI
esp32_cs = DigitalInOut(board.ESP_CS) esp32_cs = DigitalInOut(board.ESP_CS)
@ -37,8 +45,8 @@ esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET) esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
# These pins are used as both analog and digital! XL, XR and YU must be analog # These pins are used as both analog and digital! XL, XR and YU must be analog
# and digital capable. YD just need to be digital # and digital capable. YD just need to be digital
@ -49,7 +57,7 @@ ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
# Set this to your LIFX personal access token in settings.toml # Set this to your LIFX personal access token in settings.toml
# (to obtain a token, visit: https://cloud.lifx.com/settings) # (to obtain a token, visit: https://cloud.lifx.com/settings)
lifx_token = os.getenv("LIFX_TOKEN") lifx_token = getenv("LIFX_TOKEN")
# Initialize the LIFX API Helper # Initialize the LIFX API Helper
lifx = adafruit_lifx.LIFX(wifi, lifx_token) lifx = adafruit_lifx.LIFX(wifi, lifx_token)

View file

@ -7,26 +7,35 @@ This example will access the lastFM API, grab a number like subreddit followers
and display it on a screen and display it on a screen
If you can find something that spits out JSON data, we can display it! If you can find something that spits out JSON data, we can display it!
""" """
from os import getenv
import time import time
import board import board
from adafruit_pyportal import PyPortal from adafruit_pyportal import PyPortal
# Get wifi details and more from a secrets.py file # Get WiFi details, ensure these are setup in settings.toml
try: ssid = getenv("CIRCUITPY_WIFI_SSID")
from secrets import secrets password = getenv("CIRCUITPY_WIFI_PASSWORD")
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!") if None in [ssid, password]:
raise raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# Set up where we'll be fetching data from # Set up where we'll be fetching data from
DATA_SOURCE = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&format=json" DATA_SOURCE = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&format=json"
CAPTION = "www.last.fm/user" CAPTION = "www.last.fm/user"
# If we have an access token, we can query more often # If we have an access token, we can query more often
if 'lfm_username' in secrets: lfm_username = getenv("lfm_username")
DATA_SOURCE += "&user="+secrets['lfm_username'] lfm_key = getenv("lfm_key")
CAPTION += "/"+secrets['lfm_username'] if lfm_username:
if 'lfm_key' in secrets: DATA_SOURCE += "&user=" + lfm_username
DATA_SOURCE += "&api_key="+secrets['lfm_key'] CAPTION += "/" + lfm_username
if lfm_key:
DATA_SOURCE += "&api_key=" + lfm_key
print(DATA_SOURCE) print(DATA_SOURCE)
# Total number of plays # Total number of plays

View file

@ -5,20 +5,33 @@
""" """
This project will access the League of Legends API, grab a Summoner's Level This project will access the League of Legends API, grab a Summoner's Level
and display it on a screen. and display it on a screen.
You'll need a Riot API key in your secrets.py file You'll need a Riot API key in your settings.toml file
If you can find something that spits out JSON data, we can display it! If you can find something that spits out JSON data, we can display it!
""" """
import os
from os import getenv
import time import time
import board import board
from adafruit_pyportal import PyPortal from adafruit_pyportal import PyPortal
# Get WiFi details, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
if None in [ssid, password]:
raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
#Choose a valid Summoner name #Choose a valid Summoner name
SUMMONER_NAME = "RiotSchmick" SUMMONER_NAME = "RiotSchmick"
# Set up where we'll be fetching data from # Set up where we'll be fetching data from
DATA_SOURCE = "https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+SUMMONER_NAME DATA_SOURCE = "https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+SUMMONER_NAME
DATA_SOURCE += "?api_key=" + os.getenv("LEAGUE_TOKEN") DATA_SOURCE += "?api_key=" + getenv("LEAGUE_TOKEN")
DATA_LOCATION = ["summonerLevel"] DATA_LOCATION = ["summonerLevel"]
CAPTION = "SUMMONER "+SUMMONER_NAME CAPTION = "SUMMONER "+SUMMONER_NAME

View file

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from os import getenv
import board import board
import displayio import displayio
import busio import busio
@ -18,14 +19,19 @@ from adafruit_button import Button
import adafruit_touchscreen import adafruit_touchscreen
import adafruit_minimqtt.adafruit_minimqtt as MQTT import adafruit_minimqtt.adafruit_minimqtt as MQTT
# ------------- WiFi ------------- # # Get WiFi details, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
# Get wifi details and more from a secrets.py file if None in [ssid, password]:
try: raise RuntimeError(
from secrets import secrets "WiFi settings are kept in settings.toml, "
except ImportError: "please add them there. The settings file must contain "
print("WiFi secrets are kept in secrets.py, please add them there!") "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
raise "at a minimum."
)
# ------------- WiFi ------------- #
# If you are using a board with pre-defined ESP32 Pins: # If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS) esp32_cs = DigitalInOut(board.ESP_CS)
@ -34,8 +40,8 @@ esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
# ------- Sensor Setup ------- # # ------- Sensor Setup ------- #
# init. the temperature sensor # init. the temperature sensor
@ -234,10 +240,10 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
# Set up a MiniMQTT Client # Set up a MiniMQTT Client
client = MQTT.MQTT( client = MQTT.MQTT(
broker=secrets["broker"], broker=getenv("mqtt_broker"),
port=1883, port=1883,
username=secrets["user"], username=getenv("mqtt_username"),
password=secrets["pass"], password=getenv("mqtt_password"),
socket_pool=pool, socket_pool=pool,
ssl_context=ssl_context, ssl_context=ssl_context,
) )

View file

@ -1,11 +0,0 @@
# SPDX-FileCopyrightText: 2020 Anne Barela for Adafruit Industries
#
# SPDX-License-Identifier: MIT
secrets = {
'ssid' : '_your_wifi_ssid_',
'password' : '_your_wifi_password_',
'broker' : '_your_mqtt_broker_url_or_ip',
'user' : '_your_mqtt_broker_username_',
'pass' : '_your_mqtt_broker_password_'
}

View file

@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: 2020 Anne Barela for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# This file is where you keep private settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
CIRCUITPY_WIFI_SSID="your-wifi-ssid"
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
mqtt_broker="your-mqtt-broker-url-or-ip"
mqtt_username="your-mqtt-broker-username"
mqtt_password="your-mqtt-broker-password"

View file

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from os import getenv
import sys import sys
import time import time
import board import board
@ -11,12 +12,17 @@ cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where th
sys.path.append(cwd) sys.path.append(cwd)
import openweather_graphics # pylint: disable=wrong-import-position import openweather_graphics # pylint: disable=wrong-import-position
# Get wifi details and more from a secrets.py file # Get WiFi details, ensure these are setup in settings.toml
try: ssid = getenv("CIRCUITPY_WIFI_SSID")
from secrets import secrets password = getenv("CIRCUITPY_WIFI_PASSWORD")
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!") if None in [ssid, password]:
raise raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# Use cityname, country code where countrycode is ISO3166 format. # Use cityname, country code where countrycode is ISO3166 format.
# E.g. "New York, US" or "London, GB" # E.g. "New York, US" or "London, GB"
@ -24,7 +30,7 @@ LOCATION = "New York, US"
# Set up where we'll be fetching data from # Set up where we'll be fetching data from
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION
DATA_SOURCE += "&appid="+secrets['openweather_token'] DATA_SOURCE += "&appid=" + getenv('openweather_token')
# You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22' # You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22'
DATA_LOCATION = [] DATA_LOCATION = []

View file

@ -7,6 +7,8 @@ This example queries the Open Weather Maps site API to find out the current
weather for your location... and display it on a screen! weather for your location... and display it on a screen!
if you can find something that spits out JSON data, we can display it if you can find something that spits out JSON data, we can display it
""" """
from os import getenv
import sys import sys
import time import time
import board import board
@ -15,12 +17,17 @@ cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where th
sys.path.append(cwd) sys.path.append(cwd)
import openweather_graphics # pylint: disable=wrong-import-position import openweather_graphics # pylint: disable=wrong-import-position
# Get wifi details and more from a secrets.py file # Get WiFi details, ensure these are setup in settings.toml
try: ssid = getenv("CIRCUITPY_WIFI_SSID")
from secrets import secrets password = getenv("CIRCUITPY_WIFI_PASSWORD")
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!") if None in [ssid, password]:
raise raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# Use cityname, country code where countrycode is ISO3166 format. # Use cityname, country code where countrycode is ISO3166 format.
# E.g. "New York, US" or "London, GB" # E.g. "New York, US" or "London, GB"
@ -28,7 +35,7 @@ LOCATION = "Manhattan, US"
# Set up where we'll be fetching data from # Set up where we'll be fetching data from
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION
DATA_SOURCE += "&appid="+secrets['openweather_token'] DATA_SOURCE += "&appid=" + getenv('openweather_token')
# You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22' # You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22'
DATA_LOCATION = [] DATA_LOCATION = []

View file

@ -7,7 +7,8 @@ PyPortal Philips Hue Lighting Controller
Brent Rubell for Adafruit Industries, 2019 Brent Rubell for Adafruit Industries, 2019
""" """
import os
from os import getenv
import board import board
import displayio import displayio
@ -23,9 +24,17 @@ from adafruit_esp32spi import adafruit_esp32spi_wifimanager
# Import Philips Hue Bridge # Import Philips Hue Bridge
from adafruit_hue import Bridge from adafruit_hue import Bridge
secrets = dict() # Get WiFi details, ensure these are setup in settings.toml
secrets["ssid"] = os.getenv("CIRCUITPY_WIFI_SSID") ssid = getenv("CIRCUITPY_WIFI_SSID")
secrets["password"] = os.getenv("CIRCUITPY_WIFI_PASSWORD") password = getenv("CIRCUITPY_WIFI_PASSWORD")
if None in [ssid, password]:
raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# ESP32 SPI # ESP32 SPI
esp32_cs = DigitalInOut(board.ESP_CS) esp32_cs = DigitalInOut(board.ESP_CS)
@ -33,13 +42,13 @@ esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET) esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
# Attempt to load bridge username and IP address from secrets.py # Attempt to load bridge username and IP address from settings.toml
try: try:
username = os.getenv("HUE_USERNAME") username = getenv("HUE_USERNAME")
bridge_ip = os.getenv("BRIDGE_IP") bridge_ip = getenv("BRIDGE_IP")
my_bridge = Bridge(wifi, bridge_ip, username) my_bridge = Bridge(wifi, bridge_ip, username)
except: except:
# Perform first-time bridge setup # Perform first-time bridge setup

View file

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from os import getenv
import time import time
import board import board
import busio import busio
@ -13,12 +14,17 @@ from adafruit_pyportal import PyPortal
from adafruit_bitmap_font import bitmap_font from adafruit_bitmap_font import bitmap_font
from adafruit_display_text import label from adafruit_display_text import label
try: # Get WiFi details, ensure these are setup in settings.toml
from secrets import secrets ssid = getenv("CIRCUITPY_WIFI_SSID")
except ImportError: password = getenv("CIRCUITPY_WIFI_PASSWORD")
print("""WiFi settings are kept in secrets.py, please add them there!
the secrets dictionary must contain 'ssid' and 'password' at a minimum""") if None in [ssid, password]:
raise raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# Label colors # Label colors
LABEL_DAY_COLOR = 0xFFFFFF LABEL_DAY_COLOR = 0xFFFFFF
@ -88,7 +94,7 @@ if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("Connecting to AP...") print("Connecting to AP...")
while not esp.is_connected: while not esp.is_connected:
try: try:
esp.connect_AP(secrets['ssid'], secrets['password']) esp.connect_AP(ssid, password)
except RuntimeError as e: except RuntimeError as e:
print("could not connect to AP, retrying: ", e) print("could not connect to AP, retrying: ", e)
continue continue
@ -117,7 +123,7 @@ while True:
if (not refresh_time) or (time.monotonic() - refresh_time) > 3600: if (not refresh_time) or (time.monotonic() - refresh_time) > 3600:
try: try:
print("Getting new time from internet...") print("Getting new time from internet...")
pyportal.get_local_time(secrets['timezone']) pyportal.get_local_time(getenv('timezone'))
refresh_time = time.monotonic() refresh_time = time.monotonic()
# set the_time # set the_time
the_time = time.localtime() the_time = time.localtime()

View file

@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries # SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from os import getenv
import time import time
import math import math
import board import board
@ -13,6 +14,18 @@ import displayio
import adafruit_touchscreen import adafruit_touchscreen
import adafruit_imageload import adafruit_imageload
# Get WiFi details, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
if None in [ssid, password]:
raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# Set up the touchscreen # Set up the touchscreen
ts = adafruit_touchscreen.Touchscreen( ts = adafruit_touchscreen.Touchscreen(
board.TOUCH_XL, board.TOUCH_XL,
@ -23,13 +36,6 @@ ts = adafruit_touchscreen.Touchscreen(
size=(320, 240), size=(320, 240),
) )
# 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 you are using a board with pre-defined ESP32 Pins: # If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS) esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_ready = DigitalInOut(board.ESP_BUSY)
@ -38,9 +44,9 @@ esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
# Set the ip of your Roku here # Set the ip of your Roku here
ip = "192.168.1.3" ip = "192.168.1.3"

View file

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from os import getenv
import time import time
import gc import gc
import board import board
@ -20,6 +21,21 @@ import adafruit_touchscreen
from adafruit_minimqtt import MQTT from adafruit_minimqtt import MQTT
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")
if None in [ssid, password, aio_username, aio_key]:
raise RuntimeError(
"WiFi and Adafruit IO settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
)
DISPLAY_COLOR = 0x006600 DISPLAY_COLOR = 0x006600
SWITCH_COLOR = 0x008800 SWITCH_COLOR = 0x008800
SWITCH_FILL_COLOR = 0xffffff SWITCH_FILL_COLOR = 0xffffff
@ -39,15 +55,8 @@ def get_local_timestamp(location=None):
"""Fetch and "set" the local time of this microcontroller to the local time at the location, using an internet time API. """Fetch and "set" the local time of this microcontroller to the local time at the location, using an internet time API.
:param str location: Your city and country, e.g. ``"New York, US"``. :param str location: Your city and country, e.g. ``"New York, US"``.
""" """
# pylint: enable=line-too-long
api_url = None api_url = None
try: location = getenv('timezone', location)
aio_username = secrets['aio_username']
aio_key = secrets['aio_key']
except KeyError:
raise KeyError("\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
location = secrets.get('timezone', location)
if location: if location:
print("Getting time for timezone", location) print("Getting time for timezone", location)
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location) api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
@ -70,7 +79,7 @@ def get_local_timestamp(location=None):
tzseconds += tzminutes * 60 tzseconds += tzminutes * 60
print(seconds + tzseconds, tzoffset, tzhours, tzminutes) print(seconds + tzseconds, tzoffset, tzhours, tzminutes)
except KeyError: except KeyError:
raise KeyError("Was unable to lookup the time, try setting secrets['timezone'] according to http://worldtimeapi.org/timezones") # pylint: disable=line-too-long raise KeyError("Was unable to lookup the time, try setting timezone in your settings.toml according to http://worldtimeapi.org/timezones") # pylint: disable=line-too-long
# now clean up # now clean up
response.close() response.close()
@ -157,7 +166,7 @@ class Clock(object):
# Update the time # Update the time
print("update the time") print("update the time")
self.update_time = int(now) self.update_time = int(now)
self.snapshot_time = get_local_timestamp(secrets['timezone']) self.snapshot_time = get_local_timestamp(getenv("timezone"))
self.current_time = time.localtime(self.snapshot_time) self.current_time = time.localtime(self.snapshot_time)
else: else:
self.current_time = time.localtime(int(now) - self.update_time + self.snapshot_time) self.current_time = time.localtime(int(now) - self.update_time + self.snapshot_time)
@ -185,8 +194,8 @@ class Clock(object):
def connected(client, userdata, flags, rc): def connected(client, userdata, flags, rc):
# This function will be called when the client is connected # This function will be called when the client is connected
# successfully to the broker. # successfully to the broker.
onoff_feed = secrets['aio_username'] + '/feeds/' + FEED_NAME onoff_feed = f"{aio_username}/feeds/{FEED_NAME}"
print('Connected to Adafruit IO! Listening for topic changes on %s' % onoff_feed) print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
# Subscribe to all changes on the onoff_feed. # Subscribe to all changes on the onoff_feed.
client.subscribe(onoff_feed) client.subscribe(onoff_feed)
@ -205,13 +214,6 @@ def message(client, topic, message):
############################################ ############################################
try:
from secrets import secrets
except ImportError:
print("""WiFi settings are kept in secrets.py, please add them there!
the secrets dictionary must contain 'ssid' and 'password' at a minimum""")
raise
esp32_cs = digitalio.DigitalInOut(board.ESP_CS) esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY) esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET) esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)
@ -257,7 +259,7 @@ for ap in esp.scan_networks():
print("Connecting to AP...") print("Connecting to AP...")
while not esp.is_connected: while not esp.is_connected:
try: try:
esp.connect_AP(secrets['ssid'], secrets['password']) esp.connect_AP(ssid, password)
except RuntimeError as e: except RuntimeError as e:
print("could not connect to AP, retrying: ",e) print("could not connect to AP, retrying: ",e)
continue continue
@ -265,14 +267,15 @@ print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address)) print("My IP address is", esp.pretty_ip(esp.ip_address))
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager( wifi = adafruit_esp32spi_wifimanager.WiFiManager(
esp, secrets, debug = True) esp, ssid, password, debug=True
)
# Set up a MiniMQTT Client # Set up a MiniMQTT Client
mqtt_client = MQTT(broker='io.adafruit.com', mqtt_client = MQTT(broker='io.adafruit.com',
username=secrets['aio_username'], username=aio_username,
password=secrets['aio_key'], password=aio_key,
network_manager=wifi, network_manager=wifi,
socket_pool=pool, socket_pool=pool,
ssl_context=ssl_context) ssl_context=ssl_context)

View file

@ -1,15 +0,0 @@
# SPDX-FileCopyrightText: 2019 Kattni Rembor 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
secrets = {
'ssid' : 'CHANGE ME',
'password' : 'CHANGE ME',
# leave blank or use timezone from # http://worldtimeapi.org/timezones
'timezone' : '',
'aio_username' : 'CHANGE ME',
'aio_key' : 'CHANGE ME',
}

View file

@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: 2019 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# This file is where you keep private settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
CIRCUITPY_WIFI_SSID="your-wifi-ssid"
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
ADAFRUIT_AIO_USERNAME="my_username"
ADAFRUIT_AIO_KEY="my_key"
timezone="" # leave blank or use timezone from # http://worldtimeapi.org/timezones

View file

@ -10,6 +10,8 @@ thermometer with Adafruit IO
Author: Brent Rubell for Adafruit Industries, 2019 Author: Brent Rubell for Adafruit Industries, 2019
""" """
from os import getenv
import time import time
import board import board
import neopixel import neopixel
@ -27,12 +29,20 @@ import thermometer_helper
# rate at which to refresh the pyportal screen, in seconds # rate at which to refresh the pyportal screen, in seconds
PYPORTAL_REFRESH = 2 PYPORTAL_REFRESH = 2
# Get wifi details and more from a secrets.py file # Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
try: # (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
from secrets import secrets ssid = getenv("CIRCUITPY_WIFI_SSID")
except ImportError: password = getenv("CIRCUITPY_WIFI_PASSWORD")
print("WiFi secrets are kept in secrets.py, please add them there!") aio_username = getenv("ADAFRUIT_AIO_USERNAME")
raise aio_key = getenv("ADAFRUIT_AIO_KEY")
if None in [ssid, password, aio_username, aio_key]:
raise RuntimeError(
"WiFi and Adafruit IO settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
)
# PyPortal ESP32 Setup # PyPortal ESP32 Setup
esp32_cs = DigitalInOut(board.ESP_CS) esp32_cs = DigitalInOut(board.ESP_CS)
@ -40,21 +50,11 @@ esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET) esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
try:
ADAFRUIT_IO_USER = secrets['aio_username']
ADAFRUIT_IO_KEY = secrets['aio_key']
except KeyError:
raise KeyError('To use this code, you need to include your Adafruit IO username \
and password in a secrets.py file on the CIRCUITPY drive.')
# Create an instance of the IO_HTTP client # Create an instance of the IO_HTTP client
io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi) io = IO_HTTP(aio_username, aio_key, wifi)
# Get the temperature feed from Adafruit IO # Get the temperature feed from Adafruit IO
temperature_feed = io.get_feed('temperature') temperature_feed = io.get_feed('temperature')

View file

@ -36,12 +36,11 @@ ALWAYS_ON = True
# How long to stay on if not in always_on mode # How long to stay on if not in always_on mode
ON_SECONDS = 60 ON_SECONDS = 60
# Get totp keys from a secrets.py file # Get totp_keys from a totp_keys.py file
try: try:
from secrets import secrets from totp_keys import totp_keys
except ImportError: except ImportError:
print("TOTP keys are kept in secrets.py, please add them there!") print("TOTP info not found in totp_keys.py, please add them there!")
raise
# Initialize PyPortal Display # Initialize PyPortal Display
display = board.DISPLAY display = board.DISPLAY
@ -228,13 +227,13 @@ mono_time = int(time.monotonic())
print("Monotonic time", mono_time) print("Monotonic time", mono_time)
# Add buttons to the interface # Add buttons to the interface
assert len(secrets['totp_keys']) < 6, "This code can only display 5 keys at a time" assert len(totp_keys) < 6, "This code can only display 5 keys at a time"
# generate buttons # generate buttons
buttons = [] buttons = []
btn_x = 5 btn_x = 5
for i in secrets['totp_keys']: for i in totp_keys:
button = Button(name=i[0], x=btn_x, button = Button(name=i[0], x=btn_x,
y=175, width=60, y=175, width=60,
height=60, label=i[0].strip(" "), height=60, label=i[0].strip(" "),
@ -264,7 +263,7 @@ splash.append(progress_bar)
countdown = ON_SECONDS countdown = ON_SECONDS
# current button state, defaults to first item in totp_keys # current button state, defaults to first item in totp_keys
current_button = secrets['totp_keys'][0][0] current_button = totp_keys[0][0]
buttons[0].selected = True buttons[0].selected = True
while ALWAYS_ON or (countdown > 0): while ALWAYS_ON or (countdown > 0):
@ -295,7 +294,7 @@ while ALWAYS_ON or (countdown > 0):
for i, b in enumerate(buttons): for i, b in enumerate(buttons):
if b.contains(p): if b.contains(p):
b.selected = True b.selected = True
for name, secret in secrets['totp_keys']: for name, secret in totp_keys:
# check if button name is the same as a key name # check if button name is the same as a key name
if b.name == name: if b.name == name:
current_button = name current_button = name
@ -305,7 +304,7 @@ while ALWAYS_ON or (countdown > 0):
else: else:
b.selected = False b.selected = False
else: else:
for name, secret in secrets['totp_keys']: for name, secret in totp_keys:
if current_button == name: if current_button == name:
# Generate OTP # Generate OTP
otp = generate_otp(unix_time // 30, secret) otp = generate_otp(unix_time // 30, secret)

View file

@ -1,14 +0,0 @@
# SPDX-FileCopyrightText: 2019 Brent Rubell 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
secrets = {
'totp_keys' : [("Discord ", "JBSWY3DPEHPK3PXP"),
("Gmail", "JBSWY3DPEHPK3PZP"),
("GitHub", "JBSWY5DZEHPK3PXP"),
("Adafruit", "JBSWY6DZEHPK3PXP"),
("Outlook", "JBSWY7DZEHPK3PXP")]
}

View file

@ -0,0 +1,14 @@
# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# This file contains totp codes!
# If you put them in the code you risk committing that info or sharing it
totp_keys = [
("Discord ", "JBSWY3DPEHPK3PXP"),
("Gmail", "JBSWY3DPEHPK3PZP"),
("GitHub", "JBSWY5DZEHPK3PXP"),
("Adafruit", "JBSWY6DZEHPK3PXP"),
("Outlook", "JBSWY7DZEHPK3PXP"),
]

View file

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from os import getenv
import time import time
import json import json
import board import board
@ -9,12 +10,17 @@ from adafruit_pyportal import PyPortal
from adafruit_bitmap_font import bitmap_font from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.label import Label from adafruit_display_text.label import Label
# Get wifi details and more from a secrets.py file # Get WiFi details, ensure these are setup in settings.toml
try: ssid = getenv("CIRCUITPY_WIFI_SSID")
from secrets import secrets password = getenv("CIRCUITPY_WIFI_PASSWORD")
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!") if None in [ssid, password]:
raise raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
#--| USER CONFIG |-------------------------- #--| USER CONFIG |--------------------------
STATION_ID = "0245" # tide location, find yours from admiralty website/ STATION_ID = "0245" # tide location, find yours from admiralty website/
@ -30,7 +36,7 @@ DATA_LOCATION = []
# determine the current working directory needed so we know where to find files # determine the current working directory needed so we know where to find files
cwd = ("/"+__file__).rsplit('/', 1)[0] cwd = ("/"+__file__).rsplit('/', 1)[0]
pyportal = PyPortal(url=DATA_SOURCE, pyportal = PyPortal(url=DATA_SOURCE,
headers={"Ocp-Apim-Subscription-Key":secrets['Ocp-Apim-Subscription-Key']}, headers={"Ocp-Apim-Subscription-Key": getenv('Ocp-Apim-Subscription-Key')},
json_path=DATA_LOCATION, json_path=DATA_LOCATION,
status_neopixel=board.NEOPIXEL, status_neopixel=board.NEOPIXEL,
default_bg=cwd+"/images/tides_bg.bmp") default_bg=cwd+"/images/tides_bg.bmp")

View file

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from os import getenv
import time import time
import json import json
import board import board
@ -10,12 +11,17 @@ from adafruit_pyportal import PyPortal
from adafruit_bitmap_font import bitmap_font from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.label import Label from adafruit_display_text.label import Label
# Get wifi details and more from a secrets.py file # Get WiFi details, ensure these are setup in settings.toml
try: ssid = getenv("CIRCUITPY_WIFI_SSID")
from secrets import secrets password = getenv("CIRCUITPY_WIFI_PASSWORD")
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!") if None in [ssid, password]:
raise raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
#--| USER CONFIG |-------------------------- #--| USER CONFIG |--------------------------
STATION_ID = "0245" # tide location, find yours from admiralty website STATION_ID = "0245" # tide location, find yours from admiralty website
@ -44,7 +50,7 @@ else:
bg_image_path = "/images/tides_bg_graph.bmp" bg_image_path = "/images/tides_bg_graph.bmp"
pyportal = PyPortal(url=DATA_SOURCE, pyportal = PyPortal(url=DATA_SOURCE,
headers={"Ocp-Apim-Subscription-Key":secrets['Ocp-Apim-Subscription-Key']}, headers={"Ocp-Apim-Subscription-Key": getenv('Ocp-Apim-Subscription-Key')},
json_path=DATA_LOCATION, json_path=DATA_LOCATION,
status_neopixel=board.NEOPIXEL, status_neopixel=board.NEOPIXEL,
default_bg=cwd+bg_image_path) default_bg=cwd+bg_image_path)

View file

@ -2,6 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from os import getenv
import time import time
from calendar import alarms from calendar import alarms
from calendar import timers from calendar import timers
@ -12,20 +13,25 @@ from adafruit_button import Button
from adafruit_pyportal import PyPortal from adafruit_pyportal import PyPortal
import openweather_graphics # pylint: disable=wrong-import-position import openweather_graphics # pylint: disable=wrong-import-position
# Get wifi details and more from a secrets.py file # Get WiFi details, ensure these are setup in settings.toml
try: ssid = getenv("CIRCUITPY_WIFI_SSID")
from secrets import secrets password = getenv("CIRCUITPY_WIFI_PASSWORD")
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!") if None in [ssid, password]:
raise raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
# Use cityname, country code where countrycode is ISO3166 format. # Use cityname, country code where countrycode is ISO3166 format.
# E.g. "New York, US" or "London, GB" # E.g. "New York, US" or "London, GB"
LOCATION = secrets['location'] LOCATION = getenv('location')
# Set up where we'll be fetching data from # Set up where we'll be fetching data from
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q=" + LOCATION DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q=" + LOCATION
DATA_SOURCE += "&appid="+secrets['openweather_token'] DATA_SOURCE += "&appid=" + getenv('openweather_token')
# You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22' # You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22'
DATA_LOCATION = [] DATA_LOCATION = []

View file

@ -1,15 +0,0 @@
# SPDX-FileCopyrightText: 2020 Liz Clark 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
secrets = {
'ssid' : 'your-ssid-here',
'password' : 'your-password-here',
'openweather_token' : 'your-openweather-token-here',
'aio_username' : "your-aio-username-here",
'aio_key' : 'your-aio-key-here',
'location' : 'New York, US'
}

View file

@ -0,0 +1,14 @@
# SPDX-FileCopyrightText: 2020 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# This file is where you keep private settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
CIRCUITPY_WIFI_SSID="your-wifi-ssid"
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
ADAFRUIT_AIO_USERNAME="my_username"
ADAFRUIT_AIO_KEY="my_key"
timezone="America/New_York" # http://worldtimeapi.org/timezones
openweather_token="my_openweather_token"
location="New York, US"

View file

@ -14,11 +14,11 @@ analogin = AnalogIn(board.LIGHT)
cwd = ("/"+__file__).rsplit('/', 1)[0] cwd = ("/"+__file__).rsplit('/', 1)[0]
laura = (cwd+"/laura.bmp") laura = cwd+"/laura.bmp"
woodsman = (cwd+"/woodsman.bmp") woodsman = cwd+"/woodsman.bmp"
gottaLight = (cwd+"/gottaLight.wav") gottaLight = cwd+"/gottaLight.wav"
pyportal = PyPortal(default_bg=laura) pyportal = PyPortal(default_bg=laura)

View file

@ -16,6 +16,7 @@ Licensed under the MIT license.
All text above must be included in any redistribution. All text above must be included in any redistribution.
""" """
from os import getenv
import time import time
import json import json
import board import board
@ -25,12 +26,17 @@ from adafruit_display_shapes.rect import Rect
from adafruit_display_text.Label import Label from adafruit_display_text.Label import Label
from adafruit_bitmap_font import bitmap_font from adafruit_bitmap_font import bitmap_font
try: # Get WiFi details, ensure these are setup in settings.toml
from secrets import secrets ssid = getenv("CIRCUITPY_WIFI_SSID")
except ImportError: password = getenv("CIRCUITPY_WIFI_PASSWORD")
print("""WiFi settings are kept in secrets.py, please add them there!
the secrets dictionary must contain 'ssid' and 'password' at a minimum""") if None in [ssid, password]:
raise raise RuntimeError(
"WiFi settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"at a minimum."
)
MAX_BAR_HEIGHT = 160 MAX_BAR_HEIGHT = 160
MARGIN = 10 MARGIN = 10
@ -48,7 +54,7 @@ CAPTION_FONT_FILE = cwd+'/fonts/Helvetica-Bold-16.bdf'
BAR_FONT_FILE = cwd+'/fonts/Arial-Bold-12.bdf' BAR_FONT_FILE = cwd+'/fonts/Arial-Bold-12.bdf'
#pylint:disable=line-too-long #pylint:disable=line-too-long
url = 'https://enviro.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/{0}/JSON'.format(secrets['zip']) url = f"https://enviro.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/{getenv('zip')}/JSON"
#pylint:enable=line-too-long #pylint:enable=line-too-long
def extract_hour(date_time): def extract_hour(date_time):

View file

@ -1,12 +0,0 @@
# SPDX-FileCopyrightText: 2019 Kattni Rembor 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
secrets = {
'ssid' : 'CHANGE ME',
'password' : 'CHANGE ME',
'zip' : 'CHANGE ME',
}

View file

@ -2,8 +2,9 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
# This file is where you keep secret settings, passwords, and tokens! # This file is where you keep private settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it # If you put them in the code you risk committing that info or sharing it
secrets = { CIRCUITPY_WIFI_SSID="your-wifi-ssid"
} CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
zip="CHANGE ME"

View file

@ -1 +0,0 @@
PyPortal_User_Interface/code.py 128: Line too long (117/100) (line-too-long)

View file

@ -1,11 +0,0 @@
# SPDX-FileCopyrightText: 2020 Anne Barela for Adafruit Industries
#
# SPDX-License-Identifier: MIT
secrets = {
'ssid' : '_your_wifi_ssid_',
'password' : '_your_wifi_password_',
'broker' : '_your_mqtt_broker_url_or_ip',
'user' : '_your_mqtt_broker_username_',
'pass' : '_your_mqtt_broker_password_'
}

View file

@ -2,7 +2,7 @@
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
import os from os import getenv
import time import time
import board import board
@ -21,6 +21,21 @@ from adafruit_pyportal import PyPortal
from adafruit_seesaw.seesaw import Seesaw from adafruit_seesaw.seesaw import Seesaw
from simpleio import map_range from simpleio import map_range
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")
if None in [ssid, password, aio_username, aio_key]:
raise RuntimeError(
"WiFi and Adafruit IO settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
)
#---| User Config |--------------- #---| User Config |---------------
# How often to poll the soil sensor, in seconds # How often to poll the soil sensor, in seconds
@ -52,11 +67,6 @@ wav_water_low = "/sounds/water-low.wav"
# the current working directory (where this file is) # the current working directory (where this file is)
cwd = ("/"+__file__).rsplit('/', 1)[0] cwd = ("/"+__file__).rsplit('/', 1)[0]
secrets = {
"ssid" : os.getenv("CIRCUITPY_WIFI_SSID"),
"password" : os.getenv("CIRCUITPY_WIFI_PASSWORD"),
}
# Set up i2c bus # Set up i2c bus
i2c_bus = busio.I2C(board.SCL, board.SDA) i2c_bus = busio.I2C(board.SCL, board.SDA)
@ -70,8 +80,8 @@ esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
# Initialize PyPortal Display # Initialize PyPortal Display
display = board.DISPLAY display = board.DISPLAY
@ -190,8 +200,8 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
# Initialize a new MQTT Client object # Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(broker="io.adafruit.com", mqtt_client = MQTT.MQTT(broker="io.adafruit.com",
username=os.getenv("AIO_USERNAME"), username=aio_username,
password=os.getenv("AIO_KEY"), password=aio_key,
socket_pool=pool, socket_pool=pool,
ssl_context=ssl_context) ssl_context=ssl_context)

View file

@ -10,6 +10,8 @@ Adafruit IO
Author: Brent Rubell for Adafruit Industries, 2019 Author: Brent Rubell for Adafruit Industries, 2019
""" """
from os import getenv
import time import time
import board import board
import neopixel import neopixel
@ -38,12 +40,20 @@ anemometer_max_volts = 2.0
min_wind_speed = 0.0 min_wind_speed = 0.0
max_wind_speed = 32.4 max_wind_speed = 32.4
# Get wifi details and more from a secrets.py file # Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
try: # (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
from secrets import secrets ssid = getenv("CIRCUITPY_WIFI_SSID")
except ImportError: password = getenv("CIRCUITPY_WIFI_PASSWORD")
print("WiFi secrets are kept in secrets.py, please add them there!") aio_username = getenv("ADAFRUIT_AIO_USERNAME")
raise aio_key = getenv("ADAFRUIT_AIO_KEY")
if None in [ssid, password, aio_username, aio_key]:
raise RuntimeError(
"WiFi and Adafruit IO settings are kept in settings.toml, "
"please add them there. The settings file must contain "
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
)
# PyPortal ESP32 Setup # PyPortal ESP32 Setup
esp32_cs = DigitalInOut(board.ESP_CS) esp32_cs = DigitalInOut(board.ESP_CS)
@ -51,14 +61,8 @@ esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET) esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel)
# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
ADAFRUIT_IO_USER = secrets['aio_username']
ADAFRUIT_IO_KEY = secrets['aio_key']
# Create an instance of the Adafruit IO HTTP client # Create an instance of the Adafruit IO HTTP client
io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi) io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)