-
Notifications
You must be signed in to change notification settings - Fork 240
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
problems with reconnection to broker #71
Comments
Most likely there is some null pointer exception when calling But the difference between the mega2560 and the esp8266 is weird, since the same code is executed on both platforms. Maybe we can check this again when the issue has been fixed. I'll work on this as soon as I'm back. |
I fixed a bug that might have caused a crash when disconnecting without having any previous connection. Can you give the new |
void mqConnect() {
uint8_t loops = 0;
toggle (24, HIGH);
mq.setWill("node/" MQTT_CLIENTID "/status", "0", true, 0); // ... retained, QoS
while (!mq.connect(MQTT_CLIENTID, MQTT_USERNAME, MQTT_KEY)) {
Serial.print('.');
if (++loops >= 2) {
toggle (24, LOW);
return;
}
delay(400);
//mq.disconnect();
}
toggle (24, LOW);
Serial.print(F("\nConnected to mqtt-broker\n"));
mq.subscribe("node/#"); Tested on v2.1.4. On Mega2560 it still does not reconnect after broker has been offline for a while. (code above). If the broker is offline when Arduino boots up, Mega2560 has no problem connecting to the broker. If I uncomment Other info: |
Hello! I'm working on Genuino MKR1000 (SAMD21 + ATWINC1500 WiFi module), I'm using connect and publish functions as described in the example.
When the program is working correctly all messages in Mosquito log are identified with the correct client id; in this case I have client id. (I don't have other clients running). |
Hi all, I just pushed some commits that may fix the metioned problem (check the git history for what has been changed). It would be awesome if you could rerun your tests with master branch of the library before I release a new version |
I just released v2.2.0 (might take some time to appear in the library manager) that fixes more related issues. Can you check again? |
@256dpi I have the same issue using the latest version (v2.2.0).
It run for almost 38 hours without a glitch (thanks for the amazing library). Here is the code I use: void setup() {
mqtt.begin(MQTT_SERVER, client);
mqtt.setOptions(60, false, 2500);
mqtt.onMessage(messageReceived);
mqtt_subscribe();
}
void loop() {
mqtt.loop();
if(!mqtt.connected()) {
mqtt_subscribe();
}
....
}
bool mqtt_subscribe() {
if (!mqtt.connected()) mqtt_connect();
mqtt.subscribe("topic/p/" + IMEI, MQTT_QOS);
mqtt_event_subscribe(); # sending events as soon as the device subscribes
return true;
}
bool mqtt_connect() {
if (!mqtt.connected()) {
Serial.print("\nConnecting to MQTT server...");
while (!mqtt.connect(IMEI.c_str(), MQTT_USER, MQTT_PASS)) {
Serial.print(".");
delay(500);
}
mqtt.setWill("topic/testament", IMEI.c_str());
Serial.print(" [OK]");
}
return true;
} |
Now, with my hardware and v2.2.0 runs without problems. Thanks! |
The first issue, that the code refuse to reconnect when the broker is online again is solved in v2.2.0. Good work! Thanks! |
If I turn off the Mosquitto broker and start it again, the Mega2560-client will not reconnect. The ESP8266 reconnects without a problem.
To be able to reconnect I had to add a 'disconnect' into the connection loop. Now my code looks like this:
On the ESP8266, if I run 'disconnect' without being connected, it reboots.
I have all work-arounds I need now, but it would of course be nice to other beginners if this could be handled by the library.
The text was updated successfully, but these errors were encountered: