Skip to content
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

Change free_socket to close_socket #208

Merged
merged 1 commit into from
Mar 4, 2024

Conversation

justmobilize
Copy link
Collaborator

Fixes #207

@justmobilize justmobilize marked this pull request as ready for review March 4, 2024 18:21
@justmobilize
Copy link
Collaborator Author

@FoamyGuy willing to take a look?

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

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

Looks good to me. I confirmed this issue and successfully tested the fix on a Feather ESP32-S3 TFT with this slightly modified example code:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import time
import ssl
import socketpool
import wifi
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. DO NOT share that file or commit it into Git or other
# source control.

# Set your Adafruit IO Username, Key and Port in settings.toml
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = os.getenv("aio_username")
aio_key = os.getenv("aio_key")

print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(
    os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
)
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")
### Feeds ###

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

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

### Code ###


# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connected(client, userdata, flags, rc):
    # This function will be called when the client is connected
    # successfully to the broker.
    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)


def disconnected(client, userdata, rc):
    # This method is called when the client is disconnected
    print("Disconnected from Adafruit IO!")


def message(client, topic, message):
    # This method is called when a topic the client is subscribed to
    # has a new message.
    print(f"New message on topic {topic}: {message}")


# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
ssl_context = ssl.create_default_context()

# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the
# ssl context by uncommenting the lines below and adding the following keys to your settings.toml:
# "device_cert_path" - Path to the Device Certificate
# "device_key_path" - Path to the RSA Private Key
# ssl_context.load_cert_chain(
#     certfile=os.getenv("device_cert_path"), keyfile=os.getenv("device_key_path")
# )

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

# Setup the callback methods above
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
mqtt_client.on_message = message

# Connect the client to the MQTT broker.
print("Connecting to Adafruit IO...")


photocell_val = 0
while True:
    mqtt_client.connect()
    # Poll the message queue
    mqtt_client.loop(timeout=1)

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

    mqtt_client.disconnect()
    time.sleep(5)

Thanks for the quick fix @justmobilize!

@FoamyGuy FoamyGuy merged commit cbe2177 into adafruit:main Mar 4, 2024
1 check passed
@justmobilize justmobilize deleted the change-free-to-close branch March 4, 2024 20:23
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 5, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_FONA to 3.0.4 from 3.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_FONA#32 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 6.3.3 from 6.3.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#133 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#132 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 5.0.7 from 5.0.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#147 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#146 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_AWS_IOT to 2.0.18 from 2.0.17:
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#26 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#25 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_AWS_IOT#24 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core to 3.2.11 from 3.2.10:
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#29 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#28 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#27 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO to 5.8.2 from 5.8.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#118 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#117 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_AdafruitIO#116 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT to 2.5.18 from 2.5.17:
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#64 from justmobilize/fix-readme-requirements
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#63 from justmobilize/fix-ssl-and-set-socket

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE to 10.0.6 from 10.0.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE#193 from dhalbert/deinit-characteristic-buffers

Updating https://github.com/adafruit/Adafruit_CircuitPython_Dash_Dis to 2.1.2 from 2.1.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_Dash_Dis#18 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_Dash_Dis#17 from justmobilize/update-legacy-set-socket-examples

Updating https://github.com/adafruit/Adafruit_CircuitPython_JSON_Stream to 0.8.3 from 0.8.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_JSON_Stream#3 from bablokb/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.6.3 from 7.6.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#208 from justmobilize/change-free-to-close
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#205 from LelandSindt/main
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#206 from justmobilize/fix-get-radio-ssl-context

Updating https://github.com/adafruit/Adafruit_CircuitPython_OAuth2 to 1.0.17 from 1.0.16:
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#12 from justmobilize/fix-get-radio-ssl-context
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#11 from justmobilize/update-legacy-set-socket-examples
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#10 from justmobilize/fix-readme-requirements

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

free_socket or close_socket ?
2 participants