Add single setServer func in place of port/ip/domain
This commit is contained in:
parent
5418f32458
commit
3852808aac
@ -9,43 +9,35 @@
|
|||||||
|
|
||||||
PubSubClient::PubSubClient() {
|
PubSubClient::PubSubClient() {
|
||||||
this->_client = NULL;
|
this->_client = NULL;
|
||||||
this->stream = NULL;
|
this->stream = NULL;
|
||||||
setCallback(NULL);
|
setCallback(NULL);
|
||||||
setPort(1883);
|
|
||||||
setBrokerDomain(NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) {
|
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) {
|
||||||
setClient(client);
|
setClient(client);
|
||||||
setCallback(callback);
|
setCallback(callback);
|
||||||
setBrokerIP(ip);
|
setServer(ip, port);
|
||||||
setPort(port);
|
|
||||||
setBrokerDomain(NULL);
|
|
||||||
this->stream = NULL;
|
this->stream = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) {
|
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) {
|
||||||
setClient(client);
|
setClient(client);
|
||||||
setCallback(callback);
|
setCallback(callback);
|
||||||
setBrokerDomain(domain);
|
setServer(domain,port);
|
||||||
setPort(port);
|
|
||||||
this->stream = NULL;
|
this->stream = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client, Stream& stream) {
|
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client, Stream& stream) {
|
||||||
setClient(client);
|
setClient(client);
|
||||||
setCallback(callback);
|
setCallback(callback);
|
||||||
setBrokerIP(ip);
|
setServer(ip,port);
|
||||||
setPort(port);
|
|
||||||
setBrokerDomain(NULL);
|
|
||||||
setStream(stream);
|
setStream(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client, Stream& stream) {
|
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client, Stream& stream) {
|
||||||
setClient(client);
|
setClient(client);
|
||||||
setCallback(callback);
|
setCallback(callback);
|
||||||
setBrokerDomain(domain);
|
setServer(domain,port);
|
||||||
setPort(port);
|
|
||||||
setStream(stream);
|
setStream(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,13 +57,13 @@ boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_
|
|||||||
boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) {
|
boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) {
|
||||||
if (!connected()) {
|
if (!connected()) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
if (domain != NULL) {
|
if (domain != NULL) {
|
||||||
result = _client->connect(this->domain, this->port);
|
result = _client->connect(this->domain, this->port);
|
||||||
} else {
|
} else {
|
||||||
result = _client->connect(this->ip, this->port);
|
result = _client->connect(this->ip, this->port);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result) {
|
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};
|
||||||
@ -113,11 +105,11 @@ boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic,
|
|||||||
length = writeString(pass,buffer,length);
|
length = writeString(pass,buffer,length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
write(MQTTCONNECT,buffer,length-5);
|
write(MQTTCONNECT,buffer,length-5);
|
||||||
|
|
||||||
lastInActivity = lastOutActivity = millis();
|
lastInActivity = lastOutActivity = millis();
|
||||||
|
|
||||||
while (!_client->available()) {
|
while (!_client->available()) {
|
||||||
unsigned long t = millis();
|
unsigned long t = millis();
|
||||||
if (t-lastInActivity > MQTT_KEEPALIVE*1000UL) {
|
if (t-lastInActivity > MQTT_KEEPALIVE*1000UL) {
|
||||||
@ -127,7 +119,7 @@ boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic,
|
|||||||
}
|
}
|
||||||
uint8_t llen;
|
uint8_t llen;
|
||||||
uint16_t len = readPacket(&llen);
|
uint16_t len = readPacket(&llen);
|
||||||
|
|
||||||
if (len == 4 && buffer[3] == 0) {
|
if (len == 4 && buffer[3] == 0) {
|
||||||
lastInActivity = millis();
|
lastInActivity = millis();
|
||||||
pingOutstanding = false;
|
pingOutstanding = false;
|
||||||
@ -153,7 +145,7 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
|||||||
uint8_t digit = 0;
|
uint8_t digit = 0;
|
||||||
uint16_t skip = 0;
|
uint16_t skip = 0;
|
||||||
uint8_t start = 0;
|
uint8_t start = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
digit = readByte();
|
digit = readByte();
|
||||||
buffer[len++] = digit;
|
buffer[len++] = digit;
|
||||||
@ -186,7 +178,7 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
|||||||
}
|
}
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->stream && len > MQTT_MAX_PACKET_SIZE) {
|
if (!this->stream && len > MQTT_MAX_PACKET_SIZE) {
|
||||||
len = 0; // This will cause the packet to be ignored.
|
len = 0; // This will cause the packet to be ignored.
|
||||||
}
|
}
|
||||||
@ -231,7 +223,7 @@ boolean PubSubClient::loop() {
|
|||||||
msgId = (buffer[llen+3+tl]<<8)+buffer[llen+3+tl+1];
|
msgId = (buffer[llen+3+tl]<<8)+buffer[llen+3+tl+1];
|
||||||
payload = buffer+llen+3+tl+2;
|
payload = buffer+llen+3+tl+2;
|
||||||
callback(topic,payload,len-llen-3-tl-2);
|
callback(topic,payload,len-llen-3-tl-2);
|
||||||
|
|
||||||
buffer[0] = MQTTPUBACK;
|
buffer[0] = MQTTPUBACK;
|
||||||
buffer[1] = 2;
|
buffer[1] = 2;
|
||||||
buffer[2] = (msgId >> 8);
|
buffer[2] = (msgId >> 8);
|
||||||
@ -293,13 +285,13 @@ boolean PubSubClient::publish_P(char* topic, uint8_t* PROGMEM payload, unsigned
|
|||||||
unsigned int i;
|
unsigned int i;
|
||||||
uint8_t header;
|
uint8_t header;
|
||||||
unsigned int len;
|
unsigned int len;
|
||||||
|
|
||||||
if (!connected()) {
|
if (!connected()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
tlen = strlen(topic);
|
tlen = strlen(topic);
|
||||||
|
|
||||||
header = MQTTPUBLISH;
|
header = MQTTPUBLISH;
|
||||||
if (retained) {
|
if (retained) {
|
||||||
header |= 1;
|
header |= 1;
|
||||||
@ -351,7 +343,7 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
|||||||
buf[5-llen+i] = lenBuf[i];
|
buf[5-llen+i] = lenBuf[i];
|
||||||
}
|
}
|
||||||
rc = _client->write(buf+(4-llen),length+1+llen);
|
rc = _client->write(buf+(4-llen),length+1+llen);
|
||||||
|
|
||||||
lastOutActivity = millis();
|
lastOutActivity = millis();
|
||||||
return (rc == 1+llen+length);
|
return (rc == 1+llen+length);
|
||||||
}
|
}
|
||||||
@ -428,23 +420,20 @@ boolean PubSubClient::connected() {
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PubSubClient::setBrokerIP(uint8_t * ip){
|
void PubSubClient::setServer(uint8_t * ip, uint16_t port) {
|
||||||
this->domain = NULL;
|
this->ip = ip;
|
||||||
this->ip = ip;
|
this->port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PubSubClient::setBrokerDomain(char * domain){
|
void PubSubClient::setServer(char * domain, uint16_t port) {
|
||||||
this->domain = domain;
|
this->domain = domain;
|
||||||
|
this->port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PubSubClient::setCallback(void(*callback)(char*,uint8_t*,unsigned int)){
|
void PubSubClient::setCallback(void(*callback)(char*,uint8_t*,unsigned int)){
|
||||||
this->callback = callback;
|
this->callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PubSubClient::setPort(uint16_t port){
|
|
||||||
this->port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PubSubClient::setClient(Client& client){
|
void PubSubClient::setClient(Client& client){
|
||||||
this->_client = &client;
|
this->_client = &client;
|
||||||
}
|
}
|
||||||
|
@ -61,14 +61,13 @@ public:
|
|||||||
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&);
|
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&);
|
||||||
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
|
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
|
||||||
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&);
|
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&);
|
||||||
|
|
||||||
void setBrokerIP(uint8_t * ip);
|
void setServer(uint8_t * ip, uint16_t port);
|
||||||
void setBrokerDomain(char * domain);
|
void setServer(char * domain, uint16_t port);
|
||||||
void setCallback(void(*callback)(char*,uint8_t*,unsigned int));
|
void setCallback(void(*callback)(char*,uint8_t*,unsigned int));
|
||||||
void setPort(uint16_t port);
|
|
||||||
void setClient(Client& client);
|
void setClient(Client& client);
|
||||||
void setStream(Stream& stream);
|
void setStream(Stream& stream);
|
||||||
|
|
||||||
boolean connect(char *);
|
boolean connect(char *);
|
||||||
boolean connect(char *, char *, char *);
|
boolean connect(char *, char *, char *);
|
||||||
boolean connect(char *, char *, uint8_t, uint8_t, char *);
|
boolean connect(char *, char *, uint8_t, uint8_t, char *);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user