From 0ec1895128985c9d4e10a7f69bfe3876d5299e60 Mon Sep 17 00:00:00 2001 From: hg Date: Wed, 6 May 2015 19:09:18 +0200 Subject: [PATCH] tracking down and fixing large payload issue --- MQTT/PubSubClient.cpp | 36 ++++++++++++++++++++++++++++++++---- WiFi/WiFiClient.cpp | 2 ++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/MQTT/PubSubClient.cpp b/MQTT/PubSubClient.cpp index 1f8a19c..d931b8a 100755 --- a/MQTT/PubSubClient.cpp +++ b/MQTT/PubSubClient.cpp @@ -39,7 +39,8 @@ boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_ } boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) { - if (!connected()) { +// if (!connected()) { + if (true) { int result = 0; if (domain != NULL) { @@ -213,7 +214,10 @@ boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plengt if (retained) { header |= 1; } + // Serial.print("pub len: "); Serial.println(length-5); return write(header,buffer,length-5); + } else { + // Serial.println("connection lost"); } return false; } @@ -283,10 +287,29 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) { for (int i=0;iwrite(buf+(4-llen),length+1+llen); + +// Serial.print("write len: "); Serial.println(length+1+llen); +// size_t olen = length + 1 + llen; +// rc = _client->write(buf+(4-llen),length+1+llen); + + const size_t SEND_AT_ONCE = 64; + size_t remains = length + 1 + llen; + // Serial.print("write len: "); Serial.println(remains); + const uint8_t *writebuf = buf + (4 - llen); + bool result = true; + while ((remains > 0) && result) { + size_t actuallySendChars = (remains > SEND_AT_ONCE) ? SEND_AT_ONCE : remains; + // Serial.print("tbs: "); Serial.println(actuallySendChars); + size_t sentChars = _client->write(writebuf, actuallySendChars); + result = sentChars == actuallySendChars; + remains -= sentChars; + writebuf += sentChars; + } + lastOutActivity = millis(); - return (rc == 1+llen+length); +// return (rc == 1+llen+length); + return result; } @@ -331,7 +354,12 @@ uint16_t PubSubClient::writeString(char* string, uint8_t* buf, uint16_t pos) { boolean PubSubClient::connected() { int rc = (int)_client->connected(); - if (!rc) _client->stop(); + //Serial.print("rc: "); Serial.println(rc); + if (!rc) { + //Serial.println("would stop"); + _client->stop(); + // while (true); + } return rc; } diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index a88d0dc..2630f7e 100644 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -161,6 +161,8 @@ void WiFiClient::stop() { } uint8_t WiFiClient::connected() { + // Serial.print("sock: "); Serial.println(_sock); + // Serial.print("status: "); Serial.println(status()); if (_sock == 255) { return 0;