Add wider IPAddress support in api
This commit is contained in:
parent
c753ecaebe
commit
12a9d89ea2
@ -5,7 +5,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "PubSubClient.h"
|
#include "PubSubClient.h"
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
PubSubClient::PubSubClient() {
|
PubSubClient::PubSubClient() {
|
||||||
this->_client = NULL;
|
this->_client = NULL;
|
||||||
@ -13,31 +12,73 @@ PubSubClient::PubSubClient() {
|
|||||||
setCallback(NULL);
|
setCallback(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) {
|
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client) {
|
||||||
|
setServer(addr, port);
|
||||||
setClient(client);
|
setClient(client);
|
||||||
setCallback(callback);
|
|
||||||
setServer(ip, port);
|
|
||||||
this->stream = NULL;
|
this->stream = NULL;
|
||||||
}
|
}
|
||||||
|
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream) {
|
||||||
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client) {
|
setServer(addr,port);
|
||||||
setClient(client);
|
setClient(client);
|
||||||
|
setStream(stream);
|
||||||
|
}
|
||||||
|
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||||
|
setServer(addr, port);
|
||||||
setCallback(callback);
|
setCallback(callback);
|
||||||
setServer(domain,port);
|
setClient(client);
|
||||||
this->stream = NULL;
|
this->stream = NULL;
|
||||||
}
|
}
|
||||||
|
PubSubClient::PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||||
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client, Stream& stream) {
|
setServer(addr,port);
|
||||||
setClient(client);
|
|
||||||
setCallback(callback);
|
setCallback(callback);
|
||||||
setServer(ip,port);
|
setClient(client);
|
||||||
setStream(stream);
|
setStream(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int), Client& client, Stream& stream) {
|
|
||||||
|
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client) {
|
||||||
|
setServer(ip, port);
|
||||||
setClient(client);
|
setClient(client);
|
||||||
|
this->stream = NULL;
|
||||||
|
}
|
||||||
|
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, Client& client, Stream& stream) {
|
||||||
|
setServer(ip,port);
|
||||||
|
setClient(client);
|
||||||
|
setStream(stream);
|
||||||
|
}
|
||||||
|
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||||
|
setServer(ip, port);
|
||||||
setCallback(callback);
|
setCallback(callback);
|
||||||
|
setClient(client);
|
||||||
|
this->stream = NULL;
|
||||||
|
}
|
||||||
|
PubSubClient::PubSubClient(uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||||
|
setServer(ip,port);
|
||||||
|
setCallback(callback);
|
||||||
|
setClient(client);
|
||||||
|
setStream(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
PubSubClient::PubSubClient(char* domain, uint16_t port, Client& client) {
|
||||||
setServer(domain,port);
|
setServer(domain,port);
|
||||||
|
setClient(client);
|
||||||
|
this->stream = NULL;
|
||||||
|
}
|
||||||
|
PubSubClient::PubSubClient(char* domain, uint16_t port, Client& client, Stream& stream) {
|
||||||
|
setServer(domain,port);
|
||||||
|
setClient(client);
|
||||||
|
setStream(stream);
|
||||||
|
}
|
||||||
|
PubSubClient::PubSubClient(char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client) {
|
||||||
|
setServer(domain,port);
|
||||||
|
setCallback(callback);
|
||||||
|
setClient(client);
|
||||||
|
this->stream = NULL;
|
||||||
|
}
|
||||||
|
PubSubClient::PubSubClient(char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream) {
|
||||||
|
setServer(domain,port);
|
||||||
|
setCallback(callback);
|
||||||
|
setClient(client);
|
||||||
setStream(stream);
|
setStream(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,8 +90,7 @@ boolean PubSubClient::connect(char *id, char *user, char *pass) {
|
|||||||
return connect(id,user,pass,0,0,0,0);
|
return connect(id,user,pass,0,0,0,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean 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) {
|
||||||
{
|
|
||||||
return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage);
|
return connect(id,NULL,NULL,willTopic,willQos,willRetain,willMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +103,6 @@ boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic,
|
|||||||
} else {
|
} else {
|
||||||
result = _client->connect(this->ip, this->port);
|
result = _client->connect(this->ip, this->port);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
nextMsgId = 1;
|
nextMsgId = 1;
|
||||||
// Leave room in the buffer for header and variable length field
|
// Leave room in the buffer for header and variable length field
|
||||||
@ -429,6 +468,11 @@ boolean PubSubClient::connected() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PubSubClient::setServer(uint8_t * ip, uint16_t port) {
|
void PubSubClient::setServer(uint8_t * ip, uint16_t port) {
|
||||||
|
IPAddress addr(ip[0],ip[1],ip[2],ip[3]);
|
||||||
|
setServer(addr,port);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PubSubClient::setServer(IPAddress ip, uint16_t port) {
|
||||||
this->ip = ip;
|
this->ip = ip;
|
||||||
this->port = port;
|
this->port = port;
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#define PubSubClient_h
|
#define PubSubClient_h
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include "IPAddress.h"
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
#include "Stream.h"
|
#include "Stream.h"
|
||||||
|
|
||||||
@ -43,6 +44,8 @@
|
|||||||
#define MQTTQOS1 (1 << 1)
|
#define MQTTQOS1 (1 << 1)
|
||||||
#define MQTTQOS2 (2 << 1)
|
#define MQTTQOS2 (2 << 1)
|
||||||
|
|
||||||
|
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*,uint8_t*,unsigned int)
|
||||||
|
|
||||||
class PubSubClient {
|
class PubSubClient {
|
||||||
private:
|
private:
|
||||||
Client* _client;
|
Client* _client;
|
||||||
@ -51,25 +54,34 @@ private:
|
|||||||
unsigned long lastOutActivity;
|
unsigned long lastOutActivity;
|
||||||
unsigned long lastInActivity;
|
unsigned long lastInActivity;
|
||||||
bool pingOutstanding;
|
bool pingOutstanding;
|
||||||
void (*callback)(char*,uint8_t*,unsigned int);
|
MQTT_CALLBACK_SIGNATURE;
|
||||||
uint16_t readPacket(uint8_t*);
|
uint16_t readPacket(uint8_t*);
|
||||||
uint8_t readByte();
|
uint8_t readByte();
|
||||||
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
|
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
|
||||||
uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);
|
uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);
|
||||||
uint8_t *ip;
|
IPAddress ip;
|
||||||
char* domain;
|
char* domain;
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
Stream* stream;
|
Stream* stream;
|
||||||
public:
|
public:
|
||||||
PubSubClient();
|
PubSubClient();
|
||||||
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
|
PubSubClient(IPAddress, uint16_t, Client& client);
|
||||||
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&);
|
PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
|
||||||
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
|
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||||
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&);
|
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||||
|
PubSubClient(uint8_t *, uint16_t, Client& client);
|
||||||
|
PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
|
||||||
|
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||||
|
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||||
|
PubSubClient(char*, uint16_t, Client& client);
|
||||||
|
PubSubClient(char*, uint16_t, Client& client, Stream&);
|
||||||
|
PubSubClient(char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
|
||||||
|
PubSubClient(char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
|
||||||
|
|
||||||
|
void setServer(IPAddress ip, uint16_t port);
|
||||||
void setServer(uint8_t * ip, uint16_t port);
|
void setServer(uint8_t * ip, uint16_t port);
|
||||||
void setServer(char * domain, uint16_t port);
|
void setServer(char * domain, uint16_t port);
|
||||||
void setCallback(void(*callback)(char*,uint8_t*,unsigned int));
|
void setCallback(MQTT_CALLBACK_SIGNATURE);
|
||||||
void setClient(Client& client);
|
void setClient(Client& client);
|
||||||
void setStream(Stream& stream);
|
void setStream(Stream& stream);
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Basic MQTT example with Authentication
|
Basic MQTT example with Authentication
|
||||||
|
|
||||||
- connects to an MQTT server, providing username
|
- connects to an MQTT server, providing username
|
||||||
and password
|
and password
|
||||||
- publishes "hello world" to the topic "outTopic"
|
- publishes "hello world" to the topic "outTopic"
|
||||||
@ -13,8 +13,8 @@
|
|||||||
|
|
||||||
// Update these with values suitable for your network.
|
// Update these with values suitable for your network.
|
||||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||||
byte server[] = { 172, 16, 0, 2 };
|
IPAddress ip(172, 16, 0, 100);
|
||||||
byte ip[] = { 172, 16, 0, 100 };
|
IPAddress server(172, 16, 0, 2);
|
||||||
|
|
||||||
void callback(char* topic, byte* payload, unsigned int length) {
|
void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
// handle message arrived
|
// handle message arrived
|
||||||
@ -36,4 +36,3 @@ void loop()
|
|||||||
{
|
{
|
||||||
client.loop();
|
client.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Basic MQTT example
|
Basic MQTT example
|
||||||
|
|
||||||
- connects to an MQTT server
|
- connects to an MQTT server
|
||||||
- publishes "hello world" to the topic "outTopic"
|
- publishes "hello world" to the topic "outTopic"
|
||||||
- subscribes to the topic "inTopic"
|
- subscribes to the topic "inTopic"
|
||||||
@ -12,8 +12,8 @@
|
|||||||
|
|
||||||
// Update these with values suitable for your network.
|
// Update these with values suitable for your network.
|
||||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||||
byte server[] = { 172, 16, 0, 2 };
|
IPAddress ip(172, 16, 0, 100);
|
||||||
byte ip[] = { 172, 16, 0, 100 };
|
IPAddress server(172, 16, 0, 2);
|
||||||
|
|
||||||
void callback(char* topic, byte* payload, unsigned int length) {
|
void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
// handle message arrived
|
// handle message arrived
|
||||||
@ -35,4 +35,3 @@ void loop()
|
|||||||
{
|
{
|
||||||
client.loop();
|
client.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
Publishing in the callback
|
Publishing in the callback
|
||||||
|
|
||||||
- connects to an MQTT server
|
- connects to an MQTT server
|
||||||
- subscribes to the topic "inTopic"
|
- subscribes to the topic "inTopic"
|
||||||
- when a message is received, republishes it to "outTopic"
|
- when a message is received, republishes it to "outTopic"
|
||||||
|
|
||||||
This example shows how to publish messages within the
|
This example shows how to publish messages within the
|
||||||
callback function. The callback function header needs to
|
callback function. The callback function header needs to
|
||||||
be declared before the PubSubClient constructor and the
|
be declared before the PubSubClient constructor and the
|
||||||
actual callback defined afterwards.
|
actual callback defined afterwards.
|
||||||
This ensures the client reference in the callback function
|
This ensures the client reference in the callback function
|
||||||
is valid.
|
is valid.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
@ -20,8 +20,8 @@
|
|||||||
|
|
||||||
// Update these with values suitable for your network.
|
// Update these with values suitable for your network.
|
||||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||||
byte server[] = { 172, 16, 0, 2 };
|
IPAddress ip(172, 16, 0, 100);
|
||||||
byte ip[] = { 172, 16, 0, 100 };
|
IPAddress server(172, 16, 0, 2);
|
||||||
|
|
||||||
// Callback function header
|
// Callback function header
|
||||||
void callback(char* topic, byte* payload, unsigned int length);
|
void callback(char* topic, byte* payload, unsigned int length);
|
||||||
@ -34,7 +34,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
// In order to republish this payload, a copy must be made
|
// In order to republish this payload, a copy must be made
|
||||||
// as the orignal payload buffer will be overwritten whilst
|
// as the orignal payload buffer will be overwritten whilst
|
||||||
// constructing the PUBLISH packet.
|
// constructing the PUBLISH packet.
|
||||||
|
|
||||||
// Allocate the correct amount of memory for the payload copy
|
// Allocate the correct amount of memory for the payload copy
|
||||||
byte* p = (byte*)malloc(length);
|
byte* p = (byte*)malloc(length);
|
||||||
// Copy the payload to the new buffer
|
// Copy the payload to the new buffer
|
||||||
@ -46,7 +46,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
|
||||||
Ethernet.begin(mac, ip);
|
Ethernet.begin(mac, ip);
|
||||||
if (client.connect("arduinoClient")) {
|
if (client.connect("arduinoClient")) {
|
||||||
client.publish("outTopic","hello world");
|
client.publish("outTopic","hello world");
|
||||||
@ -58,4 +58,3 @@ void loop()
|
|||||||
{
|
{
|
||||||
client.loop();
|
client.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
// Update these with values suitable for your network.
|
// Update these with values suitable for your network.
|
||||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||||
byte server[] = { 172, 16, 0, 2 };
|
IPAddress ip(172, 16, 0, 100);
|
||||||
byte ip[] = { 172, 16, 0, 100 };
|
IPAddress server(172, 16, 0, 2);
|
||||||
|
|
||||||
SRAM sram(4, SRAM_1024);
|
SRAM sram(4, SRAM_1024);
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
Serial.write(sram.read());
|
Serial.write(sram.read());
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
// Reset position for the next message to be stored
|
// Reset position for the next message to be stored
|
||||||
sram.seek(1);
|
sram.seek(1);
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ void setup()
|
|||||||
|
|
||||||
sram.begin();
|
sram.begin();
|
||||||
sram.seek(1);
|
sram.seek(1);
|
||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,4 +55,3 @@ void loop()
|
|||||||
{
|
{
|
||||||
client.loop();
|
client.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user