ping flushes input, tries multiple times

This commit is contained in:
ladyada 2015-10-22 13:03:55 -04:00
parent 2aed9ae737
commit 7c2b558ee9
2 changed files with 12 additions and 3 deletions

View File

@ -368,13 +368,20 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
return subscriptions[i]; return subscriptions[i];
} }
bool Adafruit_MQTT::ping(uint8_t num = 1) { void Adafruit_MQTT::flushIncoming(uint16_t timeout) {
// flush input!
DEBUG_PRINTLN(F("Flushing input buffer"));
while (readPacket(buffer, MAXBUFFERSIZE, timeout));
}
bool Adafruit_MQTT::ping(uint8_t num) {
flushIncoming(100);
while (num--) { while (num--) {
// Construct and send ping packet. // Construct and send ping packet.
uint8_t len = pingPacket(buffer); uint8_t len = pingPacket(buffer);
if (!sendPacket(buffer, len)) if (!sendPacket(buffer, len))
return false; continue;
// Process ping reply. // Process ping reply.
len = readPacket(buffer, 2, PING_TIMEOUT_MS); len = readPacket(buffer, 2, PING_TIMEOUT_MS);

View File

@ -178,7 +178,7 @@ class Adafruit_MQTT {
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout=0); Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout=0);
// Ping the server to ensure the connection is still alive. // Ping the server to ensure the connection is still alive.
bool ping(uint8_t n); bool ping(uint8_t n = 1);
protected: protected:
// Interface that subclasses need to implement: // Interface that subclasses need to implement:
@ -215,6 +215,8 @@ class Adafruit_MQTT {
private: private:
Adafruit_MQTT_Subscribe *subscriptions[MAXSUBSCRIPTIONS]; Adafruit_MQTT_Subscribe *subscriptions[MAXSUBSCRIPTIONS];
void flushIncoming(uint16_t timeout);
// Functions to generate MQTT packets. // Functions to generate MQTT packets.
uint8_t connectPacket(uint8_t *packet); uint8_t connectPacket(uint8_t *packet);
uint8_t disconnectPacket(uint8_t *packet); uint8_t disconnectPacket(uint8_t *packet);