Problem in locally connecting ESP32 to Mosquitto Broker [message #1863990] |
Sun, 10 March 2024 03:44 |
Yugu Zhou Messages: 1 Registered: March 2024 |
Junior Member |
|
|
I have recently installed mosquittto -2.0.18 on my windows 11. After installation the testing with "mosquitto_sub" and "mosquitto_pub"in different command windows works fine. And the localhost connection with MQTT Explorer also has no problem.
When trying to locally connecting an ESP32 device (physically connected to the PC running mosquitto Broker) using MicroPython code below
from umqtt.simple import MQTTClient
import network
import utime as time
import machine
# 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_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
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()
# 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()
#try:
# mqtt_client = mqtt_connect()
#except OSError as e:
# restart_and_reconnect()
print("Mqtt Connected!")
It failed to connect to the broker with error message as
Connecting to MQTT broker ...Traceback (most recent call last):
File "<stdin>", line 72, 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 if used to connect to an external Broker, "broker.mqttdashboard.com", rather than the local one "localhost", it works fine.
I tried to add a try except to caught the OSError and to make the device restart and reconnect, it returns the following
Connecting to MQTT broker ...Failed to connect to MQTT broker. Reconnecting...
ets Jul 29 2019 12:21:46
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4728
load:0x40078000,len:14888
load:0x40080400,len:3368
entry 0x400805cc
MicroPython v1.22.1 on 2024-01-05; Generic ESP32 module with ESP32
Type "help()" for more information.
Still not connect to the broker. Could anyone out there kindly help me with this problem? Thanks in advance!
|
|
|
Powered by
FUDForum. Page generated in 0.02955 seconds