update settings to secrets

This commit is contained in:
ladyada 2019-02-23 17:23:51 -05:00
parent abf17e70b8
commit 1434532c71
7 changed files with 25 additions and 23 deletions

View file

@ -410,10 +410,10 @@ class ESP_SPIcontrol: # pylint: disable=too-many-public-methods
self.reset() self.reset()
return False return False
def connect(self, settings): def connect(self, secrets):
"""Connect to an access point using a settings dictionary """Connect to an access point using a secrets dictionary
that contains a 'ssid' and 'password' entry""" that contains a 'ssid' and 'password' entry"""
self.connect_AP(settings['ssid'], settings['password']) self.connect_AP(secrets['ssid'], secrets['password'])
def connect_AP(self, ssid, password): # pylint: disable=invalid-name def connect_AP(self, ssid, password): # pylint: disable=invalid-name
"""Connect to an access point with given name and password. """Connect to an access point with given name and password.

View file

@ -39,10 +39,10 @@ class ESPSPI_WiFiManager:
""" """
A class to help manage the Wifi connection A class to help manage the Wifi connection
""" """
def __init__(self, esp, settings, status_neopixel=None, attempts=2): def __init__(self, esp, secrets, status_neopixel=None, attempts=2):
""" """
:param ESP_SPIcontrol esp: The ESP object we are using :param ESP_SPIcontrol esp: The ESP object we are using
:param dict settings: The WiFi and Adafruit IO Settings (See examples) :param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2) :param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
:param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None) :param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None)
:type status_neopixel: Pin :type status_neopixel: Pin
@ -50,8 +50,8 @@ class ESPSPI_WiFiManager:
# Read the settings # Read the settings
self._esp = esp self._esp = esp
self.debug = False self.debug = False
self.ssid = settings['ssid'] self.ssid = secrets['ssid']
self.password = settings['password'] self.password = secrets['password']
self.attempts = attempts self.attempts = attempts
requests.set_interface(self._esp) requests.set_interface(self._esp)
if status_neopixel: if status_neopixel:

View file

@ -8,11 +8,11 @@ from adafruit_esp32spi import adafruit_esp32spi_wifimanager
print("ESP32 SPI webclient test") print("ESP32 SPI webclient test")
# Get wifi details and more from a settings.py file # Get wifi details and more from a secrets.py file
try: try:
from esp32spi_settings import settings from esp32spi_secrets import secrets
except ImportError: except ImportError:
print("WiFi settings are kept in esp32spi_settings.py, please add them there!") print("WiFi secrets are kept in secrets.py, please add them there!")
raise raise
esp32_cs = DigitalInOut(board.D9) esp32_cs = DigitalInOut(board.D9)
@ -20,7 +20,7 @@ esp32_ready = DigitalInOut(board.D10)
esp32_reset = DigitalInOut(board.D5) esp32_reset = DigitalInOut(board.D5)
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)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, settings, board.NEOPIXEL) wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
counter = 0 counter = 0
while True: while True:
@ -30,8 +30,8 @@ while True:
feed = 'test' feed = 'test'
payload = {'value':data} payload = {'value':data}
response = wifi.post( response = wifi.post(
"https://io.adafruit.com/api/v2/"+settings['aio_username']+"/feeds/"+feed+"/data", "https://io.adafruit.com/api/v2/"+secrets['aio_username']+"/feeds/"+feed+"/data",
json=payload,headers={bytes("X-AIO-KEY","utf-8"):bytes(settings['aio_key'],"utf-8")}) json=payload,headers={bytes("X-AIO-KEY","utf-8"):bytes(secrets['aio_key'],"utf-8")})
print(response.json()) print(response.json())
response.close() response.close()
counter = counter + 1 counter = counter + 1

View file

@ -9,11 +9,11 @@ from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import neopixel import neopixel
import adafruit_fancyled.adafruit_fancyled as fancy import adafruit_fancyled.adafruit_fancyled as fancy
# Get wifi details and more from a settings.py file # Get wifi details and more from a secrets.py file
try: try:
from esp32spi_settings import settings from esp32spi_secrets import secrets
except ImportError: except ImportError:
print("WiFi settings are kept in esp32spi_settings.py, please add them there!") print("WiFi secrets are kept in secrets.py, please add them there!")
raise raise
print("ESP32 SPI webclient test") print("ESP32 SPI webclient test")
@ -26,7 +26,7 @@ esp32_ready = DigitalInOut(board.D10)
esp32_reset = DigitalInOut(board.D5) esp32_reset = DigitalInOut(board.D5)
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)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, settings, board.NEOPIXEL) wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
# neopixels # neopixels
pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3) pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3)

View file

@ -7,11 +7,11 @@ from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import rtc import rtc
# Get wifi details and more from a settings.py file # Get wifi details and more from a secrets.py file
try: try:
from settings import settings from secrets import secrets
except ImportError: except ImportError:
print("WiFi settings are kept in settings.py, please add them there!") print("WiFi secrets are kept in secrets.py, please add them there!")
raise raise
print("ESP32 local time") print("ESP32 local time")
@ -24,7 +24,7 @@ esp32_gpio0 = DigitalInOut(microcontroller.pin.PB15)
esp32_reset = DigitalInOut(microcontroller.pin.PB17) esp32_reset = DigitalInOut(microcontroller.pin.PB17)
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)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, settings, board.NEOPIXEL) wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, board.NEOPIXEL)
the_rtc = rtc.RTC() the_rtc = rtc.RTC()

View file

@ -1,7 +1,7 @@
# This file is where you keep secret settings, passwords, and tokens! # 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 # If you put them in the code you risk committing that info or sharing it
settings = { secrets = {
'ssid' : 'yourssid', 'ssid' : 'yourssid',
'password' : 'yourpassword', 'password' : 'yourpassword',
'timezone' : -5, # this is offset from UTC 'timezone' : -5, # this is offset from UTC

View file

@ -23,10 +23,12 @@ if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode") print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version) print("Firmware vers.", esp.firmware_version)
print("MAC addr:", [hex(i) for i in esp.MAC_address]) print("MAC addr:", [hex(i) for i in esp.MAC_address])
for ap in esp.scan_networks(): for ap in esp.scan_networks():
print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi'])) print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))
print("Connecting to AP...") print("Connecting to AP...")
esp.connect_AP(b'adafruit', b'ffffffff') esp.connect_AP(b'MY_SSID_NAME', b'MY_SSID_PASSWORD')
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi) 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))
print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))) print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")))