Various changes for v1.8 including larger message support and tidied up types.
This commit is contained in:
parent
e4fdabf4db
commit
4d2e5994bc
@ -1,3 +1,10 @@
|
|||||||
|
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
|
1.7
|
||||||
* Improved keepalive handling
|
* Improved keepalive handling
|
||||||
* Updated to the Arduino-1.0 API
|
* Updated to the Arduino-1.0 API
|
||||||
|
@ -11,22 +11,22 @@
|
|||||||
PubSubClient::PubSubClient() : _client() {
|
PubSubClient::PubSubClient() : _client() {
|
||||||
}
|
}
|
||||||
|
|
||||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,int)) : _client() {
|
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->ip = ip;
|
||||||
this->port = port;
|
this->port = port;
|
||||||
}
|
}
|
||||||
int PubSubClient::connect(char *id) {
|
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(this->ip, this->port)) {
|
if (_client.connect(this->ip, this->port)) {
|
||||||
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];
|
||||||
}
|
}
|
||||||
@ -35,8 +35,8 @@ 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);
|
||||||
@ -46,23 +46,23 @@ int PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t wi
|
|||||||
lastOutActivity = millis();
|
lastOutActivity = millis();
|
||||||
lastInActivity = millis();
|
lastInActivity = millis();
|
||||||
while (!_client.available()) {
|
while (!_client.available()) {
|
||||||
long t= millis();
|
unsigned long t= millis();
|
||||||
if (t-lastInActivity > KEEPALIVE) {
|
if (t-lastInActivity > MQTT_KEEPALIVE*1000) {
|
||||||
_client.stop();
|
_client.stop();
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
uint8_t len = readPacket();
|
uint16_t len = readPacket();
|
||||||
|
|
||||||
if (len == 4 && buffer[3] == 0) {
|
if (len == 4 && buffer[3] == 0) {
|
||||||
lastInActivity = millis();
|
lastInActivity = millis();
|
||||||
pingOutstanding = false;
|
pingOutstanding = false;
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_client.stop();
|
_client.stop();
|
||||||
}
|
}
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t PubSubClient::readByte() {
|
uint8_t PubSubClient::readByte() {
|
||||||
@ -70,11 +70,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();
|
||||||
@ -83,9 +83,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();
|
||||||
@ -96,13 +96,13 @@ 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 - lastInActivity > KEEPALIVE) || (t - lastOutActivity > KEEPALIVE)) {
|
if ((t - lastInActivity > MQTT_KEEPALIVE*1000) || (t - lastOutActivity > MQTT_KEEPALIVE*1000)) {
|
||||||
if (pingOutstanding) {
|
if (pingOutstanding) {
|
||||||
_client.stop();
|
_client.stop();
|
||||||
return 0;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
_client.write(MQTTPINGREQ);
|
_client.write(MQTTPINGREQ);
|
||||||
_client.write((uint8_t)0);
|
_client.write((uint8_t)0);
|
||||||
@ -112,15 +112,15 @@ int PubSubClient::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (_client.available()) {
|
if (_client.available()) {
|
||||||
uint8_t len = readPacket();
|
uint16_t len = readPacket();
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
lastInActivity = t;
|
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;
|
||||||
@ -136,56 +136,75 @@ int PubSubClient::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
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();
|
lastOutActivity = millis();
|
||||||
return 0;
|
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() {
|
||||||
@ -196,9 +215,9 @@ void PubSubClient::disconnect() {
|
|||||||
lastOutActivity = 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++;
|
||||||
@ -210,7 +229,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;
|
||||||
|
@ -10,10 +10,12 @@
|
|||||||
#include "Ethernet.h"
|
#include "Ethernet.h"
|
||||||
#include "EthernetClient.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
|
||||||
@ -31,33 +33,37 @@
|
|||||||
#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:
|
||||||
EthernetClient _client;
|
EthernetClient _client;
|
||||||
uint8_t buffer[MAX_PACKET_SIZE];
|
uint8_t buffer[MQTT_MAX_PACKET_SIZE];
|
||||||
uint8_t nextMsgId;
|
uint16_t nextMsgId;
|
||||||
long lastOutActivity;
|
unsigned long lastOutActivity;
|
||||||
long lastInActivity;
|
unsigned long lastInActivity;
|
||||||
bool pingOutstanding;
|
bool pingOutstanding;
|
||||||
void (*callback)(char*,uint8_t*,int);
|
void (*callback)(char*,uint8_t*,unsigned int);
|
||||||
uint8_t readPacket();
|
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;
|
uint8_t *ip;
|
||||||
uint16_t port;
|
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 *);
|
boolean connect(char *);
|
||||||
int connect(char*, char*, uint8_t, uint8_t, 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();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user