Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnection even though no reconnect is necessary #508

Closed
Flole998 opened this issue Oct 24, 2018 · 4 comments
Closed

Reconnection even though no reconnect is necessary #508

Flole998 opened this issue Oct 24, 2018 · 4 comments

Comments

@Flole998
Copy link

I am running this library on an ESP8266 and noticed the following in the logs of my MQTT Server today:

Oct 24 23:48:23 MQTT-Server  mosquitto: New connection from 10.0.1.6 on port 1883.
Oct 24 23:48:23 MQTT-Server  mosquitto: Client MyESP already connected, closing old connection.
Oct 24 23:48:23 MQTT-Server  mosquitto: Client MyESPdisconnected.
Oct 24 23:48:23 MQTT-Server mosquitto: New client connected from 10.0.1.6 as MyESP (c1, k15).

The code I am running is:

void loop() {
	if (WiFi.status() == WL_CONNECTED) {
		if (!client.connected()) {
			reconnect();
		} else {
			client.loop();
		}
	}
}

void reconnect() {
	if (!client.connected()) {
		if (client.connect(("ESP_" + devname).c_str())) {
			client.subscribe("/MyDev/#");
		}
	}
}

Is there anything wrong with my code? This is happening on a lot of ESPs I am using, I haven't checked if there is one that is not affected.

@knolleary
Copy link
Owner

I think this comes from calling client.loop and client.connected too often - you have them in a tight loop which doesn't give the ESP much room to breathe.

Try adding delay(50) in your loop to see if to improves reliability.

@Argon-
Copy link

Argon- commented Nov 5, 2018

How would you define too often?
Because I also have that a lot but with at least 150ms between each call to loop(). Calling loop() less frequently (say, >1s interval), however, leads to actual disconnects spanning multiple ms of time. That's why I'm currently calling loop() more often: more unnecessary reconnects but less actual disconnects for a considerable amount of time ("considerable" in my case is >1s).

@Flole998
Copy link
Author

Flole998 commented Nov 5, 2018

I've done further investigation and noticed that this is happening when the ESP is renewing it's DHCP Lease. I thought that issue was supposed to be fixed a long time ago already?

@Flole998
Copy link
Author

Issue was due to AP disconnect, not an issue in this library. Thanks to everyone who helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants