256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 232 forks source link

Can't connect when set clean session flag and will message send right after reconnected #253

Closed chauvoluuhuong closed 3 years ago

chauvoluuhuong commented 3 years ago

Hi,

I got two problems.

Firstly is about connecting with the clean session flag set to false (I would like to have a persistent session ). After I set that flag. The device disconnected right after connected (of course it tried to reconnect it but it doesn't) Here is my code flow

mqttClient.setCleanSession(false); mqttClient.setWill(BOARD_CONNECTING_STATUS_TOPIC, str.c_str()); mqttClient.setKeepAlive(20); mqttClient.connect(clientId.c_str(), userName.c_str(), userName.c_str());

Of course i tried to comment out the line "mqttClient.setCleanSession(false)" and my device connected normally.

Secondary is about will topic triggered right after reconnected I used the same code lines above. I tried to turn the device off and will message triggered. But after I turn the device on to make it connect again The will topic fired right after my device connected.

I used ESP32

chauvoluuhuong commented 3 years ago

Sorry, i my bad,

I did a lot of test and realized that I didn't set Client ID, so that the device use random client id for taking connection.