Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
ecedcb804f | |||
569ada9937 | |||
97e9614780 | |||
6f23967ee1 | |||
a971ed4a2c | |||
2d43044338 | |||
9607eefa0f | |||
62693d406c | |||
7e53f612f2 | |||
87fb3a9895 | |||
18fe49c070 | |||
cc7e1c45c7 | |||
ee378802a9 | |||
4d2e5994bc | |||
6f0db7db0f | |||
f328d0849f | |||
e4fdabf4db | |||
4ad0d5f4ac | |||
fb5f8de2a4 | |||
fca33c7f5a | |||
632c44a4d1 | |||
ce9fff9349 | |||
31f3742040 | |||
ef565d8d98 | |||
ef75340e6e | |||
07547e2ec0 |
22
CHANGES.txt
22
CHANGES.txt
@ -1,22 +0,0 @@
|
||||
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
|
||||
* Clarified licensing - see LICENSE.txt
|
||||
|
||||
|
||||
1.0
|
||||
* Only Quality of Service (QOS) 0 messaging is supported
|
||||
* The maximum message size, including header, is 128 bytes
|
||||
* The keepalive interval is set to 30 seconds
|
||||
* No support for Will messages
|
||||
|
196
PubSubClient.cpp
196
PubSubClient.cpp
@ -1,196 +0,0 @@
|
||||
/*
|
||||
PubSubClient.cpp - A simple client for MQTT.
|
||||
Nicholas O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#include "WConstants.h"
|
||||
#include "PubSubClient.h"
|
||||
#include "Client.h"
|
||||
#include "string.h"
|
||||
|
||||
#define MQTTCONNECT 1<<4
|
||||
#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;
|
||||
}
|
||||
int PubSubClient::connect(char *id) {
|
||||
return connect(id,0,0,0,0);
|
||||
}
|
||||
|
||||
int PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) {
|
||||
if (!connected()) {
|
||||
if (_client.connect()) {
|
||||
nextMsgId = 1;
|
||||
uint8_t d[9] = {0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03};
|
||||
uint8_t length = 0;
|
||||
int j;
|
||||
for (j = 0;j<9;j++) {
|
||||
buffer[length++] = d[j];
|
||||
}
|
||||
if (willTopic) {
|
||||
buffer[length++] = 0x06|(willQos<<3)|(willRetain<<5);
|
||||
} else {
|
||||
buffer[length++] = 0x02;
|
||||
}
|
||||
buffer[length++] = 0;
|
||||
buffer[length++] = (KEEPALIVE/1000);
|
||||
length = writeString(id,buffer,length);
|
||||
if (willTopic) {
|
||||
length = writeString(willTopic,buffer,length);
|
||||
length = writeString(willMessage,buffer,length);
|
||||
}
|
||||
write(MQTTCONNECT,buffer,length);
|
||||
while (!_client.available()) {}
|
||||
uint8_t len = readPacket();
|
||||
|
||||
if (len == 4 && buffer[3] == 0) {
|
||||
lastActivity = millis();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
_client.stop();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t PubSubClient::readByte() {
|
||||
while(!_client.available()) {}
|
||||
return _client.read();
|
||||
}
|
||||
|
||||
uint8_t PubSubClient::readPacket() {
|
||||
uint8_t len = 0;
|
||||
buffer[len++] = readByte();
|
||||
uint8_t multiplier = 1;
|
||||
uint8_t length = 0;
|
||||
uint8_t digit = 0;
|
||||
do {
|
||||
digit = readByte();
|
||||
buffer[len++] = digit;
|
||||
length += (digit & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
} while ((digit & 128) != 0);
|
||||
|
||||
for (int i = 0;i<length;i++)
|
||||
{
|
||||
if (len < MAX_PACKET_SIZE) {
|
||||
buffer[len++] = readByte();
|
||||
} else {
|
||||
readByte();
|
||||
len = 0; // This will cause the packet to be ignored.
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int PubSubClient::loop() {
|
||||
if (connected()) {
|
||||
long t = millis();
|
||||
if (t - lastActivity > KEEPALIVE) {
|
||||
_client.write(192);
|
||||
_client.write((uint8_t)0);
|
||||
lastActivity = t;
|
||||
}
|
||||
if (_client.available()) {
|
||||
uint8_t len = readPacket();
|
||||
if (len > 0) {
|
||||
uint8_t type = buffer[0]>>4;
|
||||
if (type == 3) { // PUBLISH
|
||||
if (callback) {
|
||||
uint8_t tl = (buffer[2]<<3)+buffer[3];
|
||||
char topic[tl+1];
|
||||
for (int i=0;i<tl;i++) {
|
||||
topic[i] = buffer[4+i];
|
||||
}
|
||||
topic[tl] = 0;
|
||||
// ignore msgID - only support QoS 0 subs
|
||||
uint8_t *payload = buffer+4+tl;
|
||||
callback(topic,payload,len-4-tl);
|
||||
}
|
||||
} else if (type == 12) { // PINGREG
|
||||
_client.write(208);
|
||||
_client.write((uint8_t)0);
|
||||
lastActivity = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PubSubClient::publish(char* topic, char* payload) {
|
||||
return publish(topic,(uint8_t*)payload,strlen(payload));
|
||||
}
|
||||
|
||||
|
||||
int PubSubClient::publish(char* topic, uint8_t* payload, uint8_t plength) {
|
||||
if (connected()) {
|
||||
uint8_t length = writeString(topic,buffer,0);
|
||||
int i;
|
||||
for (i=0;i<plength;i++) {
|
||||
buffer[length++] = payload[i];
|
||||
}
|
||||
//header |= 1; retain
|
||||
write(MQTTPUBLISH,buffer,length);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int PubSubClient::write(uint8_t header, uint8_t* buf, uint8_t length) {
|
||||
_client.write(header);
|
||||
_client.write(length);
|
||||
for (int i=0;i<length;i++) {
|
||||
_client.write(buf[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void PubSubClient::subscribe(char* topic) {
|
||||
if (connected()) {
|
||||
uint8_t length = 2;
|
||||
nextMsgId++;
|
||||
buffer[0] = nextMsgId >> 8;
|
||||
buffer[1] = nextMsgId - (buffer[0]<<8);
|
||||
length = writeString(topic, buffer,length);
|
||||
buffer[length++] = 0; // Only do QoS 0 subs
|
||||
write(MQTTSUBSCRIBE,buffer,length);
|
||||
}
|
||||
}
|
||||
|
||||
void PubSubClient::disconnect() {
|
||||
_client.write(224);
|
||||
_client.write((uint8_t)0);
|
||||
_client.stop();
|
||||
lastActivity = millis();
|
||||
}
|
||||
|
||||
uint8_t PubSubClient::writeString(char* string, uint8_t* buf, uint8_t pos) {
|
||||
char* idp = string;
|
||||
uint8_t i = 0;
|
||||
pos += 2;
|
||||
while (*idp) {
|
||||
buf[pos++] = *idp++;
|
||||
i++;
|
||||
}
|
||||
buf[pos-i-2] = 0;
|
||||
buf[pos-i-1] = i;
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
int PubSubClient::connected() {
|
||||
int rc = (int)_client.connected();
|
||||
if (!rc) _client.stop();
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,40 +0,0 @@
|
||||
/*
|
||||
PubSubClient.h - A simple client for MQTT.
|
||||
Nicholas O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#ifndef PubSubClient_h
|
||||
#define PubSubClient_h
|
||||
|
||||
#include "Client.h"
|
||||
|
||||
#define MAX_PACKET_SIZE 128
|
||||
#define KEEPALIVE 15000 // max value = 255000
|
||||
|
||||
|
||||
class PubSubClient {
|
||||
private:
|
||||
Client _client;
|
||||
uint8_t buffer[MAX_PACKET_SIZE];
|
||||
uint8_t nextMsgId;
|
||||
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:
|
||||
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,int));
|
||||
int connect(char *);
|
||||
int connect(char*, char*, uint8_t, uint8_t, char*);
|
||||
void disconnect();
|
||||
int publish(char *, char *);
|
||||
int publish(char *, uint8_t *, uint8_t);
|
||||
void subscribe(char *);
|
||||
int loop();
|
||||
int connected();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
47
PubSubClient/CHANGES.txt
Executable file
47
PubSubClient/CHANGES.txt
Executable file
@ -0,0 +1,47 @@
|
||||
1.9
|
||||
* Do not split MQTT packets over multiple calls to _client->write()
|
||||
* API change: All constructors now require an instance of Client
|
||||
to be passed in.
|
||||
* Fixed example to match 1.8 api changes - dpslwk
|
||||
* Added username/password support - WilHall
|
||||
* Added publish_P - publishes messages from PROGMEM - jobytaffey
|
||||
|
||||
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
|
||||
* Added the ability to publish a retained message
|
||||
|
||||
1.5
|
||||
* Added default constructor
|
||||
* Fixed compile error when used with arduino-0021 or later
|
||||
|
||||
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
|
||||
* Clarified licensing - see LICENSE.txt
|
||||
|
||||
|
||||
1.0
|
||||
* Only Quality of Service (QOS) 0 messaging is supported
|
||||
* The maximum message size, including header, is 128 bytes
|
||||
* The keepalive interval is set to 30 seconds
|
||||
* No support for Will messages
|
||||
|
2
LICENSE.txt → PubSubClient/LICENSE.txt
Normal file → Executable file
2
LICENSE.txt → PubSubClient/LICENSE.txt
Normal file → Executable file
@ -1,4 +1,4 @@
|
||||
Copyright (c) 2008-2009 Nicholas O'Leary
|
||||
Copyright (c) 2008-2012 Nicholas O'Leary
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
337
PubSubClient/PubSubClient.cpp
Executable file
337
PubSubClient/PubSubClient.cpp
Executable file
@ -0,0 +1,337 @@
|
||||
/*
|
||||
PubSubClient.cpp - A simple client for MQTT.
|
||||
Nicholas O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#include "PubSubClient.h"
|
||||
#include <string.h>
|
||||
|
||||
PubSubClient::PubSubClient(Client& client) {
|
||||
this->_client = &client;
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) {
|
||||
this->_client = &client;
|
||||
this->callback = callback;
|
||||
this->ip = ip;
|
||||
this->port = port;
|
||||
}
|
||||
|
||||
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) {
|
||||
this->_client = &client;
|
||||
this->callback = callback;
|
||||
this->domain = domain;
|
||||
this->port = port;
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(char *id) {
|
||||
return connect(id,NULL,NULL,0,0,0,0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(char *id, char *user, char *pass) {
|
||||
return connect(id,user,pass,0,0,0,0);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage)
|
||||
{
|
||||
return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage);
|
||||
}
|
||||
|
||||
boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) {
|
||||
if (!connected()) {
|
||||
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;
|
||||
uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p',MQTTPROTOCOLVERSION};
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 5;
|
||||
unsigned int j;
|
||||
for (j = 0;j<9;j++) {
|
||||
buffer[length++] = d[j];
|
||||
}
|
||||
|
||||
uint8_t v;
|
||||
if (willTopic) {
|
||||
v = 0x06|(willQos<<3)|(willRetain<<5);
|
||||
} else {
|
||||
v = 0x02;
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
v = v|0x80;
|
||||
|
||||
if(pass != NULL) {
|
||||
v = v|(0x80>>1);
|
||||
}
|
||||
}
|
||||
|
||||
buffer[length++] = v;
|
||||
|
||||
buffer[length++] = ((MQTT_KEEPALIVE) >> 8);
|
||||
buffer[length++] = ((MQTT_KEEPALIVE) & 0xFF);
|
||||
length = writeString(id,buffer,length);
|
||||
if (willTopic) {
|
||||
length = writeString(willTopic,buffer,length);
|
||||
length = writeString(willMessage,buffer,length);
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
length = writeString(user,buffer,length);
|
||||
if(pass != NULL) {
|
||||
length = writeString(pass,buffer,length);
|
||||
}
|
||||
}
|
||||
|
||||
write(MQTTCONNECT,buffer,length-5);
|
||||
|
||||
lastInActivity = lastOutActivity = millis();
|
||||
|
||||
while (!_client->available()) {
|
||||
unsigned long t = millis();
|
||||
if (t-lastInActivity > MQTT_KEEPALIVE*1000UL) {
|
||||
_client->stop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
uint16_t len = readPacket();
|
||||
|
||||
if (len == 4 && buffer[3] == 0) {
|
||||
lastInActivity = millis();
|
||||
pingOutstanding = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
_client->stop();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t PubSubClient::readByte() {
|
||||
while(!_client->available()) {}
|
||||
return _client->read();
|
||||
}
|
||||
|
||||
uint16_t PubSubClient::readPacket() {
|
||||
uint16_t len = 0;
|
||||
buffer[len++] = readByte();
|
||||
uint8_t multiplier = 1;
|
||||
uint16_t length = 0;
|
||||
uint8_t digit = 0;
|
||||
do {
|
||||
digit = readByte();
|
||||
buffer[len++] = digit;
|
||||
length += (digit & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
} while ((digit & 128) != 0);
|
||||
|
||||
for (uint16_t i = 0;i<length;i++)
|
||||
{
|
||||
if (len < MQTT_MAX_PACKET_SIZE) {
|
||||
buffer[len++] = readByte();
|
||||
} else {
|
||||
readByte();
|
||||
len = 0; // This will cause the packet to be ignored.
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
boolean PubSubClient::loop() {
|
||||
if (connected()) {
|
||||
unsigned long t = millis();
|
||||
if ((t - lastInActivity > MQTT_KEEPALIVE*1000UL) || (t - lastOutActivity > MQTT_KEEPALIVE*1000UL)) {
|
||||
if (pingOutstanding) {
|
||||
_client->stop();
|
||||
return false;
|
||||
} else {
|
||||
buffer[0] = MQTTPINGREQ;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
lastOutActivity = t;
|
||||
lastInActivity = t;
|
||||
pingOutstanding = true;
|
||||
}
|
||||
}
|
||||
if (_client->available()) {
|
||||
uint16_t len = readPacket();
|
||||
if (len > 0) {
|
||||
lastInActivity = t;
|
||||
uint8_t type = buffer[0]&0xF0;
|
||||
if (type == MQTTPUBLISH) {
|
||||
if (callback) {
|
||||
uint16_t tl = (buffer[2]<<8)+buffer[3];
|
||||
char topic[tl+1];
|
||||
for (uint16_t i=0;i<tl;i++) {
|
||||
topic[i] = buffer[4+i];
|
||||
}
|
||||
topic[tl] = 0;
|
||||
// ignore msgID - only support QoS 0 subs
|
||||
uint8_t *payload = buffer+4+tl;
|
||||
callback(topic,payload,len-4-tl);
|
||||
}
|
||||
} else if (type == MQTTPINGREQ) {
|
||||
buffer[0] = MQTTPINGRESP;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
} else if (type == MQTTPINGRESP) {
|
||||
pingOutstanding = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(char* topic, char* payload) {
|
||||
return publish(topic,(uint8_t*)payload,strlen(payload),false);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plength) {
|
||||
return publish(topic, payload, plength, false);
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish(char* topic, uint8_t* payload, unsigned int plength, boolean retained) {
|
||||
if (connected()) {
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 5;
|
||||
length = writeString(topic,buffer,length);
|
||||
uint16_t i;
|
||||
for (i=0;i<plength;i++) {
|
||||
buffer[length++] = payload[i];
|
||||
}
|
||||
uint8_t header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
return write(header,buffer,length-5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean PubSubClient::publish_P(char* topic, uint8_t* PROGMEM payload, unsigned int plength, boolean retained) {
|
||||
uint8_t llen = 0;
|
||||
uint8_t digit;
|
||||
int rc;
|
||||
uint16_t tlen;
|
||||
int pos = 0;
|
||||
int i;
|
||||
uint8_t header;
|
||||
unsigned int len;
|
||||
|
||||
if (!connected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
tlen = strlen(topic);
|
||||
|
||||
header = MQTTPUBLISH;
|
||||
if (retained) {
|
||||
header |= 1;
|
||||
}
|
||||
buffer[pos++] = header;
|
||||
len = plength + 2 + tlen;
|
||||
do {
|
||||
digit = len % 128;
|
||||
len = len / 128;
|
||||
if (len > 0) {
|
||||
digit |= 0x80;
|
||||
}
|
||||
buffer[pos++] = digit;
|
||||
llen++;
|
||||
} while(len>0);
|
||||
|
||||
pos = writeString(topic,buffer,pos);
|
||||
|
||||
rc += _client->write(buffer,pos);
|
||||
|
||||
for (i=0;i<plength;i++) {
|
||||
rc += _client->write((char)pgm_read_byte_near(payload + i));
|
||||
}
|
||||
|
||||
lastOutActivity = millis();
|
||||
return rc == len + 1 + plength;
|
||||
}
|
||||
|
||||
boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
||||
uint8_t lenBuf[4];
|
||||
uint8_t llen = 0;
|
||||
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);
|
||||
|
||||
buf[4-llen] = header;
|
||||
for (int i=0;i<llen;i++) {
|
||||
buf[5-llen+i] = lenBuf[i];
|
||||
}
|
||||
rc = _client->write(buf+(4-llen),length+1+llen);
|
||||
|
||||
lastOutActivity = millis();
|
||||
return (rc == 1+llen+length);
|
||||
}
|
||||
|
||||
|
||||
boolean PubSubClient::subscribe(char* topic) {
|
||||
if (connected()) {
|
||||
// Leave room in the buffer for header and variable length field
|
||||
uint16_t length = 7;
|
||||
nextMsgId++;
|
||||
if (nextMsgId == 0) {
|
||||
nextMsgId = 1;
|
||||
}
|
||||
buffer[0] = (nextMsgId >> 8);
|
||||
buffer[1] = (nextMsgId & 0xFF);
|
||||
length = writeString(topic, buffer,length);
|
||||
buffer[length++] = 0; // Only do QoS 0 subs
|
||||
return write(MQTTSUBSCRIBE|MQTTQOS1,buffer,length-5);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void PubSubClient::disconnect() {
|
||||
buffer[0] = MQTTDISCONNECT;
|
||||
buffer[1] = 0;
|
||||
_client->write(buffer,2);
|
||||
_client->stop();
|
||||
lastInActivity = lastOutActivity = millis();
|
||||
}
|
||||
|
||||
uint16_t PubSubClient::writeString(char* string, uint8_t* buf, uint16_t pos) {
|
||||
char* idp = string;
|
||||
uint16_t i = 0;
|
||||
pos += 2;
|
||||
while (*idp) {
|
||||
buf[pos++] = *idp++;
|
||||
i++;
|
||||
}
|
||||
buf[pos-i-2] = (i >> 8);
|
||||
buf[pos-i-1] = (i & 0xFF);
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
boolean PubSubClient::connected() {
|
||||
int rc = (int)_client->connected();
|
||||
if (!rc) _client->stop();
|
||||
return rc;
|
||||
}
|
||||
|
75
PubSubClient/PubSubClient.h
Executable file
75
PubSubClient/PubSubClient.h
Executable file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
PubSubClient.h - A simple client for MQTT.
|
||||
Nicholas O'Leary
|
||||
http://knolleary.net
|
||||
*/
|
||||
|
||||
#ifndef PubSubClient_h
|
||||
#define PubSubClient_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "Client.h"
|
||||
|
||||
// MQTT_MAX_PACKET_SIZE : Maximum packet size
|
||||
#define MQTT_MAX_PACKET_SIZE 128
|
||||
|
||||
// MQTT_KEEPALIVE : keepAlive interval in Seconds
|
||||
#define MQTT_KEEPALIVE 15
|
||||
|
||||
#define MQTTPROTOCOLVERSION 3
|
||||
#define MQTTCONNECT 1 << 4 // Client request to connect to Server
|
||||
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
|
||||
#define MQTTPUBLISH 3 << 4 // Publish message
|
||||
#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
|
||||
#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
|
||||
#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
|
||||
#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
|
||||
#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
|
||||
#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
|
||||
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
|
||||
#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
|
||||
#define MQTTPINGREQ 12 << 4 // PING Request
|
||||
#define MQTTPINGRESP 13 << 4 // PING Response
|
||||
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
|
||||
#define MQTTReserved 15 << 4 // Reserved
|
||||
|
||||
#define MQTTQOS0 (0 << 1)
|
||||
#define MQTTQOS1 (1 << 1)
|
||||
#define MQTTQOS2 (2 << 1)
|
||||
|
||||
class PubSubClient {
|
||||
private:
|
||||
Client* _client;
|
||||
uint8_t buffer[MQTT_MAX_PACKET_SIZE];
|
||||
uint16_t nextMsgId;
|
||||
unsigned long lastOutActivity;
|
||||
unsigned long lastInActivity;
|
||||
bool pingOutstanding;
|
||||
void (*callback)(char*,uint8_t*,unsigned int);
|
||||
uint16_t readPacket();
|
||||
uint8_t readByte();
|
||||
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
|
||||
uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);
|
||||
uint8_t *ip;
|
||||
char* domain;
|
||||
uint16_t port;
|
||||
public:
|
||||
PubSubClient(Client& client);
|
||||
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
|
||||
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
|
||||
boolean connect(char *);
|
||||
boolean connect(char *, char *, char *);
|
||||
boolean connect(char *, char *, uint8_t, uint8_t, char *);
|
||||
boolean connect(char *, char *, char *, char *, uint8_t, uint8_t, char*);
|
||||
void disconnect();
|
||||
boolean publish(char *, char *);
|
||||
boolean publish(char *, uint8_t *, unsigned int);
|
||||
boolean publish(char *, uint8_t *, unsigned int, boolean);
|
||||
boolean publish_P(char *, uint8_t PROGMEM *, unsigned int, boolean);
|
||||
boolean subscribe(char *);
|
||||
boolean loop();
|
||||
boolean connected();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
39
PubSubClient/examples/mqtt_auth/mqtt_auth.ino
Executable file
39
PubSubClient/examples/mqtt_auth/mqtt_auth.ino
Executable file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
Basic MQTT example with Authentication
|
||||
|
||||
- connects to an MQTT server, providing username
|
||||
and password
|
||||
- publishes "hello world" to the topic "outTopic"
|
||||
- subscribes to the topic "inTopic"
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// Update these with values suitable for your network.
|
||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||
byte server[] = { 172, 16, 0, 2 };
|
||||
byte ip[] = { 172, 16, 0, 100 };
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// handle message arrived
|
||||
}
|
||||
|
||||
EthernetClient ethClient;
|
||||
PubSubClient client(server, 1883, callback, ethClient);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Ethernet.begin(mac, ip);
|
||||
if (client.connect("arduinoClient", "testuser", "testpass")) {
|
||||
client.publish("outTopic","hello world");
|
||||
client.subscribe("inTopic");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
client.loop();
|
||||
}
|
||||
|
38
PubSubClient/examples/mqtt_basic/mqtt_basic.ino
Executable file
38
PubSubClient/examples/mqtt_basic/mqtt_basic.ino
Executable file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
Basic MQTT example
|
||||
|
||||
- connects to an MQTT server
|
||||
- publishes "hello world" to the topic "outTopic"
|
||||
- subscribes to the topic "inTopic"
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// Update these with values suitable for your network.
|
||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||
byte server[] = { 172, 16, 0, 2 };
|
||||
byte ip[] = { 172, 16, 0, 100 };
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// handle message arrived
|
||||
}
|
||||
|
||||
EthernetClient ethClient;
|
||||
PubSubClient client(server, 1883, callback, ethClient);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Ethernet.begin(mac, ip);
|
||||
if (client.connect("arduinoClient")) {
|
||||
client.publish("outTopic","hello world");
|
||||
client.subscribe("inTopic");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
client.loop();
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
Publishing in the callback
|
||||
|
||||
- connects to an MQTT server
|
||||
- subscribes to the topic "inTopic"
|
||||
- when a message is received, republishes it to "outTopic"
|
||||
|
||||
This example shows how to publish messages within the
|
||||
callback function. The callback function header needs to
|
||||
be declared before the PubSubClient constructor and the
|
||||
actual callback defined afterwards.
|
||||
This ensures the client reference in the callback function
|
||||
is valid.
|
||||
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// Update these with values suitable for your network.
|
||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||
byte server[] = { 172, 16, 0, 2 };
|
||||
byte ip[] = { 172, 16, 0, 100 };
|
||||
|
||||
// Callback function header
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
EthernetClient ethClient;
|
||||
PubSubClient client(server, 1883, callback, ethClient);
|
||||
|
||||
// Callback function
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
client.publish("outTopic", payload, length);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Ethernet.begin(mac, ip);
|
||||
if (client.connect("arduinoClient")) {
|
||||
client.publish("outTopic","hello world");
|
||||
client.subscribe("inTopic");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
client.loop();
|
||||
}
|
||||
|
Reference in New Issue
Block a user