Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
a937a97659 | |||
ad96f3e82a | |||
f8a977ddd0 | |||
265016844b |
10
CHANGES.txt
10
CHANGES.txt
@ -1,3 +1,13 @@
|
||||
1.4
|
||||
* Fixed connection lost handling
|
||||
|
||||
1.3
|
||||
* Fixed packet reading bug in PubSubClient.readPacket
|
||||
|
||||
1.2
|
||||
* Fixed compile error when used with arduino-0016 or later
|
||||
|
||||
|
||||
1.1
|
||||
* Reduced size of library
|
||||
* Added support for Will messages
|
||||
|
@ -13,7 +13,6 @@
|
||||
#define MQTTPUBLISH 3<<4
|
||||
#define MQTTSUBSCRIBE 8<<4
|
||||
|
||||
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,int)) : _client(ip,port) {
|
||||
this->callback = callback;
|
||||
}
|
||||
@ -22,7 +21,7 @@ int PubSubClient::connect(char *id) {
|
||||
}
|
||||
|
||||
int PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) {
|
||||
if (!_client.connected()) {
|
||||
if (!connected()) {
|
||||
if (_client.connect()) {
|
||||
nextMsgId = 1;
|
||||
uint8_t d[9] = {0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03};
|
||||
@ -37,7 +36,7 @@ int PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t wi
|
||||
buffer[length++] = 0x02;
|
||||
}
|
||||
buffer[length++] = 0;
|
||||
buffer[length++] = (KEEPALIVE/500);
|
||||
buffer[length++] = (KEEPALIVE/1000);
|
||||
length = writeString(id,buffer,length);
|
||||
if (willTopic) {
|
||||
length = writeString(willTopic,buffer,length);
|
||||
@ -46,26 +45,30 @@ int PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t wi
|
||||
write(MQTTCONNECT,buffer,length);
|
||||
while (!_client.available()) {}
|
||||
uint8_t len = readPacket();
|
||||
|
||||
if (len == 4 && buffer[3] == 0) {
|
||||
lastActivity = millis();
|
||||
return 1;
|
||||
}
|
||||
|
||||
_client.stop();
|
||||
}
|
||||
_client.stop();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t PubSubClient::readByte() {
|
||||
while(!_client.available()) {}
|
||||
return _client.read();
|
||||
}
|
||||
|
||||
uint8_t PubSubClient::readPacket() {
|
||||
uint8_t len = 0;
|
||||
buffer[len++] = _client.read();
|
||||
|
||||
buffer[len++] = readByte();
|
||||
uint8_t multiplier = 1;
|
||||
uint8_t length = 0;
|
||||
uint8_t digit = 0;
|
||||
do {
|
||||
digit = _client.read();
|
||||
digit = readByte();
|
||||
buffer[len++] = digit;
|
||||
length += (digit & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
@ -74,21 +77,22 @@ uint8_t PubSubClient::readPacket() {
|
||||
for (int i = 0;i<length;i++)
|
||||
{
|
||||
if (len < MAX_PACKET_SIZE) {
|
||||
buffer[len++] = _client.read();
|
||||
buffer[len++] = readByte();
|
||||
} else {
|
||||
_client.read();
|
||||
readByte();
|
||||
len = 0; // This will cause the packet to be ignored.
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int PubSubClient::loop() {
|
||||
if (_client.connected()) {
|
||||
if (connected()) {
|
||||
long t = millis();
|
||||
if (t - lastActivity > KEEPALIVE) {
|
||||
_client.write(192);
|
||||
_client.write(0);
|
||||
_client.write((uint8_t)0);
|
||||
lastActivity = t;
|
||||
}
|
||||
if (_client.available()) {
|
||||
@ -109,7 +113,7 @@ int PubSubClient::loop() {
|
||||
}
|
||||
} else if (type == 12) { // PINGREG
|
||||
_client.write(208);
|
||||
_client.write(0);
|
||||
_client.write((uint8_t)0);
|
||||
lastActivity = t;
|
||||
}
|
||||
}
|
||||
@ -125,7 +129,7 @@ int PubSubClient::publish(char* topic, char* payload) {
|
||||
|
||||
|
||||
int PubSubClient::publish(char* topic, uint8_t* payload, uint8_t plength) {
|
||||
if (_client.connected()) {
|
||||
if (connected()) {
|
||||
uint8_t length = writeString(topic,buffer,0);
|
||||
int i;
|
||||
for (i=0;i<plength;i++) {
|
||||
@ -150,7 +154,7 @@ int PubSubClient::write(uint8_t header, uint8_t* buf, uint8_t length) {
|
||||
|
||||
|
||||
void PubSubClient::subscribe(char* topic) {
|
||||
if (_client.connected()) {
|
||||
if (connected()) {
|
||||
uint8_t length = 2;
|
||||
nextMsgId++;
|
||||
buffer[0] = nextMsgId >> 8;
|
||||
@ -163,7 +167,7 @@ void PubSubClient::subscribe(char* topic) {
|
||||
|
||||
void PubSubClient::disconnect() {
|
||||
_client.write(224);
|
||||
_client.write(0);
|
||||
_client.write((uint8_t)0);
|
||||
_client.stop();
|
||||
lastActivity = millis();
|
||||
}
|
||||
@ -183,7 +187,9 @@ uint8_t PubSubClient::writeString(char* string, uint8_t* buf, uint8_t pos) {
|
||||
|
||||
|
||||
int PubSubClient::connected() {
|
||||
return (int)_client.connected();
|
||||
int rc = (int)_client.connected();
|
||||
if (!rc) _client.stop();
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
@ -21,6 +21,7 @@ private:
|
||||
long lastActivity;
|
||||
void (*callback)(char*,uint8_t*,int);
|
||||
uint8_t readPacket();
|
||||
uint8_t readByte();
|
||||
int write(uint8_t header, uint8_t* buf, uint8_t length);
|
||||
uint8_t writeString(char* string, uint8_t* buf, uint8_t pos);
|
||||
public:
|
||||
|
Reference in New Issue
Block a user