Skip to content

Commit 359fb50

Browse files
authored
Merge pull request #28 from justmobilize/remove-secrets-usage
Remove secrets usage
2 parents 5f802e5 + 767339b commit 359fb50

File tree

3 files changed

+63
-59
lines changed

3 files changed

+63
-59
lines changed

examples/aws_iot_native_networking.py

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
from os import getenv
45
import time
5-
import ssl
66
import json
7-
import socketpool
87
import wifi
8+
import adafruit_connection_manager
99
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1010
from adafruit_aws_iot import MQTT_CLIENT
1111

12-
# Add a secrets.py to your filesystem that has a dictionary called "secrets". DO NOT share that
13-
# file or commit it into Git or other source control. The "secrets" dictionary should have the
14-
# following keys:
15-
# "ssid" - Your WiFi ssid
16-
# "password" - Your WiFi password
17-
# "device_cert_path" - Path to the Device Certificate from AWS IoT ("<THING_NAME>.cert.pem")
18-
# "device_key_path" - Path to the RSA Private Key from AWS IoT ("<THING_NAME>.private.key")
19-
# "broker" - The endpoint for the AWS IoT broker ("<PREFIX>.iot.<REGION>.amazonaws.com")
20-
# "client_id" - The client id. Your device's Policy needs to allow this client ("basicPubSub")
21-
#
22-
# pylint: disable=no-name-in-module,wrong-import-order
23-
try:
24-
from secrets import secrets
25-
except ImportError:
26-
print("WiFi secrets are kept in secrets.py, please add them there!")
27-
raise
12+
# Add a settings.toml to your filesystem. DO NOT share that file or commit it into
13+
# Git or other source control. The file should have the following settings:
14+
"""
15+
CIRCUITPY_WIFI_SSID="Your WiFi ssid"
16+
CIRCUITPY_WIFI_PASSWORD="Your WiFi password"
17+
device_cert_path="<THING_NAME>.cert.pem" # Path to the Device Certificate from AWS IoT
18+
device_key_path="<THING_NAME>.private.key" # Path to the RSA Private Key from AWS IoT
19+
broker="<PREFIX>.iot.<REGION>.amazonaws.com" # The endpoint for the AWS IoT broker
20+
client_id="client_id" # The client id. Your device's Policy needs to allow this client
21+
"""
22+
23+
# Get WiFi details and AWS keys, ensure these are setup in settings.toml
24+
ssid = getenv("CIRCUITPY_WIFI_SSID")
25+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
26+
device_cert_path = getenv("device_cert_path")
27+
device_key_path = getenv("device_key_path")
28+
broker = getenv("broker")
29+
client_id = getenv("client_id")
2830

2931
### Code ###
3032

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

7880

79-
print("Connecting to %s" % secrets["ssid"])
80-
wifi.radio.connect(secrets["ssid"], secrets["password"])
81-
print("Connected to %s!" % secrets["ssid"])
81+
print(f"Connecting to {ssid}")
82+
wifi.radio.connect(ssid, password)
83+
print(f"Connected to {ssid}!")
8284

8385
# Create a socket pool
84-
pool = socketpool.SocketPool(wifi.radio)
85-
ssl_context = ssl.create_default_context()
86+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
87+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
8688

8789
# Set AWS Device Certificate and AWS RSA Private Key
88-
ssl_context.load_cert_chain(
89-
certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"]
90-
)
90+
ssl_context.load_cert_chain(certfile=device_cert_path, keyfile=device_key_path)
9191

9292
# Set up a MiniMQTT Client
9393
mqtt_client = MQTT.MQTT(
94-
broker=secrets["broker"],
95-
client_id=secrets["client_id"],
94+
broker=broker,
95+
client_id=client_id,
9696
is_ssl=True,
9797
socket_pool=pool,
9898
ssl_context=ssl_context,
@@ -109,7 +109,7 @@ def message(client, topic, msg):
109109
aws_iot.on_publish = publish
110110
aws_iot.on_message = message
111111

112-
print("Attempting to connect to %s" % mqtt_client.broker)
112+
print(f"Attempting to connect to {mqtt_client.broker}")
113113
aws_iot.connect()
114114

115115
# Start a blocking message loop...

examples/aws_iot_shadows.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
from os import getenv
45
import time
56
import json
67
import board
@@ -13,14 +14,11 @@
1314
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1415
from adafruit_aws_iot import MQTT_CLIENT
1516

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

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

39+
### WiFi ###
40+
4141
# If you are using a board with pre-defined ESP32 Pins:
4242
esp32_cs = DigitalInOut(board.ESP_CS)
4343
esp32_ready = DigitalInOut(board.ESP_BUSY)
@@ -57,19 +57,21 @@
5757
), "Please update nina-fw to >=1.4.0."
5858

