Skip to content

Remove secrets usage #28

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

Merged
merged 2 commits into from
Feb 25, 2025
Merged
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
58 changes: 29 additions & 29 deletions examples/aws_iot_native_networking.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

from os import getenv
import time
import ssl
import json
import socketpool
import wifi
import adafruit_connection_manager
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_aws_iot import MQTT_CLIENT

# Add a secrets.py to your filesystem that has a dictionary called "secrets". DO NOT share that
# file or commit it into Git or other source control. The "secrets" dictionary should have the
# following keys:
# "ssid" - Your WiFi ssid
# "password" - Your WiFi password
# "device_cert_path" - Path to the Device Certificate from AWS IoT ("<THING_NAME>.cert.pem")
# "device_key_path" - Path to the RSA Private Key from AWS IoT ("<THING_NAME>.private.key")
# "broker" - The endpoint for the AWS IoT broker ("<PREFIX>.iot.<REGION>.amazonaws.com")
# "client_id" - The client id. Your device's Policy needs to allow this client ("basicPubSub")
#
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Add a settings.toml to your filesystem. DO NOT share that file or commit it into
# Git or other source control. The file should have the following settings:
"""
CIRCUITPY_WIFI_SSID="Your WiFi ssid"
CIRCUITPY_WIFI_PASSWORD="Your WiFi password"
device_cert_path="<THING_NAME>.cert.pem" # Path to the Device Certificate from AWS IoT
device_key_path="<THING_NAME>.private.key" # Path to the RSA Private Key from AWS IoT
broker="<PREFIX>.iot.<REGION>.amazonaws.com" # The endpoint for the AWS IoT broker
client_id="client_id" # The client id. Your device's Policy needs to allow this client
"""
Comment on lines +14 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good but I don't understand why you put it in triple quotes instead of using comments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you can do a direct copy and place it in your settings.toml. Happy to change it back to comments if you prefer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a good idea!


# Get WiFi details and AWS keys, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
device_cert_path = getenv("device_cert_path")
device_key_path = getenv("device_key_path")
broker = getenv("broker")
client_id = getenv("client_id")

### Code ###

Expand Down Expand Up @@ -76,23 +78,21 @@ def message(client, topic, msg):
print("Message from {}: {}".format(topic, msg))


print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
print(f"Connecting to {ssid}")
wifi.radio.connect(ssid, password)
print(f"Connected to {ssid}!")

# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
ssl_context = ssl.create_default_context()
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)

# Set AWS Device Certificate and AWS RSA Private Key
ssl_context.load_cert_chain(
certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"]
)
ssl_context.load_cert_chain(certfile=device_cert_path, keyfile=device_key_path)

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=secrets["broker"],
client_id=secrets["client_id"],
broker=broker,
client_id=client_id,
is_ssl=True,
socket_pool=pool,
ssl_context=ssl_context,
Expand All @@ -109,7 +109,7 @@ def message(client, topic, msg):
aws_iot.on_publish = publish
aws_iot.on_message = message

print("Attempting to connect to %s" % mqtt_client.broker)
print(f"Attempting to connect to {mqtt_client.broker}")
aws_iot.connect()

# Start a blocking message loop...
Expand Down
32 changes: 17 additions & 15 deletions examples/aws_iot_shadows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

from os import getenv
import time
import json
import board
Expand All @@ -13,14 +14,11 @@
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_aws_iot import MQTT_CLIENT

### WiFi ###

# 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
# Get WiFi details and AWS keys, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
broker = getenv("broker")
client_id = getenv("client_id")

# Get device certificate
try:
Expand All @@ -38,6 +36,8 @@
print("Certificate (private.pem.key) not found on CIRCUITPY filesystem.")
raise

### WiFi ###

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
Expand All @@ -57,19 +57,21 @@
), "Please update nina-fw to >=1.4.0."

# Use below for Most Boards
status_light = neopixel.NeoPixel(
status_pixel = neopixel.NeoPixel(
board.NEOPIXEL, 1, brightness=0.2
) # Uncomment for Most Boards
# Uncomment below for ItsyBitsy M4
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Uncomment below for an externally defined RGB LED
# import adafruit_rgbled
# from adafruit_esp32spi import PWMOut
# RED_LED = PWMOut.PWMOut(esp, 26)
# 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)
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
wifi = adafruit_esp32spi_wifimanager.WiFiManager(
esp, ssid, password, status_pixel=status_pixel
)

### Code ###

Expand Down Expand Up @@ -139,8 +141,8 @@ def message(client, topic, msg):

# Set up a new MiniMQTT Client
client = MQTT.MQTT(
broker=secrets["broker"],
client_id=secrets["client_id"],
broker=broker,
client_id=client_id,
is_ssl=True,
socket_pool=pool,
ssl_context=ssl_context,
Expand All @@ -157,7 +159,7 @@ def message(client, topic, msg):
aws_iot.on_publish = publish
aws_iot.on_message = message

print("Attempting to connect to %s" % client.broker)
print(f"Attempting to connect to {client.broker}")
aws_iot.connect()

# Pump the message loop forever, all events
Expand Down
32 changes: 17 additions & 15 deletions examples/aws_iot_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

from os import getenv
import time
import json
import board
Expand All @@ -13,14 +14,11 @@
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_aws_iot import MQTT_CLIENT

### WiFi ###

# 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
# Get WiFi details and AWS keys, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
broker = getenv("broker")
client_id = getenv("client_id")

# Get device certificate
try:
Expand All @@ -38,6 +36,8 @@
print("Certificate (private.pem.key) not found on CIRCUITPY filesystem.")
raise

### WiFi ###

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
Expand All @@ -57,19 +57,21 @@
), "Please update nina-fw to >=1.4.0."

# Use below for Most Boards
status_light = neopixel.NeoPixel(
status_pixel = neopixel.NeoPixel(
board.NEOPIXEL, 1, brightness=0.2
) # Uncomment for Most Boards
# Uncomment below for ItsyBitsy M4
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Uncomment below for an externally defined RGB LED
# import adafruit_rgbled
# from adafruit_esp32spi import PWMOut
# RED_LED = PWMOut.PWMOut(esp, 26)
# 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)
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
wifi = adafruit_esp32spi_wifimanager.WiFiManager(
esp, ssid, password, status_pixel=status_pixel
)

### Code ###

Expand Down Expand Up @@ -136,8 +138,8 @@ def message(client, topic, msg):

# Set up a new MiniMQTT Client
client = MQTT.MQTT(
broker=secrets["broker"],
client_id=secrets["client_id"],
broker=broker,
client_id=client_id,
is_ssl=True,
socket_pool=pool,
ssl_context=ssl_context,
Expand All @@ -154,7 +156,7 @@ def message(client, topic, msg):
aws_iot.on_publish = publish
aws_iot.on_message = message

print("Attempting to connect to %s" % client.broker)
print(f"Attempting to connect to {client.broker}")
aws_iot.connect()

# Pump the message loop forever, all events
Expand Down