Skip to content

Remove secrets usage #245

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 10 commits into from
Mar 6, 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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
rev: v0.9.9
hooks:
- id: ruff-format
- id: ruff
args: ["--fix"]
- id: ruff-format
- repo: https://github.com/fsfe/reuse-tool
rev: v3.0.1
hooks:
Expand Down
4 changes: 3 additions & 1 deletion adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

"""

# ruff: noqa: PLR6104,PLR6201,PLR6301 non-augmented-assignment,literal-membership,no-self-use

import errno
import struct
import time
Expand Down Expand Up @@ -111,7 +113,7 @@ def __init__(self) -> None:
setattr(NullLogger, log_level, self.nothing)


class MQTT:
class MQTT: # noqa: PLR0904 # too-many-public-methods
"""MQTT Client for CircuitPython.

:param str broker: MQTT Broker URL or IP Address.
Expand Down
27 changes: 13 additions & 14 deletions examples/cellular/minimqtt_adafruitio_cellular.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
from os import getenv

import adafruit_connection_manager
import adafruit_fona.adafruit_fona_network as network
Expand All @@ -14,12 +14,13 @@

import adafruit_minimqtt.adafruit_minimqtt as MQTT

# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
# with your GPRS credentials. Add your Adafruit IO username and key as well.
# DO NOT share that file or commit it into Git or other source control.

aio_username = os.getenv("aio_username")
aio_key = os.getenv("aio_key")
# Get FONA details and Adafruit IO keys, ensure these are setup in settings.toml
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
apn = getenv("apn")
apn_username = getenv("apn_username")
apn_password = getenv("apn_password")
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")

### Cellular ###

Expand All @@ -32,10 +33,10 @@
### Feeds ###

# Setup a feed named 'photocell' for publishing to a feed
photocell_feed = aio_username + "/feeds/photocell"
photocell_feed = f"{aio_username}/feeds/photocell"

# Setup a feed named 'onoff' for subscribing to changes
onoff_feed = aio_username + "/feeds/onoff"
onoff_feed = f"{aio_username}/feeds/onoff"

### Code ###

Expand All @@ -44,7 +45,7 @@
def connected(client, userdata, flags, rc):
# This function will be called when the client is connected
# successfully to the broker.
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
# Subscribe to all changes on the onoff_feed.
client.subscribe(onoff_feed)

Expand All @@ -61,9 +62,7 @@ def message(client, topic, message):


# Initialize cellular data network
network = network.CELLULAR(
fona, (os.getenv("apn"), os.getenv("apn_username"), os.getenv("apn_password"))
)
network = network.CELLULAR(fona, (apn, apn_username, apn_password))

while not network.is_attached:
print("Attaching to network...")
Expand Down Expand Up @@ -105,7 +104,7 @@ def message(client, topic, message):
mqtt_client.loop()

# Send a new message
print("Sending photocell value: %d..." % photocell_val)
print(f"Sending photocell value: {photocell_val}...")
mqtt_client.publish(photocell_feed, photocell_val)
print("Sent!")
photocell_val += 1
Expand Down
36 changes: 18 additions & 18 deletions examples/cellular/minimqtt_simpletest_cellular.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
from os import getenv

import adafruit_connection_manager
import adafruit_fona.adafruit_fona_network as network
Expand All @@ -14,12 +14,14 @@

import adafruit_minimqtt.adafruit_minimqtt as MQTT

# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
# with your GPRS credentials. Add your Adafruit IO username and key as well.
# DO NOT share that file or commit it into Git or other source control.

aio_username = os.getenv("aio_username")
aio_key = os.getenv("aio_key")
# Get FONA details and Adafruit IO keys, ensure these are setup in settings.toml
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
apn = getenv("apn")
apn_username = getenv("apn_username")
apn_password = getenv("apn_password")
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")
broker = getenv("broker", "io.adafruit.com")

# Create a serial connection for the FONA connection
uart = busio.UART(board.TX, board.RX)
Expand Down Expand Up @@ -70,9 +72,7 @@ def publish(client, userdata, topic, pid):


# Initialize cellular data network
network = network.CELLULAR(
fona, (os.getenv("apn"), os.getenv("apn_username"), os.getenv("apn_password"))
)
network = network.CELLULAR(fona, (apn, apn_username, apn_password))

while not network.is_attached:
print("Attaching to network...")
Expand All @@ -89,9 +89,9 @@ def publish(client, userdata, topic, pid):

