Skip to content

Remove secrets usage #30

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
22 changes: 10 additions & 12 deletions adafruit_gc_iot_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: dictionary of settings.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be renamed to settings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that, but if people are passing in named args it would break. If you are okay with that change, I'll totally update it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since secrets.py is not mentioned, yes, seems like ok to call it secrets still.

:param bool log: Enable Cloud_Core logging, defaults to False.
"""

Expand All @@ -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
Expand All @@ -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
Expand Down
42 changes: 28 additions & 14 deletions examples/gc_iot_core_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 board
import busio
Expand All @@ -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)
Expand All @@ -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 ###

Expand Down Expand Up @@ -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...")
Expand All @@ -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,
Expand All @@ -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
Expand Down