From 3ff3113cb534df80df84ba06c1a49b009e6761c6 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 13:55:11 -0800 Subject: [PATCH 1/2] Remove secrets usage --- adafruit_gc_iot_core.py | 22 +++++++--------- examples/gc_iot_core_simpletest.py | 42 ++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/adafruit_gc_iot_core.py b/adafruit_gc_iot_core.py index 51cf7b5..64c59e0 100644 --- a/adafruit_gc_iot_core.py +++ b/adafruit_gc_iot_core.py @@ -354,7 +354,7 @@ class Cloud_Core: """CircuitPython Google Cloud IoT Core module. :param ESP_SPIcontrol esp: ESP32SPI object. - :param dict secrets: Secrets.py file. + :param dict secrets: dictonary of settings. :param bool log: Enable Cloud_Core logging, defaults to False. """ @@ -364,7 +364,7 @@ class Cloud_Core: logger: Optional[logging.Logger] _esp: Optional[ESP32SPI.ESP_SPIcontrol] - _secrets: Optional[Dict[str, Any]] + _settings: Optional[Dict[str, Any]] _proj_id: str _region: str _reg_id: str @@ -380,22 +380,20 @@ def __init__( self._esp = esp # Validate Secrets if secrets and hasattr(secrets, "keys"): - self._secrets = secrets + self._settings = secrets else: - raise AttributeError( - "Project settings are kept in secrets.py, please add them there!" - ) + raise AttributeError("Project settings must be passed in") self.logger = None if log is True: self.logger = logging.getLogger("log") self.logger.addHandler(logging.StreamHandler()) self.logger.setLevel(logging.DEBUG) - # Configuration, from secrets file - self._proj_id = secrets["project_id"] - self._region = secrets["cloud_region"] - self._reg_id = secrets["registry_id"] - self._device_id = secrets["device_id"] - self._private_key = secrets["private_key"] + # Configuration + self._proj_id = self._settings["project_id"] + self._region = self._settings["cloud_region"] + self._reg_id = self._settings["registry_id"] + self._device_id = self._settings["device_id"] + self._private_key = self._settings["private_key"] self.broker = "mqtt.googleapis.com" self.username = b"unused" self.cid = self.client_id diff --git a/examples/gc_iot_core_simpletest.py b/examples/gc_iot_core_simpletest.py index d420a79..1756f91 100644 --- a/examples/gc_iot_core_simpletest.py +++ b/examples/gc_iot_core_simpletest.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import board import busio @@ -13,14 +14,17 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_gc_iot_core import Cloud_Core, MQTT_API -### WiFi ### +# Get WiFi details and Google Cloud keys, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +cloud_region = getenv("cloud_region") +device_id = getenv("device_id") +private_key = getenv("private_key") +project_id = getenv("project_id") +registry_id = getenv("registry_id") +jwt = getenv("jwt") -# 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 +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -35,19 +39,21 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """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 ### @@ -101,7 +107,14 @@ def message(client, topic, msg): ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) # Initialize Google Cloud IoT Core interface -google_iot = Cloud_Core(esp, secrets) +settings = { + cloud_region: cloud_region, + device_id: device_id, + private_key: private_key, + project_id: project_id, + registry_id: registry_id, +} +google_iot = Cloud_Core(esp, settings) # Optional JSON-Web-Token (JWT) Generation # print("Generating JWT...") @@ -112,7 +125,7 @@ def message(client, topic, msg): client = MQTT.MQTT( broker=google_iot.broker, username=google_iot.username, - password=secrets["jwt"], + password=jwt, client_id=google_iot.cid, socket_pool=pool, ssl_context=ssl_context, @@ -129,7 +142,8 @@ def message(client, topic, msg): google_mqtt.on_publish = publish google_mqtt.on_message = message -print("Attempting to connect to %s" % client.broker) + +print(f"Attempting to connect to {client.broker}") google_mqtt.connect() # Pump the message loop forever, all events are From adb5b07e6fdde546382de7524ad13e1e8a3f3b66 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 24 Feb 2025 15:31:24 -0800 Subject: [PATCH 2/2] Update adafruit_gc_iot_core.py Co-authored-by: Dan Halbert --- adafruit_gc_iot_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_gc_iot_core.py b/adafruit_gc_iot_core.py index 64c59e0..c48521c 100644 --- a/adafruit_gc_iot_core.py +++ b/adafruit_gc_iot_core.py @@ -354,7 +354,7 @@ class Cloud_Core: """CircuitPython Google Cloud IoT Core module. :param ESP_SPIcontrol esp: ESP32SPI object. - :param dict secrets: dictonary of settings. + :param dict secrets: dictionary of settings. :param bool log: Enable Cloud_Core logging, defaults to False. """