Skip to content

Commit 2be324f

Browse files
authored
Merge pull request #30 from justmobilize/remove-secrets-usage
Remove secrets usage
2 parents 9fc9d99 + adb5b07 commit 2be324f

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

adafruit_gc_iot_core.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ class Cloud_Core:
354354
"""CircuitPython Google Cloud IoT Core module.
355355
356356
:param ESP_SPIcontrol esp: ESP32SPI object.
357-
:param dict secrets: Secrets.py file.
357+
:param dict secrets: dictionary of settings.
358358
:param bool log: Enable Cloud_Core logging, defaults to False.
359359
"""
360360

@@ -364,7 +364,7 @@ class Cloud_Core:
364364
logger: Optional[logging.Logger]
365365

366366
_esp: Optional[ESP32SPI.ESP_SPIcontrol]
367-
_secrets: Optional[Dict[str, Any]]
367+
_settings: Optional[Dict[str, Any]]
368368
_proj_id: str
369369
_region: str
370370
_reg_id: str
@@ -380,22 +380,20 @@ def __init__(
380380
self._esp = esp
381381
# Validate Secrets
382382
if secrets and hasattr(secrets, "keys"):
383-
self._secrets = secrets
383+
self._settings = secrets
384384
else:
385-
raise AttributeError(
386-
"Project settings are kept in secrets.py, please add them there!"
387-
)
385+
raise AttributeError("Project settings must be passed in")
388386
self.logger = None
389387
if log is True:
390388
self.logger = logging.getLogger("log")
391389
self.logger.addHandler(logging.StreamHandler())
392390
self.logger.setLevel(logging.DEBUG)
393-
# Configuration, from secrets file
394-
self._proj_id = secrets["project_id"]
395-
self._region = secrets["cloud_region"]
396-
self._reg_id = secrets["registry_id"]
397-
self._device_id = secrets["device_id"]
398-
self._private_key = secrets["private_key"]
391+
# Configuration
392+
self._proj_id = self._settings["project_id"]
393+
self._region = self._settings["cloud_region"]
394+
self._reg_id = self._settings["registry_id"]
395+
self._device_id = self._settings["device_id"]
396+
self._private_key = self._settings["private_key"]
399397
self.broker = "mqtt.googleapis.com"
400398
self.username = b"unused"
401399
self.cid = self.client_id

examples/gc_iot_core_simpletest.py

Lines changed: 28 additions & 14 deletions
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 board
67
import busio
@@ -13,14 +14,17 @@
1314
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1415
from adafruit_gc_iot_core import Cloud_Core, MQTT_API
1516

16-
### WiFi ###
17+
# Get WiFi details and Google Cloud keys, ensure these are setup in settings.toml
18+
ssid = getenv("CIRCUITPY_WIFI_SSID")
19+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
20+
cloud_region = getenv("cloud_region")
21+
device_id = getenv("device_id")
22+
private_key = getenv("private_key")
23+
project_id = getenv("project_id")
24+
registry_id = getenv("registry_id")
25+
jwt = getenv("jwt")
1726

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
27+
### WiFi ###
2428

2529
# If you are using a board with pre-defined ESP32 Pins:
2630
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -35,19 +39,21 @@
3539
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3640
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3741
"""Use below for Most Boards"""
38-
status_light = neopixel.NeoPixel(
42+
status_pixel = neopixel.NeoPixel(
3943
board.NEOPIXEL, 1, brightness=0.2
4044
) # Uncomment for Most Boards
4145
"""Uncomment below for ItsyBitsy M4"""
42-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
46+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
4347
# Uncomment below for an externally defined RGB LED
4448
# import adafruit_rgbled
4549
# from adafruit_esp32spi import PWMOut
4650
# RED_LED = PWMOut.PWMOut(esp, 26)
4751
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4852
# BLUE_LED = PWMOut.PWMOut(esp, 25)
49-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
50-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
53+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
54+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(
55+
esp, ssid, password, status_pixel=status_pixel
56+
)
5157

5258
### Code ###
5359

@@ -101,7 +107,14 @@ def message(client, topic, msg):
101107
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
102108

103109
# Initialize Google Cloud IoT Core interface
104-
google_iot = Cloud_Core(esp, secrets)
110+
settings = {
111+
cloud_region: cloud_region,
112+
device_id: device_id,
113+
private_key: private_key,
114+
project_id: project_id,
115+
registry_id: registry_id,
116+
}
117+
google_iot = Cloud_Core(esp, settings)
105118

106119
# Optional JSON-Web-Token (JWT) Generation
107120
# print("Generating JWT...")
@@ -112,7 +125,7 @@ def message(client, topic, msg):
112125
client = MQTT.MQTT(
113126
broker=google_iot.broker,
114127
username=google_iot.username,
115-
password=secrets["jwt"],
128+
password=jwt,
116129
client_id=google_iot.cid,
117130
socket_pool=pool,
118131
ssl_context=ssl_context,
@@ -129,7 +142,8 @@ def message(client, topic, msg):
129142
google_mqtt.on_publish = publish
130143
google_mqtt.on_message = message
131144

132-
print("Attempting to connect to %s" % client.broker)
145+
146+
print(f"Attempting to connect to {client.broker}")
133147
google_mqtt.connect()
134148

135149
# Pump the message loop forever, all events are

0 commit comments

Comments
 (0)