Merge pull request #19 from binarybucks/master

Add unsubscribe method
This commit is contained in:
Nicholas O'Leary 2013-01-10 13:14:13 -08:00
commit ae44d21503
2 changed files with 16 additions and 1 deletions

View File

@ -290,7 +290,6 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
return (rc == 1+llen+length); return (rc == 1+llen+length);
} }
boolean PubSubClient::subscribe(char* topic) { boolean PubSubClient::subscribe(char* topic) {
if (connected()) { if (connected()) {
// Leave room in the buffer for header and variable length field // Leave room in the buffer for header and variable length field
@ -308,6 +307,21 @@ boolean PubSubClient::subscribe(char* topic) {
return false; return false;
} }
boolean PubSubClient::unsubscribe(char* topic) {
if (connected()) {
uint16_t length = 5;
nextMsgId++;
if (nextMsgId == 0) {
nextMsgId = 1;
}
buffer[length++] = (nextMsgId >> 8);
buffer[length++] = (nextMsgId & 0xFF);
length = writeString(topic, buffer,length);
return write(MQTTUNSUBSCRIBE|MQTTQOS1,buffer,length-5);
}
return false;
}
void PubSubClient::disconnect() { void PubSubClient::disconnect() {
buffer[0] = MQTTDISCONNECT; buffer[0] = MQTTDISCONNECT;
buffer[1] = 0; buffer[1] = 0;

View File

@ -67,6 +67,7 @@ public:
boolean publish(char *, uint8_t *, unsigned int, boolean); boolean publish(char *, uint8_t *, unsigned int, boolean);
boolean publish_P(char *, uint8_t PROGMEM *, unsigned int, boolean); boolean publish_P(char *, uint8_t PROGMEM *, unsigned int, boolean);
boolean subscribe(char *); boolean subscribe(char *);
boolean unsubscribe(char *);
boolean loop(); boolean loop();
boolean connected(); boolean connected();
}; };