18 Commits
v2.3 ... v2.6

Author SHA1 Message Date
341661671b Revert breaking change to callback signature 2016-02-02 07:40:48 +00:00
4739ca0802 Update library files
closes #44
2016-01-31 20:53:44 +00:00
36bb1ffa6a Merge pull request #96 from ElvisTheKing/patch-1
correct handling of dns fauilure
2016-01-31 20:48:43 +00:00
83b69a766e Merge pull request #120 from tomkcook/master
Use std::function on ESP8266 platform.
2016-01-31 20:47:32 +00:00
baeb59e263 Merge pull request #119 from skorokithakis/master
Add definable parameters.
2016-01-31 20:41:19 +00:00
67eba6dad4 Moved #include of <functional> 2016-01-27 12:41:50 +00:00
98a9c296f6 Made use of std::function ESP8266-specific. 2016-01-27 12:39:43 +00:00
68400b7b6c Add definable parameters. 2016-01-26 12:52:38 +02:00
21b75a2c4a Changed callback type. 2016-01-26 09:50:27 +00:00
830f34c7d0 correct handling of dns fauilure
In case of domain name resolution error result can be negative (see Dns.cpp:46)
2015-12-04 04:10:18 +03:00
0bb4efcea5 Update for 2.4 2015-11-21 20:56:32 +00:00
31521085ea Increase rc of write to uint16 to match max possible length
Fixes #85
2015-11-21 20:36:21 +00:00
8a1d7fb620 Merge pull request #93 from vicatcu/master
Implement timeout behavior in readByte / readPacket
2015-11-21 20:20:35 +00:00
803f54b0bd changes to use #define MQTT_SOCKET_TIMEOUT instead of dynamic read timeout interval, per comments from @knolleary on https://github.com/knolleary/pubsubclient/issues/87 2015-11-19 13:58:23 -05:00
6f97ea04f2 minor cleanup 2015-11-19 09:23:40 -05:00
6bb06187b7 added optional timeout setting so that readByte can be escaped 2015-11-18 17:19:41 -05:00
efebd2e5e4 Merge pull request #82 from e-lin/master
Match the length of type for writing data
2015-10-04 19:35:29 +01:00
5cdadf43da Match the length of type for writing data
In MQTT_MAX_TRANSFER_SIZE case, the variable bytesRemaining needs to match the type of data length.
2015-10-04 22:16:27 +09:00
5 changed files with 74 additions and 16 deletions

View File

@ -1,3 +1,8 @@
2.4
* Add MQTT_SOCKET_TIMEOUT to prevent it blocking indefinitely
whilst waiting for inbound data
* Fixed return code when publishing >256 bytes
2.3 2.3
* Add publish(topic,payload,retained) function * Add publish(topic,payload,retained) function

17
library.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "PubSubClient",
"keywords": "ethernet, mqtt, m2m, iot",
"description": "A client library for MQTT messaging. MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.",
"repository": {
"type": "git",
"url": "https://github.com/knolleary/pubsubclient.git"
},
"version": "2.6",
"exclude": "tests",
"examples": "examples/*/*.ino",
"frameworks": "arduino",
"platforms": [
"atmelavr",
"espressif"
]
}

View File

@ -1,5 +1,5 @@
name=PubSubClient name=PubSubClient
version=2.3 version=2.6
author=Nick O'Leary <nick.oleary@gmail.com> author=Nick O'Leary <nick.oleary@gmail.com>
maintainer=Nick O'Leary <nick.oleary@gmail.com> maintainer=Nick O'Leary <nick.oleary@gmail.com>
sentence=A client library for MQTT messaging. sentence=A client library for MQTT messaging.

View File

