adafruit / Adafruit_CircuitPython_AdafruitIO

Adafruit IO for CircuitPython
http://io.adafruit.com
MIT License
49 stars 33 forks source link

Cannot connect to Adafruit IO #107

Open prcutler opened 1 year ago

prcutler commented 1 year ago

Hi,

I'm trying to use the AdafruitIO library instead of (or with) Adafruit MiniMQTT. I've tried on both an S3 MatrixPortal and a Pico W.

It fails on the line: io.connect() with the error:

Traceback (most recent call last):
  File "code.py", line 79, in <module>
  File "adafruit_io/adafruit_io.py", line 116, in connect
AdafruitIO_MQTTError: MQTT Error: Unable to connect to Adafruit IO.

Here's a short example with the Pico W. If I swap out the line io = IO_MQTT(mqtt_client) and use a normal MQTT setup, it works fine and I can subscribe to my feed. I've tried using SSL and non-SSL (like below). I don't know if anyone can verify that io.connect() works for them.

import os
import ssl
import time
import json
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import socketpool
import wifi
import displayio
import terminalio
import adafruit_display_text.label
from adafruit_io.adafruit_io import IO_MQTT

wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
pool = socketpool.SocketPool(wifi.radio)
ssl_context = ssl.create_default_context()

# ------------- MQTT Topic Setup ------------- #
mqtt_topic = "prcutler/feeds/audio"

mqtt_client = MQTT.MQTT(
    broker="io.adafruit.com",
    port=1883,
    username=os.getenv('aio_username'),
    password=os.getenv('aio_key'),
    socket_pool=pool,
)

### 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("Subscribing to %s" % mqtt_topic)
    client.subscribe(mqtt_topic)

def subscribe(client, userdata, topic, granted_qos):
    # This method is called when the client subscribes to a new feed.
    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))

def unsubscribe(client, userdata, topic, pid):
    # This method is called when the client unsubscribes from a feed.
    print("Unsubscribed from {0} with PID {1}".format(topic, pid))

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

def message(client, topic, message):
    """Method callled when a client's subscribed feed has a new
    value.
    :param str topic: The topic of the feed with a new value.
    :param str message: The new value
    """
    print(topic, message)

    print(message)

io = IO_MQTT(mqtt_client)

io.on_connect = connected
io.on_disconnect = disconnected
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = message

# Connect to Adafruit IO

print("Connecting to Adafruit IO...")
io.connect()

while True:
    io.loop()
luke-edward commented 7 months ago

Still having this issue. Any fix?