Yugu
Yugu

Reputation: 1

Problem in connecting esp32 to local MQTT broker

I am having problem in connecting esp32 to local MQTT broker using the following micropython code

from umqtt.simple import MQTTClient import network import utime as time # Device Setup DEVICE_ID = "wokwi001" MQTT_CONTROL_TOPIC = "iot/control" # WiFi Setup WIFI_SSID = "####" WIFI_PASSWORD = "######" # MQTT Setup #MQTT_BROKER = "broker.mqttdashboard.com" MQTT_BROKER = "localhost" #MQTT_BROKER = "192.168.1.105" MQTT_CLIENT = DEVICE_ID def did_recieve_callback(topic, message): print('\n\nData Recieved! \ntopic = {0}, message = {1}'.format(topic, message)) # device_id/lamp/color/state # device_id/lamp/state # lamp/state if topic == MQTT_CONTROL_TOPIC.encode(): #if message == ('{0}/lamp/red/on'.format(DEVICE_ID)).encode(): # RED_LED.on() #elif message == ('{0}/lamp/red/off'.format(DEVICE_ID)).encode(): # RED_LED.off() if message == ('{0}/lamp/blue/on'.format(DEVICE_ID)).encode(): BLUE_LED.on() elif message == ('{0}/lamp/blue/off'.format(DEVICE_ID)).encode(): BLUE_LED.off() elif message == ('{0}/lamp/on'.format(DEVICE_ID)).encode() or message == b'lamp/on': #RED_LED.on() BLUE_LED.on() elif message == ('{0}/lamp/off'.format(DEVICE_ID)).encode() or message == b'lamp/off': #RED_LED.off() BLUE_LED.off() else: return send_led_status() def mqtt_connect(): print("Connecting to MQTT broker ...", end="") mqtt_client = MQTTClient(MQTT_CLIENT, MQTT_BROKER, user="", password="") mqtt_client.set_callback(did_recieve_callback) mqtt_client.connect() print("Connected.") mqtt_client.subscribe(MQTT_CONTROL_TOPIC) return mqtt_client # Connecting to Wifi wifi_client = network.WLAN(network.STA_IF) wifi_client.active(True) print("Connecting device to WiFi") wifi_client.connect(WIFI_SSID, WIFI_PASSWORD) # Wait until WiFi is Connected while not wifi_client.isconnected(): print("Connecting") time.sleep(0.1) print("WiFi Connected!") print(wifi_client.ifconfig()) # Connect to MQTT mqtt_client = mqtt_connect() #waiting until MQTT is connected #while not mqtt_client.isconnected(): # print("Connecting") # time.sleep(0.1) print("Mqtt Connected!")

The error message is

Connecting to MQTT broker ...Traceback (most recent call last):
  File "<stdin>", line 67, in <module>
  File "<stdin>", line 48, in mqtt_connect
  File "umqtt/simple.py", line 68, in connect
OSError: [Errno 104] ECONNRESET

However, the same code (except the MQTT_BROKER is changed from "localhost" to "broker.mqttdashboard.com") works fine.

I have tried adding

listener 1883

Allow anonymous True

with no success. Could anyone out there point me a direction? Thanks in advance

Upvotes: 0

Views: 261

Answers (1)

hardillb
hardillb

Reputation: 59791

First localhost always points to the machine the code is running on, so in this case localhost will be the esp32, not the machine running the broker.

You will need to use the IP address if the machine running the broker.

Second, the correct entries in the mosquitto config file is

listener 1883
allow_anonymous true

The underline and case is important

Upvotes: 1

Related Questions