@ -122,7 +122,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
} else { } else {
result = _client->connect(this->ip, this->port); result = _client->connect(this->ip, this->port);
} }
if (result) { if (result == 1) {
nextMsgId = 1; nextMsgId = 1;
// Leave room in the buffer for header and variable length field // Leave room in the buffer for header and variable length field
uint16_t length = 5; uint16_t length = 5;
@ -177,7 +177,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
while (!_client->available()) { while (!_client->available()) {
unsigned long t = millis(); unsigned long t = millis();
if (t-lastInActivity > MQTT_KEEPALIVE*1000UL) { if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) {
_state = MQTT_CONNECTION_TIMEOUT; _state = MQTT_CONNECTION_TIMEOUT;
_client->stop(); _client->stop();
return false; return false;
@ -205,14 +205,33 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
return true; return true;
} }
uint8_t PubSubClient::readByte() { // reads a byte into result
while(!_client->available()) {} boolean PubSubClient::readByte(uint8_t * result) {
return _client->read(); uint32_t previousMillis = millis();
while(!_client->available()) {
uint32_t currentMillis = millis();
if(currentMillis - previousMillis >= ((int32_t) MQTT_SOCKET_TIMEOUT * 1000)){
return false;
}
}
*result = _client->read();
return true;
}
// reads a byte into result[*index] and increments index
boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
uint16_t current_index = *index;
uint8_t * write_address = &(result[current_index]);
if(readByte(write_address)){
*index = current_index + 1;
return true;
}
return false;
} }
uint16_t PubSubClient::readPacket(uint8_t* lengthLength) { uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
uint16_t len = 0; uint16_t len = 0;
buffer[len++] = readByte(); if(!readByte(buffer, &len)) return 0;
bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH; bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH;
uint32_t multiplier = 1; uint32_t multiplier = 1;
uint16_t length = 0; uint16_t length = 0;
@ -221,7 +240,7 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
uint8_t start = 0; uint8_t start = 0;
do { do {
digit = readByte(); if(!readByte(&digit)) return 0;
buffer[len++] = digit; buffer[len++] = digit;
length += (digit & 127) * multiplier; length += (digit & 127) * multiplier;
multiplier *= 128; multiplier *= 128;
@ -230,8 +249,8 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
if (isPublish) { if (isPublish) {
// Read in topic length to calculate bytes to skip over for Stream writing // Read in topic length to calculate bytes to skip over for Stream writing
buffer[len++] = readByte(); if(!readByte(buffer, &len)) return 0;
buffer[len++] = readByte(); if(!readByte(buffer, &len)) return 0;
skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2]; skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2];
start = 2; start = 2;
if (buffer[0]&MQTTQOS1) { if (buffer[0]&MQTTQOS1) {
@ -241,7 +260,7 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
} }
for (uint16_t i = start;i<length;i++) { for (uint16_t i = start;i<length;i++) {
digit = readByte(); if(!readByte(&digit)) return 0;
if (this->stream) { if (this->stream) {
if (isPublish && len-*lengthLength-2>skip) { if (isPublish && len-*lengthLength-2>skip) {
this->stream->write(digit); this->stream->write(digit);
@ -409,7 +428,7 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
uint8_t llen = 0; uint8_t llen = 0;
uint8_t digit; uint8_t digit;
uint8_t pos = 0; uint8_t pos = 0;
uint8_t rc; uint16_t rc;
uint16_t len = length; uint16_t len = length;
do { do {
digit = len % 128; digit = len % 128;
@ -428,7 +447,7 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
#ifdef MQTT_MAX_TRANSFER_SIZE #ifdef MQTT_MAX_TRANSFER_SIZE
uint8_t* writeBuf = buf+(4-llen); uint8_t* writeBuf = buf+(4-llen);
uint8_t bytesRemaining = length+1+llen; uint16_t bytesRemaining = length+1+llen; //Match the length type
uint8_t bytesToWrite; uint8_t bytesToWrite;
boolean result = true; boolean result = true;
while((bytesRemaining > 0) && result) { while((bytesRemaining > 0) && result) {
@ -551,7 +570,7 @@ PubSubClient& PubSubClient::setServer(const char * domain, uint16_t port) {
return *this; return *this;
} }
PubSubClient& PubSubClient::setCallback(void(*callback)(char*,uint8_t*,unsigned int)){ PubSubClient& PubSubClient::setCallback(MQTT_CALLBACK_SIGNATURE) {
this->callback = callback; this->callback = callback;
return *this; return *this;
} }

View File

@ -17,13 +17,24 @@
// MQTT_VERSION : Pick the version // MQTT_VERSION : Pick the version
//#define MQTT_VERSION MQTT_VERSION_3_1 //#define MQTT_VERSION MQTT_VERSION_3_1
#ifndef MQTT_VERSION
#define MQTT_VERSION MQTT_VERSION_3_1_1 #define MQTT_VERSION MQTT_VERSION_3_1_1
#endif
// MQTT_MAX_PACKET_SIZE : Maximum packet size // MQTT_MAX_PACKET_SIZE : Maximum packet size
#ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 128 #define MQTT_MAX_PACKET_SIZE 128
#endif
// MQTT_KEEPALIVE : keepAlive interval in Seconds // MQTT_KEEPALIVE : keepAlive interval in Seconds
#ifndef MQTT_KEEPALIVE
#define MQTT_KEEPALIVE 15 #define MQTT_KEEPALIVE 15
#endif
// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
#ifndef MQTT_SOCKET_TIMEOUT
#define MQTT_SOCKET_TIMEOUT 15
#endif
// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client // 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 // in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
@ -62,7 +73,12 @@
#define MQTTQOS1 (1 << 1) #define MQTTQOS1 (1 << 1)
#define MQTTQOS2 (2 << 1) #define MQTTQOS2 (2 << 1)
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*,uint8_t*,unsigned int) #ifdef ESP8266
#include <functional>
#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
#else
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
#endif
class PubSubClient { class PubSubClient {
private: private:
@ -74,7 +90,8 @@ private:
bool pingOutstanding; bool pingOutstanding;
MQTT_CALLBACK_SIGNATURE; MQTT_CALLBACK_SIGNATURE;
uint16_t readPacket(uint8_t*); uint16_t readPacket(uint8_t*);
uint8_t readByte(); boolean readByte(uint8_t * result);
boolean readByte(uint8_t * result, uint16_t * index);
boolean write(uint8_t header, uint8_t* buf, uint16_t length); boolean write(uint8_t header, uint8_t* buf, uint16_t length);
uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos); uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
IPAddress ip; IPAddress ip;