Skip to content

sample minimqtt_simpltest.py MQTT connect error #76

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

Closed
gddeen opened this issue Mar 17, 2021 · 11 comments
Closed

sample minimqtt_simpltest.py MQTT connect error #76

gddeen opened this issue Mar 17, 2021 · 11 comments
Assignees

Comments

@gddeen
Copy link

gddeen commented Mar 17, 2021

I run minimqtt_simpletest.py and get connect issues.

Error observed:
code.py output:
Connecting to AstroACN3
Connected to AstroACN3!
Attempting to connect to io.adafruit.com
Traceback (most recent call last):
File "code.py", line 44, in
File "adafruit_minimqtt/adafruit_minimqtt.py", line 437, in connect
File "adafruit_minimqtt/adafruit_minimqtt.py", line 235, in _get_connect_socket
AttributeError: 'NoneType' object has no attribute 'info'

Code done running.

Code.py on the board:

====================================================

import ssl
import socketpool
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from secrets import secrets
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
#
mqtt_topic = secrets["aio_username"] + '/feeds/temperature'

def connect(mqtt_client, userdata, flags, rc):
    print("Connected to MQTT Broker!")
    print("Flags: {0}\n RC: {1}".format(flags, rc))


def disconnect(mqtt_client, userdata, rc):
    print("Disconnected from MQTT Broker!")


def subscribe(mqtt_client, userdata, topic, granted_qos):
    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


def unsubscribe(mqtt_client, userdata, topic, pid):
    print("Unsubscribed from {0} with PID {1}".format(topic, pid))


def publish(mqtt_client, userdata, topic, pid):
    print("Published to {0} with PID {1}".format(topic, pid))


def message(client, topic, message):
    print("New message on topic {0}: {1}".format(topic, message))


pool = socketpool.SocketPool(wifi.radio)

mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    port=secrets["port"],
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    socket_pool=pool,
    is_ssl=True,
    ssl_context=ssl.create_default_context(),
)

mqtt_client.on_connect = connect
mqtt_client.on_disconnect = disconnect
mqtt_client.on_subscribe = subscribe
mqtt_client.on_unsubscribe = unsubscribe
mqtt_client.on_publish = publish
mqtt_client.on_message = message

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

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

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

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

print("Disconnecting from %s" % mqtt_client.broker)
mqtt_client.disconnect()

====================================================

Secrets.py NEARLY:

#This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
secrets = {
'ssid' : 'AstroACN3',
'password' : 'xxxxxxxx',
'aio_username' : 'gddeen',
'aio_key' : 'deadbeefdeadbeefdeadbeef',
'aio_port' : "8883", #8883 secure 1883 not secure
'aio_feed' : "gddeen/feeds/temperature",
'port' : "8883", #8883 secure 1883 not secure
'broker' : 'io.adafruit.com',
'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
'openweather_token' : 'deadbeef',
'openweather_location' : 'New York City, US',
'openweather_latlon' : [ '40.7143', '-74.006' ],
'station_id' : '8531680',
'timer' : '1800',
'timer_name' : "GrubHub Delivery",
}

================================================

I have a Metro ESP32_s2 Express
lib has:

pi@RPI4D8G:/media/pi/CIRCUITPY $ dir -l lib
total 47
drwxr-xr-x 2 pi pi  1024 Mar 13 13:56 adafruit_bitmap_font
drwxr-xr-x 3 pi pi  1024 Mar 17 17:05 adafruit_bus_device
drwxr-xr-x 2 pi pi  1024 Mar 13 13:57 adafruit_display_shapes
drwxr-xr-x 2 pi pi   512 Mar 13 13:57 adafruit_display_text
-rw-r--r-- 1 pi pi   705 Mar 13 13:59 adafruit_fakerequests.mpy
-rw-r--r-- 1 pi pi  8786 Mar 13 13:59 adafruit_framebuf.mpy
-rw-r--r-- 1 pi pi   896 Mar 13 13:59 adafruit_il0398.mpy
drwxr-xr-x 4 pi pi   512 Mar 13 13:57 adafruit_imageload
drwxr-xr-x 2 pi pi   512 Mar 13 13:57 adafruit_io
drwxr-xr-x 2 pi pi  1024 Mar 10 20:52 adafruit_magtag
drwxr-xr-x 2 pi pi   512 Mar 10 20:52 adafruit_minimqtt
-rw-r--r-- 1 pi pi 11264 Mar 13 13:59 adafruit_miniqr.mpy
drwxr-xr-x 2 pi pi   512 Mar 10 20:52 adafruit_portalbase
-rw-r--r-- 1 pi pi 13124 Mar 13 13:59 adafruit_requests.mpy
-rw-r--r-- 1 pi pi  1891 Mar 13 13:59 neopixel.mpy
-rw-r--r-- 1 pi pi  2901 Mar 13 13:59 simpleio.mpy

