7 Commits
v1.6 ... v1.8

Author SHA1 Message Date
ee378802a9 Merge branch 'master' of github.com:knolleary/pubsubclient
Conflicts:
	PubSubClient/PubSubClient.cpp
	PubSubClient/PubSubClient.h
2012-03-08 22:11:28 +00:00
4d2e5994bc Various changes for v1.8 including larger message support and tidied up types. 2012-03-08 21:54:24 +00:00
6f0db7db0f Merge pull request #5 from mcollina/dns
Added DNS support.
2012-01-28 13:22:16 -08:00
f328d0849f Added DNS support. 2012-01-25 14:36:16 +01:00
e4fdabf4db Changes for beta4 - use EthernetClient not Client 2011-09-12 09:28:06 +01:00
4ad0d5f4ac Updated to the Arduino-1.0 API 2011-08-04 15:38:43 +01:00
fb5f8de2a4 Improved keepalive handling 2011-04-26 20:52:57 +01:00
3 changed files with 155 additions and 76 deletions

View File

@ -1,3 +1,13 @@
1.8
* KeepAlive interval is configurable in PubSubClient.h
* Maximum packet size is configurable in PubSubClient.h
* API change: Return boolean rather than int from various functions
* API change: Length parameter in message callback changed
from int to unsigned int
* Various internal tidy-ups around types
1.7
* Improved keepalive handling
* Updated to the Arduino-1.0 API
1.6 1.6
* Added the ability to publish a retained message * Added the ability to publish a retained message

View File

