Multiple errors while connecting to my own MQTT Broker

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
d0773d
 
Posts: 22
Joined: Tue Aug 07, 2012 2:45 am

Multiple errors while connecting to my own MQTT Broker

Post by d0773d »

Hardware:
Adafruit CircuitPython 7.1.1 on 2022-01-14; Adafruit Feather nRF52840 Express with nRF52840

Adafruit AirLift FeatherWing – ESP32 WiFi Co-Processor

Error #1 Appears when Line 83

Code: Select all

port=secrets["port"]
is not commented out:

Code: Select all

Connecting to WiFi...
Connected!
Connecting to MQTT Server...
Traceback (most recent call last):
  File "code.py", line 93, in <module>
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 513, in connect
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 809, in _wait_for_msg
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 893, in _sock_exact_recv
TypeError: function takes 2 positional arguments but 3 were given
Error #2 Appears when Line 83:

Code: Select all

port=secrets["port"]
is commented out:

Code: Select all

Connecting to WiFi...
Connected!
Connecting to MQTT Server...
Traceback (most recent call last):
  File "code.py", line 93, in <module>
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 442, in connect
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 276, in _get_connect_socket
RuntimeError: Repeated socket failures


What I am attempting to achieve:
I am attempting to connect to my own MQTT Broker. I believe I have my Mosquito broker installed and configured correctly. I can connect to my broker via raspberry pi terminal: mosquitto_sub -v -h localhost -t "/humidity". I can also remotely connect via node-red and my Android phone using the MQTT Snooper app.

I arrived at those errors while attempting to troubleshoot my MQTT connect issue. I thought, maybe line 83 was creating the issue; however, I have no clue :-/

I am by no means an expert at this stuff. I am very new to python and attempting to solve a problem which is send humidity value to my own MQTT broker. With that said, let me know what other information I should provide to resolve this issue.

My code:

Code: Select all

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

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

import adafruit_minimqtt.adafruit_minimqtt as MQTT

### WiFi ###

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

# If you have an externally connected ESP32:
esp32_cs = DigitalInOut(board.D13)
esp32_ready = DigitalInOut(board.D11)
esp32_reset = DigitalInOut(board.D12)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(
    board.NEOPIXEL, 1, brightness=0.2
)

wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

### Feeds ###

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

# Setup a feed named 'onoff' for subscribing to changes
#onoff_feed = secrets["aio_username"] + "/feeds/onoff"
#onoff_feed = "/humidity/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("Connected to MQTT Server! Listening for topic changes on %s" % humidity_feed)
    # Subscribe to all changes on the humidity_feed.
    client.subscribe(humidity_feed)


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


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


# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")

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

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

# 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 MQTT Server...")
mqtt_client.connect()

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

    # Send a new message
    #print("Sending photocell value: %d..." % photocell_val)
    #mqtt_client.publish(photocell_feed, photocell_val)
    mqtt_client.publish(humidity_feed, "Hello!")
    print("Sent!")
    #photocell_val += 1
    time.sleep(5)

User avatar
d0773d
 
Posts: 22
Joined: Tue Aug 07, 2012 2:45 am

Re: Multiple errors while connecting to my own MQTT Broker

Post by d0773d »

Here is some more hopefully useful info:

Secrets file:
secrets = {
'ssid': '',
'password': '',
'timezone': '', # Check http://worldtimeapi.org/timezones
'broker': '192.168.1.26',
'port': 1883
}

I intentionally left out some 'secret' info.

Also, it looks like I will need to incorporate the port section at line 83 port=secrets["port"] since I am not using the default 8883 SSL port.

User avatar
d0773d
 
Posts: 22
Joined: Tue Aug 07, 2012 2:45 am

Re: Multiple errors while connecting to my own MQTT Broker

Post by d0773d »

To troubleshoot this further I changed my code to the Airlift Example code found here: https://learn.adafruit.com/mqtt-in-circ ... wifi-usage and updated my secrets file to reflect my io username and io password. I was able to connect just fine.

I then modified the Airlift example code to point to my mqtt server. The lines that I modified are: Starting at Line 85:

Code: Select all

# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
    broker="192.168.1.26",
    port=1883,
)
I removed the user name and password options due to not having login requirements and added port 1883.

However, I am still receiving error:

Code: Select all

Traceback (most recent call last):
  File "code.py", line 90, in <module>
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 513, in connect
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 809, in _wait_for_msg
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 893, in _sock_exact_recv
TypeError: function takes 2 positional arguments but 3 were given

User avatar
jkaiser20
 
Posts: 6
Joined: Fri Jan 03, 2020 7:37 pm

Re: Multiple errors while connecting to my own MQTT Broker

Post by jkaiser20 »

Is the problem the extra comma after 1883?

User avatar
d0773d
 
Posts: 22
Joined: Tue Aug 07, 2012 2:45 am

Re: Multiple errors while connecting to my own MQTT Broker

Post by d0773d »

I don't think that was the issue..... However, I was able to get a working example :-) I think the issue had to do with the way my mosquitto server was setup. I spun up a VPS over at digitalocean and setup certs using certbot and letsencrypt and configured mosquitto to use those certs generated. Before, I was attempting to setup everything without certificates. I removed port 1883 from mqtt_client changed the broker value to my domain which worked.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”