5959
# Use below for Most Boards
60-
status_light = neopixel.NeoPixel(
60+
status_pixel = neopixel.NeoPixel(
6161
board.NEOPIXEL, 1, brightness=0.2
6262
) # Uncomment for Most Boards
6363
# Uncomment below for ItsyBitsy M4
64-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
64+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
6565
# Uncomment below for an externally defined RGB LED
6666
# import adafruit_rgbled
6767
# from adafruit_esp32spi import PWMOut
6868
# RED_LED = PWMOut.PWMOut(esp, 26)
6969
# GREEN_LED = PWMOut.PWMOut(esp, 27)
7070
# BLUE_LED = PWMOut.PWMOut(esp, 25)
71-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
72-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
71+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
72+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(
73+
esp, ssid, password, status_pixel=status_pixel
74+
)
7375

7476
### Code ###
7577

@@ -139,8 +141,8 @@ def message(client, topic, msg):
139141

140142
# Set up a new MiniMQTT Client
141143
client = MQTT.MQTT(
142-
broker=secrets["broker"],
143-
client_id=secrets["client_id"],
144+
broker=broker,
145+
client_id=client_id,
144146
is_ssl=True,
145147
socket_pool=pool,
146148
ssl_context=ssl_context,
@@ -157,7 +159,7 @@ def message(client, topic, msg):
157159
aws_iot.on_publish = publish
158160
aws_iot.on_message = message
159161

160-
print("Attempting to connect to %s" % client.broker)
162+
print(f"Attempting to connect to {client.broker}")
161163
aws_iot.connect()
162164

163165
# Pump the message loop forever, all events

examples/aws_iot_simpletest.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
from os import getenv
45
import time
56
import json
67
import board
@@ -13,14 +14,11 @@
1314
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1415
from adafruit_aws_iot import MQTT_CLIENT
1516

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

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

39+
### WiFi ###
40+
4141
# If you are using a board with pre-defined ESP32 Pins:
4242
esp32_cs = DigitalInOut(board.ESP_CS)
4343
esp32_ready = DigitalInOut(board.ESP_BUSY)
@@ -57,19 +57,21 @@
5757
), "Please update nina-fw to >=1.4.0."
5858

5959
# Use below for Most Boards
60-
status_light = neopixel.NeoPixel(
60+
status_pixel = neopixel.NeoPixel(
6161
board.NEOPIXEL, 1, brightness=0.2
6262
) # Uncomment for Most Boards
6363
# Uncomment below for ItsyBitsy M4
64-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
64+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
6565
# Uncomment below for an externally defined RGB LED
6666
# import adafruit_rgbled
6767
# from adafruit_esp32spi import PWMOut
6868
# RED_LED = PWMOut.PWMOut(esp, 26)
6969
# GREEN_LED = PWMOut.PWMOut(esp, 27)
7070
# BLUE_LED = PWMOut.PWMOut(esp, 25)
71-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
72-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
71+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
72+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(
73+
esp, ssid, password, status_pixel=status_pixel
74+
)
7375

7476
### Code ###
7577

@@ -136,8 +138,8 @@ def message(client, topic, msg):
136138

137139
# Set up a new MiniMQTT Client
138140
client = MQTT.MQTT(
139-
broker=secrets["broker"],
140-
client_id=secrets["client_id"],
141+
broker=broker,
142+
client_id=client_id,
141143
is_ssl=True,
142144
socket_pool=pool,
143145
ssl_context=ssl_context,
@@ -154,7 +156,7 @@ def message(client, topic, msg):
154156
aws_iot.on_publish = publish
155157
aws_iot.on_message = message
156158

157-
print("Attempting to connect to %s" % client.broker)
159+
print(f"Attempting to connect to {client.broker}")
158160
aws_iot.connect()
159161

160162
# Pump the message loop forever, all events

0 commit comments

Comments
 (0)