Skip to content

Commit

Permalink
Merge pull request plapointe6#31 from plapointe6/dev
Browse files Browse the repository at this point in the history
Merge dev to master - 1.7.0
  • Loading branch information
Patrick Lapointe authored Oct 21, 2019
2 parents eda500e + d8f6cad commit bbedbc1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 33 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ void loop();

Basic functions for MQTT communications.
```c++
void publish(const String &topic, const String &payload, bool retain = false);
void subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback);
void unsubscribe(const String &topic);
bool publish(const String &topic, const String &payload, bool retain = false);
bool subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback);
bool unsubscribe(const String &topic);
```
Enable the display of usefull debugging messages that will output to serial.
Expand All @@ -76,9 +76,16 @@ Enable last will message. Must be set before the first loop() call.
void enableLastWillMessage(const char* topic, const char* message, const bool retain = false);
```

Return true if everything is connected.
Connection status
```c++
bool isConnected()
bool isConnected(); // Return true if everything is connected.
bool isWifiConnected(); // Return true if WiFi is connected.
bool isMqttConnected(); // Return true if MQTT is connected.
```

Return the number of time onConnectionEstablished has been called since the beginning. Can be useful if you need to monitor the number of times the connection has dropped.
```c++
void getConnectionEstablishedCount();
```

As ESP8366 does not like to be interrupted too long with the delay() function, this function will allow a delayed execution of a function without interrupting the sketch.
Expand Down
24 changes: 16 additions & 8 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ EspMQTTClient KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################

loop KEYWORD2
isConnected KEYWORD2
publish KEYWORD2
subscribe KEYWORD2
unsubscribe KEYWORD2
executeDelayed KEYWORD2
enableDebuggingMessages KEYWORD2
enableHTTPWebUpdater KEYWORD2
enableHTTPWebUpdater KEYWORD2
enableMQTTPersistence KEYWORD2
enableLastWillMessage KEYWORD2
enableDebuggingMessages KEYWORD2

publish KEYWORD2
subscribe KEYWORD2
unsubscribe KEYWORD2

executeDelayed KEYWORD2

isConnected KEYWORD2
isWifiConnected KEYWORD2
isMqttConnected KEYWORD2
getConnectionEstablishedCount KEYWORD2

setOnConnectionEstablishedCallback KEYWORD2

onConnectionEstablished KEYWORD2
onConnectionEstablished KEYWORD2
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type": "git",
"url": "https://github.com/plapointe6/EspMQTTClient.git"
},
"version": "1.6.2",
"version": "1.7.0",
"frameworks": "arduino",
"platforms": ["espressif8266", "espressif32"],
"dependencies": [
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=EspMQTTClient
version=1.6.2
version=1.7.0
author=Patrick Lapointe <[email protected]>
maintainer=Patrick Lapointe <[email protected]>
sentence=A library that provides a wifi and MQTT connection to an ESP8266/ESP32
Expand Down
53 changes: 40 additions & 13 deletions src/EspMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ EspMQTTClient::EspMQTTClient(
mConnectionEstablishedCallback = onConnectionEstablished;
mShowLegacyConstructorWarning = false;
mDelayedExecutionListSize = 0;
mConnectionEstablishedCount = 0;
}

EspMQTTClient::~EspMQTTClient()
Expand Down Expand Up @@ -282,22 +283,29 @@ void EspMQTTClient::loop()

}

void EspMQTTClient::publish(const String &topic, const String &payload, bool retain)
bool EspMQTTClient::publish(const String &topic, const String &payload, bool retain)
{
mMqttClient.publish(topic.c_str(), payload.c_str(), retain);
bool success = mMqttClient.publish(topic.c_str(), payload.c_str(), retain);

if (mEnableSerialLogs)
Serial.printf("MQTT << [%s] %s\n", topic.c_str(), payload.c_str());
if (mEnableSerialLogs)
{
if(success)
Serial.printf("MQTT << [%s] %s\n", topic.c_str(), payload.c_str());
else
Serial.println("MQTT! publish failed, is the message too long ?"); // This can occurs if the message is too long according to the maximum defined in PubsubClient.h
}

return success;
}

void EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback)
bool EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback)
{
// Check the possibility to add a new topic
if (mTopicSubscriptionListSize >= MAX_TOPIC_SUBSCRIPTION_LIST_SIZE)
{
if (mEnableSerialLogs)
Serial.println("MQTT! Subscription list is full, ignored.");
return;
return false;
}

// Check the duplicate of the subscription to the topic
Expand All @@ -309,20 +317,30 @@ void EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messa
{
if (mEnableSerialLogs)
Serial.printf("MQTT! Subscribed to [%s] already, ignored.\n", topic.c_str());
return;
return false;
}

// All checks are passed - do the job
mMqttClient.subscribe(topic.c_str());
mTopicSubscriptionList[mTopicSubscriptionListSize++] = { topic, messageReceivedCallback };
bool success = mMqttClient.subscribe(topic.c_str());

if(success)
mTopicSubscriptionList[mTopicSubscriptionListSize++] = { topic, messageReceivedCallback };

if (mEnableSerialLogs)
Serial.printf("MQTT: Subscribed to [%s]\n", topic.c_str());
{
if(success)
Serial.printf("MQTT: Subscribed to [%s]\n", topic.c_str());
else
Serial.println("MQTT! subscribe failed");
}

return success;
}

void EspMQTTClient::unsubscribe(const String &topic)
bool EspMQTTClient::unsubscribe(const String &topic)
{
bool found = false;
bool success = false;

for (byte i = 0; i < mTopicSubscriptionListSize; i++)
{
Expand All @@ -331,9 +349,15 @@ void EspMQTTClient::unsubscribe(const String &topic)
if (mTopicSubscriptionList[i].topic.equals(topic))
{
found = true;
mMqttClient.unsubscribe(topic.c_str());
success = mMqttClient.unsubscribe(topic.c_str());

if (mEnableSerialLogs)
Serial.printf("MQTT: Unsubscribed from %s\n", topic.c_str());
{
if(success)
Serial.printf("MQTT: Unsubscribed from %s\n", topic.c_str());
else
Serial.println("MQTT! unsubscribe failed");
}
}
}

Expand All @@ -348,6 +372,8 @@ void EspMQTTClient::unsubscribe(const String &topic)
mTopicSubscriptionListSize--;
else if (mEnableSerialLogs)
Serial.println("MQTT! Topic cannot be found to unsubscribe, ignored.");

return success;
}

void EspMQTTClient::executeDelayed(const unsigned long delay, DelayedExecutionCallback callback)
Expand Down Expand Up @@ -396,6 +422,7 @@ void EspMQTTClient::connectToMqttBroker()
if (mEnableSerialLogs)
Serial.println("ok.");

mConnectionEstablishedCount++;
(*mConnectionEstablishedCallback)();
}
else if (mEnableSerialLogs)
Expand Down
14 changes: 9 additions & 5 deletions src/EspMQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class EspMQTTClient
ConnectionEstablishedCallback mConnectionEstablishedCallback;
bool mEnableSerialLogs;
bool mShowLegacyConstructorWarning;

unsigned int mConnectionEstablishedCount; // Incremented before each mConnectionEstablishedCallback call

public:
// Wifi + MQTT with no MQTT authentification
Expand Down Expand Up @@ -161,14 +161,18 @@ class EspMQTTClient
void loop();

// MQTT related
void publish(const String &topic, const String &payload, bool retain = false);
void subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback);
void unsubscribe(const String &topic); //Unsubscribes from the topic, if it exists, and removes it from the CallbackList.
bool publish(const String &topic, const String &payload, bool retain = false);
bool subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback);
bool unsubscribe(const String &topic); //Unsubscribes from the topic, if it exists, and removes it from the CallbackList.

// Other
void executeDelayed(const unsigned long delay, DelayedExecutionCallback callback);

inline bool isConnected() const { return mWifiConnected && mMqttConnected; };
inline bool isConnected() const { return isWifiConnected() && isMqttConnected(); }; // Return true if everything is connected
inline bool isWifiConnected() const { return mWifiConnected; }; // Return true if wifi is connected
inline bool isMqttConnected() const { return mMqttConnected; }; // Return true if mqtt is connected
inline bool getConnectionEstablishedCount() const { return mConnectionEstablishedCount; }; // Return the number of time onConnectionEstablished has been called since the beginning.

inline void setOnConnectionEstablishedCallback(ConnectionEstablishedCallback callback) { mConnectionEstablishedCallback = callback; }; // Default to onConnectionEstablished, you might want to override this for special cases like two MQTT connections in the same sketch

private:
Expand Down

0 comments on commit bbedbc1

Please sign in to comment.