Skip to content

Commit

Permalink
Remove set_socket
Browse files Browse the repository at this point in the history
  • Loading branch information
justmobilize committed Feb 27, 2024
1 parent 7659f21 commit f1222a2
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 143 deletions.
31 changes: 17 additions & 14 deletions examples/cellular/minimqtt_adafruitio_cellular.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
import board
import busio
import digitalio
import adafruit_connection_manager
from adafruit_fona.adafruit_fona import FONA
import adafruit_fona.adafruit_fona_network as network
import adafruit_fona.adafruit_fona_socket as socket
import adafruit_fona.adafruit_fona_socket as pool

import adafruit_minimqtt.adafruit_minimqtt as MQTT

# Get Adafruit IO details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("GPRS secrets are kept in secrets.py, please add them there!")
raise
# 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")

### Cellular ###

Expand All @@ -29,10 +31,10 @@
### Feeds ###

# Setup a feed named 'photocell' for publishing to a feed
photocell_feed = secrets["aio_username"] + "/feeds/photocell"
photocell_feed = aio_username + "/feeds/photocell"

# Setup a feed named 'onoff' for subscribing to changes
onoff_feed = secrets["aio_username"] + "/feeds/onoff"
onoff_feed = aio_username + "/feeds/onoff"

### Code ###

Expand Down Expand Up @@ -60,7 +62,7 @@ def message(client, topic, message):

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

while not network.is_attached:
Expand All @@ -74,16 +76,17 @@ def message(client, topic, message):
time.sleep(0.5)
print("Network Connected!")

# Initialize MQTT interface with the cellular interface
MQTT.set_socket(socket, fona)
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, fona)

# Set up a MiniMQTT Client
# NOTE: We'll need to connect insecurely for ethernet configurations.
mqtt_client = MQTT.MQTT(
broker="io.adafruit.com",
username=secrets["aio_username"],
password=secrets["aio_key"],
username=aio_username,
password=aio_key,
is_ssl=False,
socket_pool=pool,
ssl_context=ssl_context,
)

# Setup the callback methods above
Expand Down
29 changes: 15 additions & 14 deletions examples/cellular/minimqtt_simpletest_cellular.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
import board
import busio
import digitalio
import adafruit_connection_manager
from adafruit_fona.adafruit_fona import FONA
import adafruit_fona.adafruit_fona_network as network
import adafruit_fona.adafruit_fona_socket as socket
import adafruit_fona.adafruit_fona_socket as pool

import adafruit_minimqtt.adafruit_minimqtt as MQTT

### Cellular ###
# 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.

# Get cellular details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("Cellular secrets are kept in secrets.py, please add them there!")
raise
aio_username = os.getenv("aio_username")
aio_key = os.getenv("aio_key")

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

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

while not network.is_attached:
Expand All @@ -85,15 +85,16 @@ def publish(client, userdata, topic, pid):
time.sleep(0.5)
print("Network Connected!")

# Initialize MQTT interface with the cellular interface
MQTT.set_socket(socket, fona)
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, fona)

# Set up a MiniMQTT Client
client = MQTT.MQTT(
broker=secrets["broker"],
username=secrets["user"],
password=secrets["pass"],
broker=os.getenv("broker"),
username=os.getenv("username"),
password=os.getenv("password"),
is_ssl=False,
socket_pool=pool,
ssl_context=ssl_context,
)

# Connect callback handlers to client
Expand Down
19 changes: 10 additions & 9 deletions examples/cpython/minimqtt_adafruitio_cpython.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

import os
import socket
import ssl
import time
Expand All @@ -9,19 +10,19 @@

### Secrets File Setup ###

try:
from secrets import secrets
except ImportError:
print("Connection secrets are kept in secrets.py, please add them there!")
raise
# 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.

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

### Feeds ###

# Setup a feed named 'photocell' for publishing to a feed
photocell_feed = secrets["aio_username"] + "/feeds/photocell"
photocell_feed = aio_username + "/feeds/photocell"

# Setup a feed named 'onoff' for subscribing to changes
onoff_feed = secrets["aio_username"] + "/feeds/onoff"
onoff_feed = aio_username + "/feeds/onoff"

### Code ###