@ -5,26 +5,43 @@
*/ */
#include "PubSubClient.h" #include "PubSubClient.h"
#include "Client.h" #include <EthernetClient.h>
#include "string.h" #include <string.h>
PubSubClient::PubSubClient() : _client(0) { PubSubClient::PubSubClient() : _client() {
} }
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,int)) : _client(ip,port) { PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int)) : _client() {
this->callback = callback; this->callback = callback;
this->ip = ip;
this->port = port;
} }
int PubSubClient::connect(char *id) {
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int)) : _client() {
this->callback = callback;
this->domain = domain;
this->port = port;
}
boolean PubSubClient::connect(char *id) {
return connect(id,0,0,0,0); return connect(id,0,0,0,0);
} }
int PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) { boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) {
if (!connected()) { if (!connected()) {
if (_client.connect()) { int result = 0;
if (domain != NULL) {
result = _client.connect(this->domain, this->port);
} else {
result = _client.connect(this->ip, this->port);
}
if (result) {
nextMsgId = 1; nextMsgId = 1;
uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p',MQTTPROTOCOLVERSION}; uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p',MQTTPROTOCOLVERSION};
uint8_t length = 0; uint8_t length = 0;
int j; unsigned int j;
for (j = 0;j<9;j++) { for (j = 0;j<9;j++) {
buffer[length++] = d[j]; buffer[length++] = d[j];
} }
@ -33,25 +50,34 @@ int PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t wi
} else { } else {
buffer[length++] = 0x02; buffer[length++] = 0x02;
} }
buffer[length++] = 0; buffer[length++] = ((MQTT_KEEPALIVE) >> 8);
buffer[length++] = (KEEPALIVE/1000); buffer[length++] = ((MQTT_KEEPALIVE) & 0xff);
length = writeString(id,buffer,length); length = writeString(id,buffer,length);
if (willTopic) { if (willTopic) {
length = writeString(willTopic,buffer,length); length = writeString(willTopic,buffer,length);
length = writeString(willMessage,buffer,length); length = writeString(willMessage,buffer,length);
} }
write(MQTTCONNECT,buffer,length); write(MQTTCONNECT,buffer,length);
while (!_client.available()) {} lastOutActivity = millis();
uint8_t len = readPacket(); lastInActivity = millis();
while (!_client.available()) {
unsigned long t= millis();
if (t-lastInActivity > MQTT_KEEPALIVE*1000) {
_client.stop();
return false;
}
}
uint16_t len = readPacket();
if (len == 4 && buffer[3] == 0) { if (len == 4 && buffer[3] == 0) {
lastActivity = millis(); lastInActivity = millis();
return 1; pingOutstanding = false;
return true;
} }
} }
_client.stop(); _client.stop();
} }
return 0; return false;
} }
uint8_t PubSubClient::readByte() { uint8_t PubSubClient::readByte() {
@ -59,11 +85,11 @@ uint8_t PubSubClient::readByte() {
return _client.read(); return _client.read();
} }
uint8_t PubSubClient::readPacket() { uint16_t PubSubClient::readPacket() {
uint8_t len = 0; uint16_t len = 0;
buffer[len++] = readByte(); buffer[len++] = readByte();
uint8_t multiplier = 1; uint8_t multiplier = 1;
uint8_t length = 0; uint16_t length = 0;
uint8_t digit = 0; uint8_t digit = 0;
do { do {
digit = readByte(); digit = readByte();
@ -72,9 +98,9 @@ uint8_t PubSubClient::readPacket() {
multiplier *= 128; multiplier *= 128;
} while ((digit & 128) != 0); } while ((digit & 128) != 0);
for (int i = 0;i<length;i++) for (uint16_t i = 0;i<length;i++)
{ {
if (len < MAX_PACKET_SIZE) { if (len < MQTT_MAX_PACKET_SIZE) {
buffer[len++] = readByte(); buffer[len++] = readByte();
} else { } else {
readByte(); readByte();
@ -85,23 +111,31 @@ uint8_t PubSubClient::readPacket() {
return len; return len;
} }
int PubSubClient::loop() { boolean PubSubClient::loop() {
if (connected()) { if (connected()) {
long t = millis(); unsigned long t = millis();
if (t - lastActivity > KEEPALIVE) { if ((t - lastInActivity > MQTT_KEEPALIVE*1000) || (t - lastOutActivity > MQTT_KEEPALIVE*1000)) {
_client.write(MQTTPINGREQ); if (pingOutstanding) {
_client.write((uint8_t)0); _client.stop();
lastActivity = t; return false;
} else {
_client.write(MQTTPINGREQ);
_client.write((uint8_t)0);
lastOutActivity = t;
lastInActivity = t;
pingOutstanding = true;
}
} }
if (_client.available()) { if (_client.available()) {
uint8_t len = readPacket(); uint16_t len = readPacket();
if (len > 0) { if (len > 0) {
lastInActivity = t;
uint8_t type = buffer[0]&0xF0; uint8_t type = buffer[0]&0xF0;
if (type == MQTTPUBLISH) { if (type == MQTTPUBLISH) {
if (callback) { if (callback) {
uint8_t tl = (buffer[2]<<3)+buffer[3]; uint16_t tl = (buffer[2]<<8)+buffer[3];
char topic[tl+1]; char topic[tl+1];
for (int i=0;i<tl;i++) { for (uint16_t i=0;i<tl;i++) {
topic[i] = buffer[4+i]; topic[i] = buffer[4+i];
} }
topic[tl] = 0; topic[tl] = 0;
@ -112,71 +146,93 @@ int PubSubClient::loop() {
} else if (type == MQTTPINGREQ) { } else if (type == MQTTPINGREQ) {
_client.write(MQTTPINGRESP); _client.write(MQTTPINGRESP);
_client.write((uint8_t)0); _client.write((uint8_t)0);
lastActivity = t; } else if (type == MQTTPINGRESP) {
pingOutstanding = false;
} }
} }
} }
return 1; return true;
} }
return 0; return false;
} }
int PubSubClient::publish(char* topic, char* payload) { boolean PubSubClient::publish(char* topic, char* payload) {
return publish(topic,(uint8_t*)payload,strlen(payload)); return publish(topic,(uint8_t*)payload,strlen(payload),false);
} }
int PubSubClient::publish(char* topic, uint8_t* payload, uint8_t plength) { boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plength) {
return publish(topic, payload, plength, 0); return publish(topic, payload, plength, false);
} }
int PubSubClient::publish(char* topic, uint8_t* payload, uint8_t plength, uint8_t retained) { boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plength, boolean retained) {
if (connected()) { if (connected()) {
uint8_t length = writeString(topic,buffer,0); uint16_t length = writeString(topic,buffer,false);
int i; uint16_t i;
for (i=0;i<plength;i++) { for (i=0;i<plength;i++) {
buffer[length++] = payload[i]; buffer[length++] = payload[i];
} }
uint8_t header = MQTTPUBLISH; uint8_t header = MQTTPUBLISH;
if (retained != 0) { if (retained) {
header |= 1; header |= 1;
} }
write(header,buffer,length); return write(header,buffer,length);
return 1;
} }
return 0; return false;
} }
int PubSubClient::write(uint8_t header, uint8_t* buf, uint8_t length) { boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
_client.write(header); uint8_t lenBuf[4];
_client.write(length); uint8_t llen = 0;
_client.write(buf,length); uint8_t digit;
return 0; uint8_t pos = 0;
uint8_t rc;
uint8_t len = length;
do {
digit = len % 128;
len = len / 128;
if (len > 0) {
digit |= 0x80;
}
lenBuf[pos++] = digit;
llen++;
} while(len>0);
rc = _client.write(header);
rc += _client.write(lenBuf,llen);
rc += _client.write(buf,length);
lastOutActivity = millis();
return (rc == 1+llen+length);
} }
void PubSubClient::subscribe(char* topic) { boolean PubSubClient::subscribe(char* topic) {
if (connected()) { if (connected()) {
uint8_t length = 2; uint16_t length = 2;
nextMsgId++; nextMsgId++;
if (nextMsgId == 0) {
nextMsgId = 1;
}
buffer[0] = nextMsgId >> 8; buffer[0] = nextMsgId >> 8;
buffer[1] = nextMsgId - (buffer[0]<<8); buffer[1] = nextMsgId - (buffer[0]<<8);
length = writeString(topic, buffer,length); length = writeString(topic, buffer,length);
buffer[length++] = 0; // Only do QoS 0 subs buffer[length++] = 0; // Only do QoS 0 subs
write(MQTTSUBSCRIBE,buffer,length); return write(MQTTSUBSCRIBE|MQTTQOS1,buffer,length);
} }
return false;
} }
void PubSubClient::disconnect() { void PubSubClient::disconnect() {
_client.write(MQTTDISCONNECT); _client.write(MQTTDISCONNECT);
_client.write((uint8_t)0); _client.write((uint8_t)0);
_client.stop(); _client.stop();
lastActivity = millis(); lastInActivity = millis();
lastOutActivity = millis();
} }
uint8_t PubSubClient::writeString(char* string, uint8_t* buf, uint8_t pos) { uint16_t PubSubClient::writeString(char* string, uint8_t* buf, uint16_t pos) {
char* idp = string; char* idp = string;
uint8_t i = 0; uint16_t i = 0;
pos += 2; pos += 2;
while (*idp) { while (*idp) {
buf[pos++] = *idp++; buf[pos++] = *idp++;
@ -188,7 +244,7 @@ uint8_t PubSubClient::writeString(char* string, uint8_t* buf, uint8_t pos) {
} }
int PubSubClient::connected() { boolean PubSubClient::connected() {
int rc = (int)_client.connected(); int rc = (int)_client.connected();
if (!rc) _client.stop(); if (!rc) _client.stop();
return rc; return rc;

View File

@ -7,12 +7,15 @@
#ifndef PubSubClient_h #ifndef PubSubClient_h
#define PubSubClient_h #define PubSubClient_h
#include "Client.h" #include "Ethernet.h"
#include "EthernetClient.h"
#define MAX_PACKET_SIZE 128 // MQTT_MAX_PACKET_SIZE : Maximum packet size
#define KEEPALIVE 15000 // max value = 255000 #define MQTT_MAX_PACKET_SIZE 128
// MQTT_KEEPALIVE : keepAlive interval in Seconds
#define MQTT_KEEPALIVE 15
// from mqtt-v3r1
#define MQTTPROTOCOLVERSION 3 #define MQTTPROTOCOLVERSION 3
#define MQTTCONNECT 1 << 4 // Client request to connect to Server #define MQTTCONNECT 1 << 4 // Client request to connect to Server
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment #define MQTTCONNACK 2 << 4 // Connect Acknowledgment
@ -30,29 +33,39 @@
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting #define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
#define MQTTReserved 15 << 4 // Reserved #define MQTTReserved 15 << 4 // Reserved
#define MQTTQOS0 (0 << 1)
#define MQTTQOS1 (1 << 1)
#define MQTTQOS2 (2 << 1)
class PubSubClient { class PubSubClient {
private: private:
Client _client; EthernetClient _client;
uint8_t buffer[MAX_PACKET_SIZE]; uint8_t buffer[MQTT_MAX_PACKET_SIZE];
uint8_t nextMsgId; uint16_t nextMsgId;
long lastActivity; unsigned long lastOutActivity;
void (*callback)(char*,uint8_t*,int); unsigned long lastInActivity;
uint8_t readPacket(); bool pingOutstanding;
void (*callback)(char*,uint8_t*,unsigned int);
uint16_t readPacket();
uint8_t readByte(); uint8_t readByte();
int write(uint8_t header, uint8_t* buf, uint8_t length); boolean write(uint8_t header, uint8_t* buf, uint16_t length);
uint8_t writeString(char* string, uint8_t* buf, uint8_t pos); uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);
uint8_t *ip;
char* domain;
uint16_t port;
public: public:
PubSubClient(); PubSubClient();
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,int)); PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int));
int connect(char *); PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int));
int connect(char*, char*, uint8_t, uint8_t, char*); boolean connect(char *);
boolean connect(char*, char*, uint8_t, uint8_t, char*);
void disconnect(); void disconnect();
int publish(char *, char *); boolean publish(char *, char *);
int publish(char *, uint8_t *, uint8_t); boolean publish(char *, uint8_t *, unsigned int);
int publish(char *, uint8_t *, uint8_t, uint8_t); boolean publish(char *, uint8_t *, unsigned int, boolean);
void subscribe(char *); boolean subscribe(char *);
int loop(); boolean loop();
int connected(); boolean connected();
}; };