upgrad esp mqtt lib

This commit is contained in:
technyon
2023-02-26 23:04:21 +01:00
parent b14ea6326e
commit aa224e8c7d
24 changed files with 219 additions and 229 deletions

View File

@@ -66,7 +66,7 @@ espMqttClientAsync()
```
Instantiate a new espMqttClient or espMqttSecure object.
On ESP32, two optional parameters are available: `espMqttClient(uint8_t priority = 1, uint8_t core = 1)`. This will change the priority of the MQTT client task and the core on which it runs (higher priority = more cpu-time).
On ESP32, three optional parameters are available: `espMqttClient(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1)`. By default, espMqttclient creates its own task to manage TCP. By setting `internalTask` to false, no task will be created and you will be responsible yourself to call `espMqttClient.loop()`. `priority` changes the priority of the MQTT client task and the core on which it runs (higher priority = more cpu-time).
For the asynchronous version, use `espMqttClientAsync`.
@@ -150,6 +150,15 @@ Set the server.
- **`host`**: Host of the server, expects a null-terminated char array (c-string)
- **`port`**: Port of the server
```cpp
espMqttClient& setTimeout(uint16_t timeout)
```
Set the timeout for packets that need acknowledgement. Defaults to 10 seconds.
When no acknowledgement has been received from the broker after sending a packet, the client will retransmit **all** the packets in the queue.
* **`timeout`**: Timeout in seconds
#### Options for TLS connections
All common options from WiFiClientSecure to setup an encrypted connection are made available. These include:
@@ -324,7 +333,7 @@ Keep in mind that this may also delete any session data and therefore is not MQT
void loop()
```
This is the worker function of the MQTT client. For ESP8266 you must call this function in the Arduino loop. For ESP32 this function is only used internally and is not available in the API.
This is the worker function of the MQTT client. For ESP8266 you must call this function in the Arduino loop. For ESP32 you have to call this function yourself **only if you have disabled the internal task** (see the constructors).
```cpp
const char* getClientId() const

View File

@@ -1,4 +1,6 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -10,6 +12,8 @@
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClient mqttClient;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;
size_t fetchPayload(uint8_t* dest, size_t len, size_t index) {
Serial.printf("filling buffer at index %zu\n", index);
@@ -33,7 +37,13 @@ void connectToWiFi() {
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}
void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
@@ -56,7 +66,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
connectToMqtt();
reconnectMqtt = true;
lastReconnect = millis();
}
}
@@ -85,5 +96,10 @@ void setup() {
}
void loop() {
static uint32_t currentMillis = millis();
mqttClient.loop();
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}
}

View File

@@ -1,5 +1,7 @@
#include <ESP8266WiFi.h>
#include <Updater.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -13,6 +15,8 @@
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClient mqttClient;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;
bool disconnectFlag = false;
bool restartFlag = false;
@@ -23,7 +27,13 @@ void connectToWiFi() {
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}
void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
@@ -53,7 +63,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
}
if (WiFi.isConnected()) {
connectToMqtt();
reconnectMqtt = true;
lastReconnect = millis();
}
}
@@ -125,16 +136,22 @@ void setup() {
}
void loop() {
mqttClient.loop();
if (disconnectFlag) {
// it's safe to call this multiple times
mqttClient.disconnect();
}
if (restartFlag) {
Serial.println("Rebooting... See you next time!");
Serial.flush();
ESP.reset();
}
static uint32_t currentMillis = millis();
mqttClient.loop();
if (!disconnectFlag && reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}
if (disconnectFlag) {
// it's safe to call this multiple times
mqttClient.disconnect();
}
}

View File

@@ -1,4 +1,6 @@
#include <WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -8,6 +10,8 @@
#define MQTT_PORT 1883
espMqttClient mqttClient;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -16,7 +20,13 @@ void connectToWiFi() {
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}
void WiFiEvent(WiFiEvent_t event) {
@@ -57,7 +67,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
connectToMqtt();
reconnectMqtt = true;
lastReconnect = millis();
}
}
@@ -122,5 +133,9 @@ void setup() {
}
void loop() {
// nothing to do here
static uint32_t currentMillis = millis();
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}
}

View File

@@ -1,4 +1,6 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -10,6 +12,8 @@
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClient mqttClient;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -18,7 +22,13 @@ void connectToWiFi() {
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}
void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
@@ -51,7 +61,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
connectToMqtt();
reconnectMqtt = true;
lastReconnect = millis();
}
}
@@ -117,5 +128,10 @@ void setup() {
}
void loop() {
static uint32_t currentMillis = millis();
mqttClient.loop();
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}
}

View File

@@ -1,4 +1,6 @@
#include <WiFi.h>
#include <Ticker.h>
#include <espMqttClientAsync.h>
#define WIFI_SSID "yourSSID"
@@ -8,6 +10,8 @@
#define MQTT_PORT 1883
espMqttClientAsync mqttClient;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -16,7 +20,13 @@ void connectToWiFi() {
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}
void WiFiEvent(WiFiEvent_t event) {
@@ -57,7 +67,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
connectToMqtt();
reconnectMqtt = true;
lastReconnect = millis();
}
}
@@ -122,5 +133,9 @@ void setup() {
}
void loop() {
// nothing to do here
static uint32_t currentMillis = millis();
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}
}

View File

@@ -1,4 +1,6 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <espMqttClientAsync.h>
#define WIFI_SSID "yourSSID"
@@ -10,6 +12,8 @@
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClientAsync mqttClient;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -18,7 +22,13 @@ void connectToWiFi() {
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}
void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
@@ -51,7 +61,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
connectToMqtt();
reconnectMqtt = true;
lastReconnect = millis();
}
}
@@ -117,5 +128,9 @@ void setup() {
}
void loop() {
// nothing to do here
static uint32_t currentMillis = millis();
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}
}

View File

@@ -1,4 +1,6 @@
#include <WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -15,6 +17,8 @@ const char rootCA[] = \
"-----END CERTIFICATE-----\n";
espMqttClientSecure mqttClient;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -23,7 +27,13 @@ void connectToWiFi() {
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}
void WiFiEvent(WiFiEvent_t event) {
@@ -61,7 +71,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
connectToMqtt();
reconnectMqtt = true;
lastReconnect = millis();
}
}
@@ -130,15 +141,21 @@ void setup() {
}
void loop() {
static uint32_t currentMillis = millis();
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}
static uint32_t lastMillis = 0;
if (millis() - lastMillis > 5000) {
lastMillis = millis();
if (currentMillis - lastMillis > 5000) {
lastMillis = currentMillis;
Serial.printf("heap: %u\n", ESP.getFreeHeap());
}
static uint32_t millisDisconnect = 0;
if (millis() - millisDisconnect > 60000) {
millisDisconnect = millis();
if (currentMillis - millisDisconnect > 60000) {
millisDisconnect = currentMillis;
mqttClient.disconnect();
}
}

View File

@@ -1,4 +1,6 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -13,6 +15,8 @@ const uint8_t fingerprint[] = {0xee, 0xbc, 0x4b, 0xf8, 0x57, 0xe3, 0xd3, 0xe4, 0
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClientSecure mqttClient;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -21,7 +25,13 @@ void connectToWiFi() {
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}
void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
@@ -54,7 +64,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
connectToMqtt();
reconnectMqtt = true;
lastReconnect = millis();
}
}
@@ -121,5 +132,10 @@ void setup() {
}
void loop() {
static uint32_t currentMillis = millis();
mqttClient.loop();
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}
}

View File

@@ -42,6 +42,7 @@ MqttClient::MqttClient()
, _willPayloadLength(0)
, _willQos(0)
, _willRetain(false)
, _timeout(10000)
, _state(State::disconnected)
, _generatedClientId{0}
, _packetId(0)
@@ -373,6 +374,9 @@ int MqttClient::_sendPacket() {
EMC_SEMAPHORE_GIVE();
return -1;
}
// handle with care! millis() returns unsigned 32 bit, token is void*
static_assert(sizeof(uint32_t) <= sizeof(void*), "the size of uint32_t must be smaller than or equal to the size of a pointer");
packet->token = reinterpret_cast<void*>(millis());
_lastClientActivity = millis();
_bytesSent += written;
emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->size(), packet->packetType());
@@ -392,8 +396,7 @@ bool MqttClient::_advanceOutbox() {
if (packet->removable()) {
_outbox.removeCurrent();
} else {
// handle with care! millis() returns unsigned 32 bit, token is void*
packet->token = reinterpret_cast<void*>(millis());
// we already set 'dup' here, in case we have to retry
if ((packet->packetType()) == PacketType.PUBLISH) packet->setDup();
_outbox.next();
}
@@ -496,6 +499,17 @@ void MqttClient::_checkPing() {
}
}
void MqttClient::_checkTimeout() {
espMqttClientInternals::Outbox<espMqttClientInternals::Packet>::Iterator it = _outbox.front();
if (it && _bytesSent == 0) { // check that we're not busy sending
if (millis() - *((uint32_t*)&(it.get()->token)) > _timeout) { // NOLINT(readability/casting)
// TODO(bertmelis): fix ugly casting hack
emc_log_w("Packet ack timeout, retrying");
_outbox.resetCurrent();
}
}
}
void MqttClient::_onConnack() {
if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) {
_pingSent = false; // reset after keepalive timeout disconnect

View File

@@ -65,17 +65,13 @@ class MqttClient {
uint16_t publish(const char* topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length);
void clearQueue(bool deleteSessionData = false); // Not MQTT compliant and may cause unpredictable results when `deleteSessionData` = true!
const char* getClientId() const;
#if defined(ARDUINO_ARCH_ESP32)
void loop();
protected:
#endif
void loop();
#if defined(ARDUINO_ARCH_ESP32)
explicit MqttClient(bool useTask, uint8_t priority = 1, uint8_t core = 1);
bool _useTask;
#else
protected:
MqttClient();
#endif
espMqttClientInternals::Transport* _transport;
@@ -102,6 +98,7 @@ class MqttClient {
uint16_t _willPayloadLength;
uint8_t _willQos;
bool _willRetain;
uint32_t _timeout;
// state is protected to allow state changes by the transport system, defined in child classes
// eg. to allow AsyncTCP
@@ -166,6 +163,7 @@ class MqttClient {
bool _advanceOutbox();
void _checkIncoming();
void _checkPing();
void _checkTimeout();
void _onConnack();
void _onPublish();

View File

@@ -68,6 +68,11 @@ class MqttClientSetup : public MqttClient {
return static_cast<T&>(*this);
}
T& setTimeout(uint16_t timeout) {
_timeout = timeout * 1000; // s to ms conversion, will also do 16 to 32 bit conversion
return static_cast<T&>(*this);
}
T& onConnect(espMqttClientTypes::OnConnectCallback callback) {
_onConnectCallback = callback;
return static_cast<T&>(*this);

View File

@@ -138,6 +138,10 @@ class Outbox {
return nullptr;
}
void resetCurrent() {
_current = _first;
}
Iterator front() const {
Iterator it;
it._node = _first;

View File

@@ -9,8 +9,8 @@ the LICENSE file.
#include "espMqttClient.h"
#if defined(ARDUINO_ARCH_ESP32)
espMqttClient::espMqttClient(uint8_t priority, uint8_t core)
: MqttClientSetup(true, priority, core)
espMqttClient::espMqttClient(bool internalTask, uint8_t priority, uint8_t core)
: MqttClientSetup(internalTask, priority, core)
, _client() {
#else
espMqttClient::espMqttClient()
@@ -21,8 +21,8 @@ espMqttClient::espMqttClient()
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core)
: MqttClientSetup(priority, core)
espMqttClientSecure::espMqttClientSecure(bool internalTask, uint8_t priority, uint8_t core)
: MqttClientSetup(internalTask, priority, core)
, _client() {
#else
espMqttClientSecure::espMqttClientSecure()

View File

@@ -23,7 +23,7 @@ the LICENSE file.
class espMqttClient : public MqttClientSetup<espMqttClient> {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1);
explicit espMqttClient(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1);
#else
espMqttClient();
#endif
@@ -40,7 +40,7 @@ class espMqttClient : public MqttClientSetup<espMqttClient> {
class espMqttClientSecure : public MqttClientSetup<espMqttClientSecure> {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1);
explicit espMqttClientSecure(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1);
#else
espMqttClientSecure();
#endif