Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 11 additions & 12 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 @@ -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
35 changes: 17 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,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")

# Create a serial connection for the FONA connection
uart = busio.UART(board.TX, board.RX)
Expand Down Expand Up @@ -70,9 +71,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 +88,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="io.adafruit.com",
username=aio_username,
password=aio_key,
is_ssl=False,
socket_pool=pool,
ssl_context=ssl_context,
Expand All @@ -104,17 +103,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()
18 changes: 10 additions & 8 deletions examples/cpython/minimqtt_adafruitio_cpython.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# 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 ###

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
24 changes: 13 additions & 11 deletions examples/cpython/minimqtt_simpletest_cpython.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# 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.
# 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")

### Topic Setup ###

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

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=os.getenv("broker"),
broker="io.adafruit.com",
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we stick with broker env var lookup and fall back to io.adafruit.com using the second parameter / default value, as this example isn't really IO related (just a good test server if the user already has an account). Similarly here: https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/pull/245/files#diff-afdace9507c758aee40533c5ef2f1cacbfe152f00f601e2fc07571931dfa63f8R16 which is examples/esp32spi/minimqtt_certificate_esp32spi.py line 16.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@tyeth , so do:

broker = getenv("broker", "io.adafruit.com")

in this one?

Copy link
Member

@tyeth tyeth Feb 26, 2025

Choose a reason for hiding this comment

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

yeah, something like that. think i spotted "broker" in a few other places too (just glancing at the connect lines)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will find all of these and make sure they are updated correctly. Will hold until I get other answers to do the work

username=aio_username,
password=aio_key,
socket_pool=socket,
Expand All @@ -76,17 +78,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()
26 changes: 13 additions & 13 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,16 +33,16 @@
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 ###

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
38 changes: 19 additions & 19 deletions examples/esp32spi/minimqtt_certificate_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

import adafruit_minimqtt.adafruit_minimqtt as MQTT

### WiFi ###
# Get WiFi details and MQTT keys, ensure these are setup in settings.toml
ssid = getenv("CIRCUITPY_WIFI_SSID")
password = getenv("CIRCUITPY_WIFI_PASSWORD")
broker = getenv("broker")
username = getenv("username")
paswword = getenv("paswword")
Comment on lines +16 to +20
Copy link
Member

Choose a reason for hiding this comment

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

getenv not defined

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@tyeth good catch. Fixed.

Ruff didn't catch that, so that's strange (cc @FoamyGuy)


# 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 @@ -32,17 +32,17 @@
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)
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)

### Topic Setup ###

Expand Down Expand Up @@ -109,9 +109,9 @@ def publish(client, userdata, topic, pid):

# Set up a MiniMQTT Client
client = MQTT.MQTT(
broker=secrets["broker"],
username=secrets["user"],
password=secrets["pass"],
broker=broker,
username=username,
password=password,
socket_pool=pool,
ssl_context=ssl_context,
)
Expand All @@ -123,17 +123,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()
Loading