# Set up a MiniMQTT Client
client = MQTT.MQTT(
broker=os.getenv("broker"),
username=os.getenv("username"),
password=os.getenv("password"),
broker=broker,
username=aio_username,
password=aio_key,
is_ssl=False,
socket_pool=pool,
ssl_context=ssl_context,
Expand All @@ -104,17 +104,17 @@ def publish(client, userdata, topic, pid):
client.on_unsubscribe = unsubscribe
client.on_publish = publish

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

print("Subscribing to %s" % mqtt_topic)
print(f"Subscribing to {mqtt_topic}")
client.subscribe(mqtt_topic)

print("Publishing to %s" % mqtt_topic)
print(f"Publishing to {mqtt_topic}")
client.publish(mqtt_topic, "Hello Broker!")

print("Unsubscribing from %s" % mqtt_topic)
print(f"Unsubscribing from {mqtt_topic}")
client.unsubscribe(mqtt_topic)

print("Disconnecting from %s" % client.broker)
print(f"Disconnecting from {client.broker}")
client.disconnect()
22 changes: 12 additions & 10 deletions examples/cpython/minimqtt_adafruitio_cpython.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import socket
import ssl
import time
from os import getenv

import adafruit_minimqtt.adafruit_minimqtt as MQTT

### Secrets File Setup ###
### Key Setup ###

# Add settings.toml to your filesystem. Add your Adafruit IO username and key as well.
# DO NOT share that file or commit it into Git or other source control.
# Add your Adafruit IO username and key to your env.
# example:
# export ADAFRUIT_AIO_USERNAME=your-aio-username
# export ADAFRUIT_AIO_KEY=your-aio-key

aio_username = os.getenv("aio_username")
aio_key = os.getenv("aio_key")
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")

### Feeds ###

# Setup a feed named 'photocell' for publishing to a feed
photocell_feed = aio_username + "/feeds/photocell"
photocell_feed = f"{aio_username}/feeds/photocell"

# Setup a feed named 'onoff' for subscribing to changes
onoff_feed = aio_username + "/feeds/onoff"
onoff_feed = f"{aio_username}/feeds/onoff"

### Code ###

Expand All @@ -31,7 +33,7 @@
def connected(client, userdata, flags, rc):
# This function will be called when the client is connected
# successfully to the broker.
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
# Subscribe to all changes on the onoff_feed.
client.subscribe(onoff_feed)

Expand Down Expand Up @@ -72,7 +74,7 @@ def message(client, topic, message):
mqtt_client.loop()

# Send a new message
print("Sending photocell value: %d..." % photocell_val)
print(f"Sending photocell value: {photocell_val}...")
mqtt_client.publish(photocell_feed, photocell_val)
print("Sent!")
photocell_val += 1
Expand Down
29 changes: 17 additions & 12 deletions examples/cpython/minimqtt_simpletest_cpython.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import socket
import ssl
from os import getenv

import adafruit_minimqtt.adafruit_minimqtt as MQTT

# Add settings.toml to your filesystems. Add your Adafruit IO username and key as well.
# DO NOT share that file or commit it into Git or other source control.
# Add your Adafruit IO username and key to your env.
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
# example:
# export ADAFRUIT_AIO_USERNAME=your-aio-username
# export ADAFRUIT_AIO_KEY=your-aio-key
# export broker=io.adafruit.com

aio_username = os.getenv("aio_username")
aio_key = os.getenv("aio_key")
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")
broker = getenv("broker", "io.adafruit.com")

### Topic Setup ###

Expand All @@ -21,7 +26,7 @@

# Adafruit IO-style Topic
# Use this topic if you'd like to connect to io.adafruit.com
# mqtt_topic = aio_username + "/feeds/temperature"
# mqtt_topic = f"{aio_username}/feeds/temperature"


### Code ###
Expand Down Expand Up @@ -61,7 +66,7 @@ def message(client, topic, message):

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=os.getenv("broker"),
broker=broker,
username=aio_username,
password=aio_key,
socket_pool=socket,
Expand All @@ -76,17 +81,17 @@ def message(client, topic, message):
mqtt_client.on_publish = publish
mqtt_client.on_message = message

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

print("Subscribing to %s" % mqtt_topic)
print(f"Subscribing to {mqtt_topic}")
mqtt_client.subscribe(mqtt_topic)

print("Publishing to %s" % mqtt_topic)
print(f"Publishing to {mqtt_topic}")
mqtt_client.publish(mqtt_topic, "Hello Broker!")

print("Unsubscribing from %s" % mqtt_topic)
print(f"Unsubscribing from {mqtt_topic}")
mqtt_client.unsubscribe(mqtt_topic)

print("Disconnecting from %s" % mqtt_client.broker)
print(f"Disconnecting from {mqtt_client.broker}")
mqtt_client.disconnect()
30 changes: 15 additions & 15 deletions examples/esp32spi/minimqtt_adafruitio_esp32spi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
from os import getenv

import adafruit_connection_manager
import board
Expand All @@ -13,12 +13,12 @@

import adafruit_minimqtt.adafruit_minimqtt as MQTT

# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
# with your WiFi credentials. Add your Adafruit IO username and key as well.
# DO NOT share that file or commit it into Git or other source control.

aio_username = os.getenv("aio_username")
aio_key = os.getenv("aio_key")
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
Expand All @@ -33,24 +33,24 @@
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(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
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)
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)

### Feeds ###

# Setup a feed named 'photocell' for publishing to a feed
photocell_feed = aio_username + "/feeds/photocell"
photocell_feed = f"{aio_username}/feeds/photocell"

# Setup a feed named 'onoff' for subscribing to changes
onoff_feed = aio_username + "/feeds/onoff"
onoff_feed = f"{aio_username}/feeds/onoff"

### Code ###

Expand All @@ -59,7 +59,7 @@
def connected(client, userdata, flags, rc):
# This function will be called when the client is connected
# successfully to the broker.
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
# Subscribe to all changes on the onoff_feed.
client.subscribe(onoff_feed)

Expand All @@ -77,7 +77,7 @@ def message(client, topic, message):

# Connect to WiFi
print("Connecting to WiFi...")
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
esp.connect_AP(ssid, password)
print("Connected!")

pool = adafruit_connection_manager.get_radio_socketpool(esp)
Expand Down Expand Up @@ -107,7 +107,7 @@ def message(client, topic, message):
mqtt_client.loop()

# Send a new message
print("Sending photocell value: %d..." % photocell_val)
print(f"Sending photocell value: {photocell_val}...")
mqtt_client.publish(photocell_feed, photocell_val)
print("Sent!")
photocell_val += 1
Expand Down
Loading