Expand Down Expand Up @@ -50,8 +51,8 @@ def message(client, topic, message):
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker="io.adafruit.com",
username=secrets["aio_username"],
password=secrets["aio_key"],
username=aio_username,
password=aio_key,
socket_pool=socket,
is_ssl=True,
ssl_context=ssl.create_default_context(),
Expand Down
23 changes: 10 additions & 13 deletions examples/cpython/minimqtt_simpletest_cpython.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import ssl
import socket
import adafruit_minimqtt.adafruit_minimqtt as MQTT

# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# 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.

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

### Topic Setup ###

Expand All @@ -23,7 +20,7 @@

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


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

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=secrets["broker"],
username=secrets["aio_username"],
password=secrets["aio_key"],
broker=os.getenv("broker"),
username=aio_username,
password=aio_key,
socket_pool=socket,
ssl_context=ssl.create_default_context(),
)
Expand Down
8 changes: 5 additions & 3 deletions examples/esp32spi/minimqtt_adafruitio_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import adafruit_connection_manager
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_esp32spi.adafruit_esp32spi_socket as pool

import adafruit_minimqtt.adafruit_minimqtt as MQTT

Expand Down Expand Up @@ -82,14 +83,15 @@ def message(client, topic, message):
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, esp)

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker="io.adafruit.com",
username=aio_username,
password=aio_key,
socket_pool=pool,
ssl_context=ssl_context,
)

# Setup the callback methods above
Expand Down
12 changes: 8 additions & 4 deletions examples/esp32spi/minimqtt_certificate_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import busio
from digitalio import DigitalInOut
import neopixel
import adafruit_connection_manager
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_esp32spi.adafruit_esp32spi_socket as pool

import adafruit_minimqtt.adafruit_minimqtt as MQTT

Expand Down Expand Up @@ -110,12 +111,15 @@ def publish(client, userdata, topic, pid):
wifi.connect()
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, esp)

# Set up a MiniMQTT Client
client = MQTT.MQTT(
broker=secrets["broker"], username=secrets["user"], password=secrets["pass"]
broker=secrets["broker"],
username=secrets["user"],
password=secrets["pass"],
socket_pool=pool,
ssl_context=ssl_context,
)

# Connect callback handlers to client
Expand Down
12 changes: 8 additions & 4 deletions examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import adafruit_connection_manager
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_esp32spi.adafruit_esp32spi_socket as pool

import adafruit_minimqtt.adafruit_minimqtt as MQTT

Expand Down Expand Up @@ -82,12 +83,15 @@ def message(client, topic, message):
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, esp)

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker="io.adafruit.com", username=aio_username, password=aio_key
broker="io.adafruit.com",
username=aio_username,
password=aio_key,
socket_pool=pool,
ssl_context=ssl_context,
)

# Setup the callback methods above
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
import board
import busio
from digitalio import DigitalInOut
import neopixel
import adafruit_connection_manager
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_esp32spi.adafruit_esp32spi_socket as pool

import adafruit_minimqtt.adafruit_minimqtt as MQTT

Expand Down Expand Up @@ -91,10 +93,15 @@ def on_message(client, topic, message):
wifi.connect()
print("Connected!")

MQTT.set_socket(socket, esp)
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, esp)

# Set up a MiniMQTT Client
client = MQTT.MQTT(broker=secrets["broker"], port=secrets["broker_port"])
client = MQTT.MQTT(
broker=os.getenv("broker"),
port=os.getenv("broker_port"),
socket_pool=pool,
ssl_context=ssl_context,
)

# Setup the callback methods above
client.on_connect = connected
Expand Down
12 changes: 8 additions & 4 deletions examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import busio
from digitalio import DigitalInOut
import neopixel
import adafruit_connection_manager
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_esp32spi.adafruit_esp32spi_socket as pool

import adafruit_minimqtt.adafruit_minimqtt as MQTT

Expand Down Expand Up @@ -81,12 +82,15 @@ def message(client, topic, message):
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)
ssl_context = adafruit_connection_manager.create_fake_ssl_context(pool, esp)

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker="io.adafruit.com", username=aio_username, password=aio_key
broker="io.adafruit.com",
username=aio_username,
password=aio_key,
socket_pool=pool,
ssl_context=ssl_context,
)

# Setup the callback methods above
Expand Down
Loading

0 comments on commit f1222a2

Please sign in to comment.