======================================================

I'm running 6.2beta
cat boot_out.txt
Adafruit CircuitPython 6.2.0-beta.3 on 2021-03-04; Adafruit MagTag with ESP32S2

======================================================

The board has an e-ink 4.2" display hooked up at EDP_SPI, I5, 6, 7, 8

========================================================

The user, me, has a single feed temperature with a value of 27

I'm available 24 hours a day...

@gddeen
Copy link
Author

gddeen commented Mar 18, 2021

mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    port=secrets["port"],
    username=secrets["aio_username"],
    password=secrets["aio_key"],
    socket_pool=pool,
    is_ssl=True,
    ssl_context=ssl.create_default_context(),    
    log=True
)

Traceback (most recent call last):
File "code.py", line 51, in
TypeError: unexpected keyword argument 'log'

@gddeen
Copy link
Author

gddeen commented Mar 18, 2021

The boot_out.txt reads MagTag not Metro. Could this board think it is a Magtag?

@bangell
Copy link

bangell commented Mar 18, 2021

I believe this issue was fixed today in version 5.0.5

@gddeen
Copy link
Author

gddeen commented Mar 19, 2021

Today I got the Metro ESP32-S2 to confirm it wasn't a Magtag by loading adafruit-circuitpython-adafruit_metro_esp32s2-en_US-6.2.0-beta.4.uf2 and replacing lib directories and mpy files.

I rerun and get:

Attempting to connect to io.adafruit.com
Traceback (most recent call last):
File "code.py", line 62, in
File "adafruit_minimqtt/adafruit_minimqtt.py", line 437, in connect
File "adafruit_minimqtt/adafruit_minimqtt.py", line 240, in _get_connect_socket
TypeError: can't convert str to int

@ghayne
Copy link

ghayne commented Mar 19, 2021

In secrets.py
Instead of

'port' : "8883"

Try

'port' : 8883

@gddeen
Copy link
Author

gddeen commented Mar 19, 2021

I reloaded some of my client code to connect to MQTT.
Once I perform io.connect() it gets an exception.

Is there a more directed way to debug this?
I am going to take all the bundle lib source for adafruit_io and adafruit_minimqtt
and put that in my library and liberally spread print() so I can follow the execution path.

Socket Pool created
Connecting to Adafruit IO...
Traceback (most recent call last):
File "code.py", line 239, in
File "/blither.py", line 140, in
File "adafruit_io/adafruit_io.py", line 95, in connect
File "adafruit_io/adafruit_io.py", line 95, in connect
AdafruitIO_MQTTError: MQTT Error: Unable to connect to Adafruit IO.

@gddeen
Copy link
Author

gddeen commented Mar 19, 2021

        Socket Pool created
io.connect()
        MQTT __init__ in at 150
        MQTT __init__ at 213
        IO_MQTT __init__ at 46
        IO_MQTT __init__ 49  <class 'MQTT'>
        IO_MQTT __init__ at 57
        IO_MQTT __init__ out at 77
        Connecting to Adafruit IO...
        IO_MQTT connect at 100 enter _client connect()
        MQTT __init__ _get_connect_socket at 222
        MQTT __init__ _get_connect_socket at 240
        MQTT __init__ _get_connect_socket at 251 io.adafruit.com 8883 0
THIS line causes an exception:
( host is 'io.adafruit.com' port is 8883 _socket_pool is 0)
        addr_info = self._socket_pool.getaddrinfo(
            host, port, 0, self._socket_pool.SOCK_STREAM
        )[0]

        IO_MQTT catch at 104

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

@gddeen
Copy link
Author

gddeen commented Mar 19, 2021

I had in secrets:
"aio_port" : "8883"
instead of
"aio_port" : 8883

Fixed

@gddeen gddeen closed this as completed Mar 19, 2021
@gddeen
Copy link
Author

gddeen commented Mar 19, 2021

Thanks for all the help everyone.

@brentru
Copy link
Member

brentru commented Mar 19, 2021

Aha! We should really validate if port is an integer within this lib's init call.

I'll leave this issue open and assign myself so I can make a small tweak.

@brentru
Copy link
Member

brentru commented Mar 26, 2021

closed via #78

@brentru brentru closed this as completed Mar 26, 2021
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

No branches or pull requests

4 participants