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"]
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
Code: Select all
port=secrets["port"]
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)