adafruit / Adafruit_CircuitPython_MiniMQTT

MQTT Client Library for CircuitPython
Other
78 stars 49 forks source link

Exception tricky to troubleshoot when defaulted to SSL but broker is not SSL #60

Closed rpavlik closed 3 years ago

rpavlik commented 3 years ago

I'm using a MatrixPortal successfully connected to wifi, on a network with a mosquitto mqtt server, no authentication or security on that. circuitpython 6.1.0, latest library bundle (I think - circup isn't working for me today, but I hand-selected the libraries I think I needed and copied them manually from the bundle), latest nina firmware on esp32, mqtt broker is specified by ip (v4) address in the secrets file.

Chopped down code:


import random
import time
from secrets import secrets

import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import board
import busio
import neopixel
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
from adafruit_matrixportal.matrixportal import MatrixPortal

esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(
    board.NEOPIXEL, 1, brightness=0.2
) 
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

print("Connecting to WiFi...")
wifi.connect()
print("Connected!")
# --- Display setup ---
matrixportal = MatrixPortal( debug=True, esp=esp, external_spi=spi)
# Create a new label with the color and text selected
matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(0, (matrixportal.graphics.display.height // 2) - 1),
    scrolling=True,
)

# Static 'Connecting' Text
matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(2, (matrixportal.graphics.display.height // 2) - 1),
)

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    log=True
)
PRESSURE_TOPIC = "office/sensor/pressure/state"

last_pressure = 0
def handle_message(client, topic, message):
    global last_pressure
    message = int(float(message))
    if last_pressure != message:
        scd.ambient_pressure = message
        last_pressure = message

def connected(client, userdata, flags, rc):
    # This function will be called when the client is connected
    # successfully to the broker.
    print("Connected to MQTT broker! Listening for topic changes on %s" % PRESSURE_TOPIC)

    matrixportal.set_text("Connected", 1)

    client.subscribe(PRESSURE_TOPIC)

mqtt_client.on_message = handle_message
mqtt_client.on_connect = connected

matrixportal.set_text("Connecting", 1)
mqtt_client.connect()

(Yes, I'm initially just trying to just read a pressure from another sensor in my network, since I'm porting some code from the Clue. Just trying to get my scd30 and pm25 sensor submitting to mqtt and thus influxdb and grafana, in addition to a local display :D )

I get this output:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.

code.py output:
Connecting to WiFi...
Connected!
Init display
Init background
Init text area
Init text area
Creating text area with : Connecting
Traceback (most recent call last):
  File "code.py", line 97, in <module>
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 304, in connect
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 304, in connect
MMQTTException: ('Invalid broker address defined.', RuntimeError('Expected 01 but got 00',))

Line 97 is the mqtt connect line at the end of the cut down code.

Resembles #50 but not sure if it's the same. Hardware reset doesn't seem to help.

(Is this the right place for these kinds of "Seems like this should work, but I might be doing it wrong" questions, or would you prefer forums or discord?)

askpatrickw commented 3 years ago

@rpavlik can you open an issue in the circup repo explaining what didn't work. I've been working on it lately. Ty!

askpatrickw commented 3 years ago

@rpavlik If this is the issue you saw https://github.com/adafruit/circup/issues/75. Circup has been fixed and released, new version is 0.9.0

rpavlik commented 3 years ago

OK, so that fixed the circup issue (though I was getting filename corruption if I tried installing all the libraries). I started fresh with an erased filesystem and just the stuff I absolutely need. Here's what circup freeze says I have:

And unfortunately this problem still happens

rpavlik commented 3 years ago

Ah, it was defaulting to use tls/port 8883, which is at least part of it.

rpavlik commented 3 years ago

Unfortunately, it's now just failing in the other half of the condition.


Traceback (most recent call last):
  File "code.py", line 90, in <module>
  File "/lib/adafruit_minimqtt/adafruit_minimqtt.py", line 316, in connect
  File "/lib/adafruit_minimqtt/adafruit_minimqtt.py", line 316, in connect
MMQTTException: ('Invalid broker address defined/could not connect: broker 192.186.1.150, port 1883', RuntimeError('ESP32 not responding',))
rpavlik commented 3 years ago

Tried re-flashing the esp32, then using the basic outline of the wifi test code. Looked the same as what I was using, but without the wifi manager. This does appear to work, at least after I properly re-type the IP address of my broker. Turning other code back on bit-by-bit.

brentru commented 3 years ago

Which version of nina-fw (https://github.com/adafruit/nina-fw) is running on your ESP32?

rpavlik commented 3 years ago

1.7.1, tested reinstalling. Thought I had added results, but I guess not. Trying to figure out how I got it to fail, since it's working now (though the scd30 is erroring, might be too much current or something?)

rpavlik commented 3 years ago

OK, I think this was mostly about missing the detail that it assumes SSL and port 8883 by default. The exception message might be a bit more useful (they're already split so wouldn't be too bad to adjust) for folks who have no ssl inside their lan, but it appears like it's not actually anything to do with the wifi manager or the matrixportal library or anything.

rpavlik commented 3 years ago

ah, I was behind by 1 major release. Now it errors different if you get the ssl-ness wrong. Maybe edit line 272 so that it says "Are you sure your broker uses SSL?" if it was defaulting to SSL?

brentru commented 3 years ago

@rpavlik Yeah, the client assumes you are using SSL. You may un-set it via the initializer.

Now it errors different if you get the ssl-ness wrong.

What does this error look like - could you copy/paste the output from the repl here?

rpavlik commented 3 years ago
>>> # Set up a MiniMQTT Client
>>> mqtt_client = MQTT.MQTT(
...     broker=secrets["broker"],
...         # is_ssl=secrets["mqttssl"]
...             #port=secrets["brokerPort"],
...                 # log=True
...                 )
>>> mqtt_client.connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/adafruit_minimqtt/adafruit_minimqtt.py", line 467, in connect
  File "/lib/adafruit_minimqtt/adafruit_minimqtt.py", line 272, in _get_socket
RuntimeError: Sending request failed
>>>

with the latest version of everything according to circup.

brentru commented 3 years ago

I was able to connect to test.mosquitto.org using its numeric IP address ('broker': "5.196.95.208", 'port': 1883).

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker=secrets["broker"],
    port=1883
)

Are other devices on your local network (MQTT client on a phone, laptop, tablet) able to see the Mosquitto broker?

Have you tried "exposing" the server using something like NGROK (https://ngrok.com)?

Thanks for being patient - I'd like to find this bug.

rpavlik commented 3 years ago

I can connect to it if I specify the port - see commented out code in requested repl log. I think the only "bug" here is that it was hard for me to figure out that the default was SSL and that's why the connection failed.

brentru commented 3 years ago

I see - let me if we can raise an error anywhere or can send a message to the logger.

brentru commented 3 years ago

Addressed in #71, closing