Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
341661671b | |||
4739ca0802 | |||
36bb1ffa6a | |||
83b69a766e | |||
baeb59e263 | |||
67eba6dad4 | |||
98a9c296f6 | |||
68400b7b6c | |||
21b75a2c4a | |||
830f34c7d0 | |||
0bb4efcea5 | |||
31521085ea | |||
8a1d7fb620 | |||
803f54b0bd | |||
6f97ea04f2 | |||
6bb06187b7 | |||
efebd2e5e4 | |||
5cdadf43da | |||
2f97e4a558 | |||
15a0e41c81 | |||
aa9afc7b44 | |||
461cbdb6e8 | |||
47a37a4663 | |||
efcf6dbf1e | |||
d5c13d578e | |||
b6f2cb29bc | |||
c1d327cac6 | |||
5ace47bc93 |
@ -1,3 +1,14 @@
|
|||||||
|
2.4
|
||||||
|
* Add MQTT_SOCKET_TIMEOUT to prevent it blocking indefinitely
|
||||||
|
whilst waiting for inbound data
|
||||||
|
* Fixed return code when publishing >256 bytes
|
||||||
|
|
||||||
|
2.3
|
||||||
|
* Add publish(topic,payload,retained) function
|
||||||
|
|
||||||
|
2.2
|
||||||
|
* Change code layout to match Arduino Library reqs
|
||||||
|
|
||||||
2.1
|
2.1
|
||||||
* Add MAX_TRANSFER_SIZE def to chunk messages if needed
|
* Add MAX_TRANSFER_SIZE def to chunk messages if needed
|
||||||
* Reject topic/payloads that exceed MQTT_MAX_PACKET_SIZE
|
* Reject topic/payloads that exceed MQTT_MAX_PACKET_SIZE
|
@ -8,7 +8,7 @@ a server that supports MQTT.
|
|||||||
The library comes with a number of example sketches. See File > Examples > PubSubClient
|
The library comes with a number of example sketches. See File > Examples > PubSubClient
|
||||||
within the Arduino application.
|
within the Arduino application.
|
||||||
|
|
||||||
Full API documentation is available here: http://knolleary.github.io/pubsubclient/
|
Full API documentation is available here: http://pubsubclient.knolleary.net
|
||||||
|
|
||||||
## Limitations
|
## Limitations
|
||||||
|
|
||||||
@ -33,7 +33,8 @@ boards and shields, including:
|
|||||||
be sure to do a `Bridge.begin()` first
|
be sure to do a `Bridge.begin()` first
|
||||||
- Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield,
|
- Arduino WiFi Shield - if you want to send packets > 90 bytes with this shield,
|
||||||
enable the `MQTT_MAX_TRANSFER_SIZE` define in `PubSubClient.h`.
|
enable the `MQTT_MAX_TRANSFER_SIZE` define in `PubSubClient.h`.
|
||||||
- Sparkfun WiFly Shield – when used with [this library](https://github.com/dpslwk/WiFly)
|
- Sparkfun WiFly Shield – [library](https://github.com/dpslwk/WiFly)
|
||||||
|
- TI CC3000 WiFi - [library](https://github.com/sparkfun/SFE_CC3000_Library)
|
||||||
- Intel Galileo/Edison
|
- Intel Galileo/Edison
|
||||||
- ESP8266
|
- ESP8266
|
||||||
|
|
||||||
|
126
examples/mqtt_esp8266/mqtt_esp8266.ino
Normal file
126
examples/mqtt_esp8266/mqtt_esp8266.ino
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
Basic ESP8266 MQTT example
|
||||||
|
|
||||||
|
This sketch demonstrates the capabilities of the pubsub library in combination
|
||||||
|
with the ESP8266 board/library.
|
||||||
|
|
||||||
|
It connects to an MQTT server then:
|
||||||
|
- publishes "hello world" to the topic "outTopic" every two seconds
|
||||||
|
- subscribes to the topic "inTopic", printing out any messages
|
||||||
|
it receives. NB - it assumes the received payloads are strings not binary
|
||||||
|
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
|
||||||
|
else switch it off
|
||||||
|
|
||||||
|
It will reconnect to the server if the connection is lost using a blocking
|
||||||
|
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
|
||||||
|
achieve the same result without blocking the main loop.
|
||||||
|
|
||||||
|
To install the ESP8266 board, (using Arduino 1.6.4+):
|
||||||
|
- Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
|
||||||
|
http://arduino.esp8266.com/stable/package_esp8266com_index.json
|
||||||
|
- Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
|
||||||
|
- Select your ESP8266 in "Tools -> Board"
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
// Update these with values suitable for your network.
|
||||||
|
|
||||||
|
const char* ssid = "........";
|
||||||
|
const char* password = "........";
|
||||||
|
const char* mqtt_server = "broker.mqtt-dashboard.com";
|
||||||
|
|
||||||
|
WiFiClient espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
long lastMsg = 0;
|
||||||
|
char msg[50];
|
||||||
|
int value = 0;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
||||||
|
Serial.begin(115200);
|
||||||
|
setup_wifi();
|
||||||
|
client.setServer(mqtt_server, 1883);
|
||||||
|
client.setCallback(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_wifi() {
|
||||||
|
|
||||||
|
delay(10);
|
||||||
|
// We start by connecting to a WiFi network
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("");
|
||||||
|
Serial.println("WiFi connected");
|
||||||
|
Serial.println("IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
|
Serial.print("Message arrived [");
|
||||||
|
Serial.print(topic);
|
||||||
|
Serial.print("] ");
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
Serial.print((char)payload[i]);
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
// Switch on the LED if an 1 was received as first character
|
||||||
|
if ((char)payload[0] == '1') {
|
||||||
|
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
|
||||||
|
// but actually the LED is on; this is because
|
||||||
|
// it is acive low on the ESP-01)
|
||||||
|
} else {
|
||||||
|
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void reconnect() {
|
||||||
|
// Loop until we're reconnected
|
||||||
|
while (!client.connected()) {
|
||||||
|
Serial.print("Attempting MQTT connection...");
|
||||||
|
// Attempt to connect
|
||||||
|
if (client.connect("ESP8266Client")) {
|
||||||
|
Serial.println("connected");
|
||||||
|
// Once connected, publish an announcement...
|
||||||
|
client.publish("outTopic", "hello world");
|
||||||
|
// ... and resubscribe
|
||||||
|
client.subscribe("inTopic");
|
||||||
|
} else {
|
||||||
|
Serial.print("failed, rc=");
|
||||||
|
Serial.print(client.state());
|
||||||
|
Serial.println(" try again in 5 seconds");
|
||||||
|
// Wait 5 seconds before retrying
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
if (!client.connected()) {
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
client.loop();
|
||||||
|
|
||||||
|
long now = millis();
|
||||||
|
if (now - lastMsg > 2000) {
|
||||||
|
lastMsg = now;
|
||||||
|
++value;
|
||||||
|
snprintf (msg, 75, "hello world #%ld", value);
|
||||||
|
Serial.print("Publish message: ");
|
||||||
|
Serial.println(msg);
|
||||||
|
client.publish("outTopic", msg);
|
||||||
|
}
|
||||||
|
}
|
17
library.json
Normal file
17
library.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "PubSubClient",
|
||||||
|
"keywords": "ethernet, mqtt, m2m, iot",
|
||||||
|
"description": "A client library for MQTT messaging. MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/knolleary/pubsubclient.git"
|
||||||
|
},
|
||||||
|
"version": "2.6",
|
||||||
|
"exclude": "tests",
|
||||||
|
"examples": "examples/*/*.ino",
|
||||||
|
"frameworks": "arduino",
|
||||||
|
"platforms": [
|
||||||
|
"atmelavr",
|
||||||
|
"espressif"
|
||||||
|
]
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
name=PubSubClient
|
name=PubSubClient
|
||||||
version=2.1
|
version=2.6
|
||||||
author=Nick O'Leary <nick.oleary@gmail.com>
|
author=Nick O'Leary <nick.oleary@gmail.com>
|
||||||
maintainer=Nick O'Leary <nick.oleary@gmail.com>
|
maintainer=Nick O'Leary <nick.oleary@gmail.com>
|
||||||
sentence=A client library for MQTT messaging.
|
sentence=A client library for MQTT messaging.
|
||||||
paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages from a remote server. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison and ESP8266.
|
paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.
|
||||||
category=Communication
|
category=Communication
|
||||||
url=http://knolleary.github.io/pubsubclient/
|
url=http://pubsubclient.knolleary.net
|
||||||
architectures=*
|
architectures=*
|
@ -122,7 +122,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
|
|||||||
} else {
|
} else {
|
||||||
result = _client->connect(this->ip, this->port);
|
result = _client->connect(this->ip, this->port);
|
||||||
}
|
}
|
||||||
if (result) {
|
if (result == 1) {
|
||||||
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
|
||||||
uint16_t length = 5;
|
uint16_t length = 5;
|
||||||
@ -177,7 +177,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
|
|||||||
|
|
||||||
while (!_client->available()) {
|
while (!_client->available()) {
|
||||||
unsigned long t = millis();
|
unsigned long t = millis();
|
||||||
if (t-lastInActivity > MQTT_KEEPALIVE*1000UL) {
|
if (t-lastInActivity >= ((int32_t) MQTT_SOCKET_TIMEOUT*1000UL)) {
|
||||||
_state = MQTT_CONNECTION_TIMEOUT;
|
_state = MQTT_CONNECTION_TIMEOUT;
|
||||||
_client->stop();
|
_client->stop();
|
||||||
return false;
|
return false;
|
||||||
@ -205,14 +205,33 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t PubSubClient::readByte() {
|
// reads a byte into result
|
||||||
while(!_client->available()) {}
|
boolean PubSubClient::readByte(uint8_t * result) {
|
||||||
return _client->read();
|
uint32_t previousMillis = millis();
|
||||||
|
while(!_client->available()) {
|
||||||
|
uint32_t currentMillis = millis();
|
||||||
|
if(currentMillis - previousMillis >= ((int32_t) MQTT_SOCKET_TIMEOUT * 1000)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*result = _client->read();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// reads a byte into result[*index] and increments index
|
||||||
|
boolean PubSubClient::readByte(uint8_t * result, uint16_t * index){
|
||||||
|
uint16_t current_index = *index;
|
||||||
|
uint8_t * write_address = &(result[current_index]);
|
||||||
|
if(readByte(write_address)){
|
||||||
|
*index = current_index + 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
||||||
uint16_t len = 0;
|
uint16_t len = 0;
|
||||||
buffer[len++] = readByte();
|
if(!readByte(buffer, &len)) return 0;
|
||||||
bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH;
|
bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH;
|
||||||
uint32_t multiplier = 1;
|
uint32_t multiplier = 1;
|
||||||
uint16_t length = 0;
|
uint16_t length = 0;
|
||||||
@ -221,7 +240,7 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
|||||||
uint8_t start = 0;
|
uint8_t start = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
digit = readByte();
|
if(!readByte(&digit)) return 0;
|
||||||
buffer[len++] = digit;
|
buffer[len++] = digit;
|
||||||
length += (digit & 127) * multiplier;
|
length += (digit & 127) * multiplier;
|
||||||
multiplier *= 128;
|
multiplier *= 128;
|
||||||
@ -230,8 +249,8 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
|||||||
|
|
||||||
if (isPublish) {
|
if (isPublish) {
|
||||||
// Read in topic length to calculate bytes to skip over for Stream writing
|
// Read in topic length to calculate bytes to skip over for Stream writing
|
||||||
buffer[len++] = readByte();
|
if(!readByte(buffer, &len)) return 0;
|
||||||
buffer[len++] = readByte();
|
if(!readByte(buffer, &len)) return 0;
|
||||||
skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2];
|
skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2];
|
||||||
start = 2;
|
start = 2;
|
||||||
if (buffer[0]&MQTTQOS1) {
|
if (buffer[0]&MQTTQOS1) {
|
||||||
@ -241,7 +260,7 @@ uint16_t PubSubClient::readPacket(uint8_t* lengthLength) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (uint16_t i = start;i<length;i++) {
|
for (uint16_t i = start;i<length;i++) {
|
||||||
digit = readByte();
|
if(!readByte(&digit)) return 0;
|
||||||
if (this->stream) {
|
if (this->stream) {
|
||||||
if (isPublish && len-*lengthLength-2>skip) {
|
if (isPublish && len-*lengthLength-2>skip) {
|
||||||
this->stream->write(digit);
|
this->stream->write(digit);
|
||||||
@ -329,6 +348,10 @@ boolean PubSubClient::publish(const char* topic, const char* payload) {
|
|||||||
return publish(topic,(const uint8_t*)payload,strlen(payload),false);
|
return publish(topic,(const uint8_t*)payload,strlen(payload),false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean PubSubClient::publish(const char* topic, const char* payload, boolean retained) {
|
||||||
|
return publish(topic,(const uint8_t*)payload,strlen(payload),retained);
|
||||||
|
}
|
||||||
|
|
||||||
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
|
boolean PubSubClient::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
|
||||||
return publish(topic, payload, plength, false);
|
return publish(topic, payload, plength, false);
|
||||||
}
|
}
|
||||||
@ -405,7 +428,7 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
|||||||
uint8_t llen = 0;
|
uint8_t llen = 0;
|
||||||
uint8_t digit;
|
uint8_t digit;
|
||||||
uint8_t pos = 0;
|
uint8_t pos = 0;
|
||||||
uint8_t rc;
|
uint16_t rc;
|
||||||
uint16_t len = length;
|
uint16_t len = length;
|
||||||
do {
|
do {
|
||||||
digit = len % 128;
|
digit = len % 128;
|
||||||
@ -424,7 +447,7 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
|||||||
|
|
||||||
#ifdef MQTT_MAX_TRANSFER_SIZE
|
#ifdef MQTT_MAX_TRANSFER_SIZE
|
||||||
uint8_t* writeBuf = buf+(4-llen);
|
uint8_t* writeBuf = buf+(4-llen);
|
||||||
uint8_t bytesRemaining = length+1+llen;
|
uint16_t bytesRemaining = length+1+llen; //Match the length type
|
||||||
uint8_t bytesToWrite;
|
uint8_t bytesToWrite;
|
||||||
boolean result = true;
|
boolean result = true;
|
||||||
while((bytesRemaining > 0) && result) {
|
while((bytesRemaining > 0) && result) {
|
||||||
@ -547,7 +570,7 @@ PubSubClient& PubSubClient::setServer(const char * domain, uint16_t port) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
PubSubClient& PubSubClient::setCallback(void(*callback)(char*,uint8_t*,unsigned int)){
|
PubSubClient& PubSubClient::setCallback(MQTT_CALLBACK_SIGNATURE) {
|
||||||
this->callback = callback;
|
this->callback = callback;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
@ -17,13 +17,24 @@
|
|||||||
|
|
||||||
// MQTT_VERSION : Pick the version
|
// MQTT_VERSION : Pick the version
|
||||||
//#define MQTT_VERSION MQTT_VERSION_3_1
|
//#define MQTT_VERSION MQTT_VERSION_3_1
|
||||||
|
#ifndef MQTT_VERSION
|
||||||
#define MQTT_VERSION MQTT_VERSION_3_1_1
|
#define MQTT_VERSION MQTT_VERSION_3_1_1
|
||||||
|
#endif
|
||||||
|
|
||||||
// MQTT_MAX_PACKET_SIZE : Maximum packet size
|
// MQTT_MAX_PACKET_SIZE : Maximum packet size
|
||||||
|
#ifndef MQTT_MAX_PACKET_SIZE
|
||||||
#define MQTT_MAX_PACKET_SIZE 128
|
#define MQTT_MAX_PACKET_SIZE 128
|
||||||
|
#endif
|
||||||
|
|
||||||
// MQTT_KEEPALIVE : keepAlive interval in Seconds
|
// MQTT_KEEPALIVE : keepAlive interval in Seconds
|
||||||
|
#ifndef MQTT_KEEPALIVE
|
||||||
#define MQTT_KEEPALIVE 15
|
#define MQTT_KEEPALIVE 15
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
|
||||||
|
#ifndef MQTT_SOCKET_TIMEOUT
|
||||||
|
#define MQTT_SOCKET_TIMEOUT 15
|
||||||
|
#endif
|
||||||
|
|
||||||
// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
|
// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
|
||||||
// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
|
// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
|
||||||
@ -62,7 +73,12 @@
|
|||||||
#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)
|
#ifdef ESP8266
|
||||||
|
#include <functional>
|
||||||
|
#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
|
||||||
|
#else
|
||||||
|
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
|
||||||
|
#endif
|
||||||
|
|
||||||
class PubSubClient {
|
class PubSubClient {
|
||||||
private:
|
private:
|
||||||
@ -74,7 +90,8 @@ private:
|
|||||||
bool pingOutstanding;
|
bool pingOutstanding;
|
||||||
MQTT_CALLBACK_SIGNATURE;
|
MQTT_CALLBACK_SIGNATURE;
|
||||||
uint16_t readPacket(uint8_t*);
|
uint16_t readPacket(uint8_t*);
|
||||||
uint8_t readByte();
|
boolean readByte(uint8_t * result);
|
||||||
|
boolean readByte(uint8_t * result, uint16_t * index);
|
||||||
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(const char* string, uint8_t* buf, uint16_t pos);
|
uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
|
||||||
IPAddress ip;
|
IPAddress ip;
|
||||||
@ -111,6 +128,7 @@ public:
|
|||||||
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
|
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
|
||||||
void disconnect();
|
void disconnect();
|
||||||
boolean publish(const char* topic, const char* payload);
|
boolean publish(const char* topic, const char* payload);
|
||||||
|
boolean publish(const char* topic, const char* payload, boolean retained);
|
||||||
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
|
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
|
||||||
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
||||||
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
|
@ -4,9 +4,9 @@ TEST_SRC=$(wildcard ${SRC_PATH}/*_spec.cpp)
|
|||||||
TEST_BIN= $(TEST_SRC:${SRC_PATH}/%.cpp=${OUT_PATH}/%)
|
TEST_BIN= $(TEST_SRC:${SRC_PATH}/%.cpp=${OUT_PATH}/%)
|
||||||
VPATH=${SRC_PATH}
|
VPATH=${SRC_PATH}
|
||||||
SHIM_FILES=${SRC_PATH}/lib/*.cpp
|
SHIM_FILES=${SRC_PATH}/lib/*.cpp
|
||||||
PSC_FILE=../PubSubClient/src/PubSubClient.cpp
|
PSC_FILE=../src/PubSubClient.cpp
|
||||||
CC=g++
|
CC=g++
|
||||||
CFLAGS=-I${SRC_PATH}/lib -I../PubSubClient/src
|
CFLAGS=-I${SRC_PATH}/lib -I../src
|
||||||
|
|
||||||
all: $(TEST_BIN)
|
all: $(TEST_BIN)
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ int test_publish_bytes() {
|
|||||||
|
|
||||||
|
|
||||||
int test_publish_retained() {
|
int test_publish_retained() {
|
||||||
IT("publishes retained");
|
IT("publishes retained - 1");
|
||||||
ShimClient shimClient;
|
ShimClient shimClient;
|
||||||
shimClient.setAllowConnect(true);
|
shimClient.setAllowConnect(true);
|
||||||
|
|
||||||
@ -88,6 +88,29 @@ int test_publish_retained() {
|
|||||||
END_IT
|
END_IT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int test_publish_retained_2() {
|
||||||
|
IT("publishes retained - 2");
|
||||||
|
ShimClient shimClient;
|
||||||
|
shimClient.setAllowConnect(true);
|
||||||
|
|
||||||
|
byte connack[] = { 0x20, 0x02, 0x00, 0x00 };
|
||||||
|
shimClient.respond(connack,4);
|
||||||
|
|
||||||
|
PubSubClient client(server, 1883, callback, shimClient);
|
||||||
|
int rc = client.connect((char*)"client_test1");
|
||||||
|
IS_TRUE(rc);
|
||||||
|
|
||||||
|
byte publish[] = {0x31,0xc,0x0,0x5,0x74,0x6f,0x70,0x69,0x63,'A','B','C','D','E'};
|
||||||
|
shimClient.expect(publish,14);
|
||||||
|
|
||||||
|
rc = client.publish((char*)"topic",(char*)"ABCDE",true);
|
||||||
|
IS_TRUE(rc);
|
||||||
|
|
||||||
|
IS_FALSE(shimClient.error());
|
||||||
|
|
||||||
|
END_IT
|
||||||
|
}
|
||||||
|
|
||||||
int test_publish_not_connected() {
|
int test_publish_not_connected() {
|
||||||
IT("publish fails when not connected");
|
IT("publish fails when not connected");
|
||||||
ShimClient shimClient;
|
ShimClient shimClient;
|
||||||
@ -158,6 +181,7 @@ int main()
|
|||||||
test_publish();
|
test_publish();
|
||||||
test_publish_bytes();
|
test_publish_bytes();
|
||||||
test_publish_retained();
|
test_publish_retained();
|
||||||
|
test_publish_retained_2();
|
||||||
test_publish_not_connected();
|
test_publish_not_connected();
|
||||||
test_publish_too_long();
|
test_publish_too_long();
|
||||||
test_publish_P();
|
test_publish_P();
|
||||||
|
Reference in New Issue
Block a user