-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
Mqtt Fails to connect #66
Comments
Parameters : char server[] = "m13.cloudmqtt.com"; // Servevor Mqtt Nuevo Basically this is my loop
Sometimes it connects, but if reset it, when trying again , won't connect again I was able to get the log from CloudMqtt: 1502718718: New connection from 179.29.45.169 on port 18509. |
I also have this issue running the example mqtttest.ino on the Photon. Could the issue be on the TCPClient lib? |
@imomo0 |
I had a similar issue when dealing with flaky WiFi connections and had to implement a write timeout in the library, this seems to solve the problem so far. I based my changes off an older version so would need to re-port it before doing a pull request. If this is useful to someone I can try to port it sooner so they can test my changes. If someone else wants to do the port here are some of the changes I did: I replaced all _client.write(...) with this: bool MQTT::writeBuffer(const uint8_t *b, size_t s) { if ( timeout < 0 ) { return s != _client.write(b,s); } size_t ptr = 0; system_tick_t start = millis(); _client.clearWriteError(); long tout = timeout; while (ptr < s && tout >= 0) { size_t rlen = _client.write(&b[ptr],s-ptr,tout); if (_client.getWriteError() != 0 ) { break; } ptr +=rlen; tout = (system_tick_t)timeout+(system_tick_t)start-(system_tick_t)millis(); } if ( ptr != s ) { _client.stop(); return false; } return true; } This now returns true/false so the MQTT::write(..) needs to check for true instead of comparing the length. I added a setter for timeout and defaulted it to -1 so the behavior is not changed if you don't set a timeout. I allow for a timeout of 0 which is truly non blocking but don't think it is useful, you need to use a more complex Async stye library if you want pure non blocking. My testing was done with a timeout of 5000 (5 seconds) NOTE: When adding timeout to the TCPClient.write you need that loop given it will return even when only part of the data is written. |
Sometimes the client fails to connect for some time. I have to reset Electron so that the entire connection restarts ( i've checked and the internet connectivity is ok and even send a mqtt.disconnect() before trying a connection). Could it be clean session needed? Thanks
The text was updated successfully, but these errors were encountered: