diff --git a/PubSubClient/src/PubSubClient.cpp b/PubSubClient/src/PubSubClient.cpp index 60d81be..d089742 100755 --- a/PubSubClient/src/PubSubClient.cpp +++ b/PubSubClient/src/PubSubClient.cpp @@ -421,10 +421,25 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { for (int i=0;iwrite(buf+(4-llen),length+1+llen); +#ifdef MQTT_MAX_TRANSFER_SIZE + uint8_t* writeBuf = buf+(4-llen); + uint8_t bytesRemaining = length+1+llen; + uint8_t bytesToWrite; + boolean result = true; + while((bytesRemaining > 0) && result) { + bytesToWrite = (bytesRemaining > MQTT_MAX_TRANSFER_SIZE)?MQTT_MAX_TRANSFER_SIZE:bytesRemaining; + rc = _client->write(writeBuf,bytesToWrite); + result = (rc == bytesToWrite); + bytesRemaining -= rc; + writeBuf += rc; + } + return result; +#else + rc = _client->write(buf+(4-llen),length+1+llen); lastOutActivity = millis(); return (rc == 1+llen+length); +#endif } boolean PubSubClient::subscribe(const char* topic) { @@ -506,8 +521,9 @@ boolean PubSubClient::connected() { if (!rc) { if (this->_state == MQTT_CONNECTED) { this->_state = MQTT_CONNECTION_LOST; + _client->flush(); + _client->stop(); } - _client->stop(); } } return rc; diff --git a/PubSubClient/src/PubSubClient.h b/PubSubClient/src/PubSubClient.h index 5c2e5b6..6a967c7 100755 --- a/PubSubClient/src/PubSubClient.h +++ b/PubSubClient/src/PubSubClient.h @@ -19,13 +19,17 @@ //#define MQTT_VERSION MQTT_VERSION_3_1 #define MQTT_VERSION MQTT_VERSION_3_1_1 - // MQTT_MAX_PACKET_SIZE : Maximum packet size #define MQTT_MAX_PACKET_SIZE 128 // MQTT_KEEPALIVE : keepAlive interval in Seconds #define MQTT_KEEPALIVE 15 +// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client +// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to +// pass the entire MQTT packet in each write call. +//#define MQTT_MAX_TRANSFER_SIZE 80 + // Possible values for client.state() #define MQTT_CONNECTION_TIMEOUT -4 #define MQTT_CONNECTION_LOST -3