upgrad esp mqtt lib
This commit is contained in:
@@ -53,7 +53,6 @@ file(GLOB SRCFILES
|
|||||||
networkDevices/EthLan8720Device.cpp
|
networkDevices/EthLan8720Device.cpp
|
||||||
networkDevices/ClientSyncW5500.cpp
|
networkDevices/ClientSyncW5500.cpp
|
||||||
networkDevices/espMqttClientW5500.cpp
|
networkDevices/espMqttClientW5500.cpp
|
||||||
networkDevices/espMqttClientWifi.cpp
|
|
||||||
QueryCommand.h
|
QueryCommand.h
|
||||||
NukiWrapper.cpp
|
NukiWrapper.cpp
|
||||||
NukiOpenerWrapper.cpp
|
NukiOpenerWrapper.cpp
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ espMqttClientAsync()
|
|||||||
```
|
```
|
||||||
|
|
||||||
Instantiate a new espMqttClient or espMqttSecure object.
|
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`.
|
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)
|
- **`host`**: Host of the server, expects a null-terminated char array (c-string)
|
||||||
- **`port`**: Port of the server
|
- **`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
|
#### Options for TLS connections
|
||||||
|
|
||||||
All common options from WiFiClientSecure to setup an encrypted connection are made available. These include:
|
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()
|
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
|
```cpp
|
||||||
const char* getClientId() const
|
const char* getClientId() const
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
#include <espMqttClient.h>
|
#include <espMqttClient.h>
|
||||||
|
|
||||||
#define WIFI_SSID "yourSSID"
|
#define WIFI_SSID "yourSSID"
|
||||||
@@ -10,6 +12,8 @@
|
|||||||
WiFiEventHandler wifiConnectHandler;
|
WiFiEventHandler wifiConnectHandler;
|
||||||
WiFiEventHandler wifiDisconnectHandler;
|
WiFiEventHandler wifiDisconnectHandler;
|
||||||
espMqttClient mqttClient;
|
espMqttClient mqttClient;
|
||||||
|
bool reconnectMqtt = false;
|
||||||
|
uint32_t lastReconnect = 0;
|
||||||
|
|
||||||
size_t fetchPayload(uint8_t* dest, size_t len, size_t index) {
|
size_t fetchPayload(uint8_t* dest, size_t len, size_t index) {
|
||||||
Serial.printf("filling buffer at index %zu\n", index);
|
Serial.printf("filling buffer at index %zu\n", index);
|
||||||
@@ -33,7 +37,13 @@ void connectToWiFi() {
|
|||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
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) {
|
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));
|
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
if (WiFi.isConnected()) {
|
||||||
connectToMqtt();
|
reconnectMqtt = true;
|
||||||
|
lastReconnect = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,5 +96,10 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
static uint32_t currentMillis = millis();
|
||||||
|
|
||||||
mqttClient.loop();
|
mqttClient.loop();
|
||||||
|
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
|
||||||
|
connectToMqtt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <Updater.h>
|
#include <Updater.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
#include <espMqttClient.h>
|
#include <espMqttClient.h>
|
||||||
|
|
||||||
#define WIFI_SSID "yourSSID"
|
#define WIFI_SSID "yourSSID"
|
||||||
@@ -13,6 +15,8 @@
|
|||||||
WiFiEventHandler wifiConnectHandler;
|
WiFiEventHandler wifiConnectHandler;
|
||||||
WiFiEventHandler wifiDisconnectHandler;
|
WiFiEventHandler wifiDisconnectHandler;
|
||||||
espMqttClient mqttClient;
|
espMqttClient mqttClient;
|
||||||
|
bool reconnectMqtt = false;
|
||||||
|
uint32_t lastReconnect = 0;
|
||||||
bool disconnectFlag = false;
|
bool disconnectFlag = false;
|
||||||
bool restartFlag = false;
|
bool restartFlag = false;
|
||||||
|
|
||||||
@@ -23,7 +27,13 @@ void connectToWiFi() {
|
|||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
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) {
|
void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
|
||||||
@@ -53,7 +63,8 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
if (WiFi.isConnected()) {
|
||||||
connectToMqtt();
|
reconnectMqtt = true;
|
||||||
|
lastReconnect = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,16 +136,22 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
mqttClient.loop();
|
|
||||||
|
|
||||||
if (disconnectFlag) {
|
|
||||||
// it's safe to call this multiple times
|
|
||||||
mqttClient.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (restartFlag) {
|
if (restartFlag) {
|
||||||
Serial.println("Rebooting... See you next time!");
|
Serial.println("Rebooting... See you next time!");
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
ESP.reset();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
#include <espMqttClient.h>
|
#include <espMqttClient.h>
|
||||||
|
|
||||||
#define WIFI_SSID "yourSSID"
|
#define WIFI_SSID "yourSSID"
|
||||||
@@ -8,6 +10,8 @@
|
|||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
|
|
||||||
espMqttClient mqttClient;
|
espMqttClient mqttClient;
|
||||||
|
bool reconnectMqtt = false;
|
||||||
|
uint32_t lastReconnect = 0;
|
||||||
|
|
||||||
void connectToWiFi() {
|
void connectToWiFi() {
|
||||||
Serial.println("Connecting to Wi-Fi...");
|
Serial.println("Connecting to Wi-Fi...");
|
||||||
@@ -16,7 +20,13 @@ void connectToWiFi() {
|
|||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
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) {
|
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));
|
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
if (WiFi.isConnected()) {
|
||||||
connectToMqtt();
|
reconnectMqtt = true;
|
||||||
|
lastReconnect = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,5 +133,9 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// nothing to do here
|
static uint32_t currentMillis = millis();
|
||||||
|
|
||||||
|
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
|
||||||
|
connectToMqtt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
#include <espMqttClient.h>
|
#include <espMqttClient.h>
|
||||||
|
|
||||||
#define WIFI_SSID "yourSSID"
|
#define WIFI_SSID "yourSSID"
|
||||||
@@ -10,6 +12,8 @@
|
|||||||
WiFiEventHandler wifiConnectHandler;
|
WiFiEventHandler wifiConnectHandler;
|
||||||
WiFiEventHandler wifiDisconnectHandler;
|
WiFiEventHandler wifiDisconnectHandler;
|
||||||
espMqttClient mqttClient;
|
espMqttClient mqttClient;
|
||||||
|
bool reconnectMqtt = false;
|
||||||
|
uint32_t lastReconnect = 0;
|
||||||
|
|
||||||
void connectToWiFi() {
|
void connectToWiFi() {
|
||||||
Serial.println("Connecting to Wi-Fi...");
|
Serial.println("Connecting to Wi-Fi...");
|
||||||
@@ -18,7 +22,13 @@ void connectToWiFi() {
|
|||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
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) {
|
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));
|
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
if (WiFi.isConnected()) {
|
||||||
connectToMqtt();
|
reconnectMqtt = true;
|
||||||
|
lastReconnect = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,5 +128,10 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
static uint32_t currentMillis = millis();
|
||||||
|
|
||||||
mqttClient.loop();
|
mqttClient.loop();
|
||||||
|
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
|
||||||
|
connectToMqtt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
#include <espMqttClientAsync.h>
|
#include <espMqttClientAsync.h>
|
||||||
|
|
||||||
#define WIFI_SSID "yourSSID"
|
#define WIFI_SSID "yourSSID"
|
||||||
@@ -8,6 +10,8 @@
|
|||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
|
|
||||||
espMqttClientAsync mqttClient;
|
espMqttClientAsync mqttClient;
|
||||||
|
bool reconnectMqtt = false;
|
||||||
|
uint32_t lastReconnect = 0;
|
||||||
|
|
||||||
void connectToWiFi() {
|
void connectToWiFi() {
|
||||||
Serial.println("Connecting to Wi-Fi...");
|
Serial.println("Connecting to Wi-Fi...");
|
||||||
@@ -16,7 +20,13 @@ void connectToWiFi() {
|
|||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
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) {
|
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));
|
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
if (WiFi.isConnected()) {
|
||||||
connectToMqtt();
|
reconnectMqtt = true;
|
||||||
|
lastReconnect = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,5 +133,9 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// nothing to do here
|
static uint32_t currentMillis = millis();
|
||||||
|
|
||||||
|
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
|
||||||
|
connectToMqtt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
#include <espMqttClientAsync.h>
|
#include <espMqttClientAsync.h>
|
||||||
|
|
||||||
#define WIFI_SSID "yourSSID"
|
#define WIFI_SSID "yourSSID"
|
||||||
@@ -10,6 +12,8 @@
|
|||||||
WiFiEventHandler wifiConnectHandler;
|
WiFiEventHandler wifiConnectHandler;
|
||||||
WiFiEventHandler wifiDisconnectHandler;
|
WiFiEventHandler wifiDisconnectHandler;
|
||||||
espMqttClientAsync mqttClient;
|
espMqttClientAsync mqttClient;
|
||||||
|
bool reconnectMqtt = false;
|
||||||
|
uint32_t lastReconnect = 0;
|
||||||
|
|
||||||
void connectToWiFi() {
|
void connectToWiFi() {
|
||||||
Serial.println("Connecting to Wi-Fi...");
|
Serial.println("Connecting to Wi-Fi...");
|
||||||
@@ -18,7 +22,13 @@ void connectToWiFi() {
|
|||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
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) {
|
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));
|
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
if (WiFi.isConnected()) {
|
||||||
connectToMqtt();
|
reconnectMqtt = true;
|
||||||
|
lastReconnect = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,5 +128,9 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// nothing to do here
|
static uint32_t currentMillis = millis();
|
||||||
|
|
||||||
|
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
|
||||||
|
connectToMqtt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
#include <espMqttClient.h>
|
#include <espMqttClient.h>
|
||||||
|
|
||||||
#define WIFI_SSID "yourSSID"
|
#define WIFI_SSID "yourSSID"
|
||||||
@@ -15,6 +17,8 @@ const char rootCA[] = \
|
|||||||
"-----END CERTIFICATE-----\n";
|
"-----END CERTIFICATE-----\n";
|
||||||
|
|
||||||
espMqttClientSecure mqttClient;
|
espMqttClientSecure mqttClient;
|
||||||
|
bool reconnectMqtt = false;
|
||||||
|
uint32_t lastReconnect = 0;
|
||||||
|
|
||||||
void connectToWiFi() {
|
void connectToWiFi() {
|
||||||
Serial.println("Connecting to Wi-Fi...");
|
Serial.println("Connecting to Wi-Fi...");
|
||||||
@@ -23,7 +27,13 @@ void connectToWiFi() {
|
|||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
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) {
|
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));
|
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
if (WiFi.isConnected()) {
|
||||||
connectToMqtt();
|
reconnectMqtt = true;
|
||||||
|
lastReconnect = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,15 +141,21 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
static uint32_t currentMillis = millis();
|
||||||
|
|
||||||
|
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
|
||||||
|
connectToMqtt();
|
||||||
|
}
|
||||||
|
|
||||||
static uint32_t lastMillis = 0;
|
static uint32_t lastMillis = 0;
|
||||||
if (millis() - lastMillis > 5000) {
|
if (currentMillis - lastMillis > 5000) {
|
||||||
lastMillis = millis();
|
lastMillis = currentMillis;
|
||||||
Serial.printf("heap: %u\n", ESP.getFreeHeap());
|
Serial.printf("heap: %u\n", ESP.getFreeHeap());
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t millisDisconnect = 0;
|
static uint32_t millisDisconnect = 0;
|
||||||
if (millis() - millisDisconnect > 60000) {
|
if (currentMillis - millisDisconnect > 60000) {
|
||||||
millisDisconnect = millis();
|
millisDisconnect = currentMillis;
|
||||||
mqttClient.disconnect();
|
mqttClient.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
#include <espMqttClient.h>
|
#include <espMqttClient.h>
|
||||||
|
|
||||||
#define WIFI_SSID "yourSSID"
|
#define WIFI_SSID "yourSSID"
|
||||||
@@ -13,6 +15,8 @@ const uint8_t fingerprint[] = {0xee, 0xbc, 0x4b, 0xf8, 0x57, 0xe3, 0xd3, 0xe4, 0
|
|||||||
WiFiEventHandler wifiConnectHandler;
|
WiFiEventHandler wifiConnectHandler;
|
||||||
WiFiEventHandler wifiDisconnectHandler;
|
WiFiEventHandler wifiDisconnectHandler;
|
||||||
espMqttClientSecure mqttClient;
|
espMqttClientSecure mqttClient;
|
||||||
|
bool reconnectMqtt = false;
|
||||||
|
uint32_t lastReconnect = 0;
|
||||||
|
|
||||||
void connectToWiFi() {
|
void connectToWiFi() {
|
||||||
Serial.println("Connecting to Wi-Fi...");
|
Serial.println("Connecting to Wi-Fi...");
|
||||||
@@ -21,7 +25,13 @@ void connectToWiFi() {
|
|||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
Serial.println("Connecting to MQTT...");
|
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) {
|
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));
|
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
if (WiFi.isConnected()) {
|
||||||
connectToMqtt();
|
reconnectMqtt = true;
|
||||||
|
lastReconnect = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,5 +132,10 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
static uint32_t currentMillis = millis();
|
||||||
|
|
||||||
mqttClient.loop();
|
mqttClient.loop();
|
||||||
|
if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
|
||||||
|
connectToMqtt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -42,6 +42,7 @@ MqttClient::MqttClient()
|
|||||||
, _willPayloadLength(0)
|
, _willPayloadLength(0)
|
||||||
, _willQos(0)
|
, _willQos(0)
|
||||||
, _willRetain(false)
|
, _willRetain(false)
|
||||||
|
, _timeout(10000)
|
||||||
, _state(State::disconnected)
|
, _state(State::disconnected)
|
||||||
, _generatedClientId{0}
|
, _generatedClientId{0}
|
||||||
, _packetId(0)
|
, _packetId(0)
|
||||||
@@ -373,6 +374,9 @@ int MqttClient::_sendPacket() {
|
|||||||
EMC_SEMAPHORE_GIVE();
|
EMC_SEMAPHORE_GIVE();
|
||||||
return -1;
|
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();
|
_lastClientActivity = millis();
|
||||||
_bytesSent += written;
|
_bytesSent += written;
|
||||||
emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->size(), packet->packetType());
|
emc_log_i("tx %zu/%zu (%02x)", _bytesSent, packet->size(), packet->packetType());
|
||||||
@@ -392,8 +396,7 @@ bool MqttClient::_advanceOutbox() {
|
|||||||
if (packet->removable()) {
|
if (packet->removable()) {
|
||||||
_outbox.removeCurrent();
|
_outbox.removeCurrent();
|
||||||
} else {
|
} else {
|
||||||
// handle with care! millis() returns unsigned 32 bit, token is void*
|
// we already set 'dup' here, in case we have to retry
|
||||||
packet->token = reinterpret_cast<void*>(millis());
|
|
||||||
if ((packet->packetType()) == PacketType.PUBLISH) packet->setDup();
|
if ((packet->packetType()) == PacketType.PUBLISH) packet->setDup();
|
||||||
_outbox.next();
|
_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() {
|
void MqttClient::_onConnack() {
|
||||||
if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) {
|
if (_parser.getPacket().variableHeader.fixed.connackVarHeader.returnCode == 0x00) {
|
||||||
_pingSent = false; // reset after keepalive timeout disconnect
|
_pingSent = false; // reset after keepalive timeout disconnect
|
||||||
|
|||||||
@@ -65,17 +65,13 @@ class MqttClient {
|
|||||||
uint16_t publish(const char* topic, uint8_t qos, bool retain, espMqttClientTypes::PayloadCallback callback, size_t length);
|
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!
|
void clearQueue(bool deleteSessionData = false); // Not MQTT compliant and may cause unpredictable results when `deleteSessionData` = true!
|
||||||
const char* getClientId() const;
|
const char* getClientId() const;
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
void loop();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
#endif
|
|
||||||
void loop();
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
explicit MqttClient(bool useTask, uint8_t priority = 1, uint8_t core = 1);
|
explicit MqttClient(bool useTask, uint8_t priority = 1, uint8_t core = 1);
|
||||||
bool _useTask;
|
bool _useTask;
|
||||||
#else
|
#else
|
||||||
|
|
||||||
protected:
|
|
||||||
MqttClient();
|
MqttClient();
|
||||||
#endif
|
#endif
|
||||||
espMqttClientInternals::Transport* _transport;
|
espMqttClientInternals::Transport* _transport;
|
||||||
@@ -102,6 +98,7 @@ class MqttClient {
|
|||||||
uint16_t _willPayloadLength;
|
uint16_t _willPayloadLength;
|
||||||
uint8_t _willQos;
|
uint8_t _willQos;
|
||||||
bool _willRetain;
|
bool _willRetain;
|
||||||
|
uint32_t _timeout;
|
||||||
|
|
||||||
// state is protected to allow state changes by the transport system, defined in child classes
|
// state is protected to allow state changes by the transport system, defined in child classes
|
||||||
// eg. to allow AsyncTCP
|
// eg. to allow AsyncTCP
|
||||||
@@ -166,6 +163,7 @@ class MqttClient {
|
|||||||
bool _advanceOutbox();
|
bool _advanceOutbox();
|
||||||
void _checkIncoming();
|
void _checkIncoming();
|
||||||
void _checkPing();
|
void _checkPing();
|
||||||
|
void _checkTimeout();
|
||||||
|
|
||||||
void _onConnack();
|
void _onConnack();
|
||||||
void _onPublish();
|
void _onPublish();
|
||||||
|
|||||||
@@ -68,6 +68,11 @@ class MqttClientSetup : public MqttClient {
|
|||||||
return static_cast<T&>(*this);
|
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) {
|
T& onConnect(espMqttClientTypes::OnConnectCallback callback) {
|
||||||
_onConnectCallback = callback;
|
_onConnectCallback = callback;
|
||||||
return static_cast<T&>(*this);
|
return static_cast<T&>(*this);
|
||||||
|
|||||||
@@ -138,6 +138,10 @@ class Outbox {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void resetCurrent() {
|
||||||
|
_current = _first;
|
||||||
|
}
|
||||||
|
|
||||||
Iterator front() const {
|
Iterator front() const {
|
||||||
Iterator it;
|
Iterator it;
|
||||||
it._node = _first;
|
it._node = _first;
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ the LICENSE file.
|
|||||||
#include "espMqttClient.h"
|
#include "espMqttClient.h"
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
espMqttClient::espMqttClient(uint8_t priority, uint8_t core)
|
espMqttClient::espMqttClient(bool internalTask, uint8_t priority, uint8_t core)
|
||||||
: MqttClientSetup(true, priority, core)
|
: MqttClientSetup(internalTask, priority, core)
|
||||||
, _client() {
|
, _client() {
|
||||||
#else
|
#else
|
||||||
espMqttClient::espMqttClient()
|
espMqttClient::espMqttClient()
|
||||||
@@ -21,8 +21,8 @@ espMqttClient::espMqttClient()
|
|||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core)
|
espMqttClientSecure::espMqttClientSecure(bool internalTask, uint8_t priority, uint8_t core)
|
||||||
: MqttClientSetup(priority, core)
|
: MqttClientSetup(internalTask, priority, core)
|
||||||
, _client() {
|
, _client() {
|
||||||
#else
|
#else
|
||||||
espMqttClientSecure::espMqttClientSecure()
|
espMqttClientSecure::espMqttClientSecure()
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ the LICENSE file.
|
|||||||
class espMqttClient : public MqttClientSetup<espMqttClient> {
|
class espMqttClient : public MqttClientSetup<espMqttClient> {
|
||||||
public:
|
public:
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#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
|
#else
|
||||||
espMqttClient();
|
espMqttClient();
|
||||||
#endif
|
#endif
|
||||||
@@ -40,7 +40,7 @@ class espMqttClient : public MqttClientSetup<espMqttClient> {
|
|||||||
class espMqttClientSecure : public MqttClientSetup<espMqttClientSecure> {
|
class espMqttClientSecure : public MqttClientSetup<espMqttClientSecure> {
|
||||||
public:
|
public:
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#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
|
#else
|
||||||
espMqttClientSecure();
|
espMqttClientSecure();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -106,8 +106,8 @@ void setupTasks()
|
|||||||
// configMAX_PRIORITIES is 25
|
// configMAX_PRIORITIES is 25
|
||||||
|
|
||||||
xTaskCreatePinnedToCore(networkTask, "ntw", 8192, NULL, 3, &networkTaskHandle, 1);
|
xTaskCreatePinnedToCore(networkTask, "ntw", 8192, NULL, 3, &networkTaskHandle, 1);
|
||||||
xTaskCreatePinnedToCore(nukiTask, "nuki", 4096, NULL, 2, &nukiTaskHandle, 1);
|
xTaskCreatePinnedToCore(nukiTask, "nuki", 3328, NULL, 2, &nukiTaskHandle, 1);
|
||||||
xTaskCreatePinnedToCore(presenceDetectionTask, "prdet", 768, NULL, 5, &presenceDetectionTaskHandle, 1);
|
xTaskCreatePinnedToCore(presenceDetectionTask, "prdet", 896, NULL, 5, &presenceDetectionTaskHandle, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getRandomId()
|
uint32_t getRandomId()
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* _prefere
|
|||||||
{
|
{
|
||||||
Log->println(F("MQTT over TLS."));
|
Log->println(F("MQTT over TLS."));
|
||||||
Log->println(_ca);
|
Log->println(_ca);
|
||||||
_mqttClientSecure = new espMqttClientWifiSecure();
|
_mqttClientSecure = new espMqttClientSecure();
|
||||||
_mqttClientSecure->setCACert(_ca);
|
_mqttClientSecure->setCACert(_ca);
|
||||||
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
||||||
{
|
{
|
||||||
@@ -38,7 +38,7 @@ EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* _prefere
|
|||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
Log->println(F("MQTT without TLS."));
|
Log->println(F("MQTT without TLS."));
|
||||||
_mqttClient = new espMqttClientWifi();
|
_mqttClient = new espMqttClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||||
@@ -112,11 +112,11 @@ void EthLan8720Device::update()
|
|||||||
{
|
{
|
||||||
if(_useEncryption)
|
if(_useEncryption)
|
||||||
{
|
{
|
||||||
_mqttClientSecure->update();
|
_mqttClientSecure->loop();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_mqttClient->update();
|
_mqttClient->loop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <WiFiClientSecure.h>
|
#include <WiFiClientSecure.h>
|
||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
#include "NetworkDevice.h"
|
#include "NetworkDevice.h"
|
||||||
#include "espMqttClientWifi.h"
|
#include "espMqttClient.h"
|
||||||
|
|
||||||
class EthLan8720Device : public NetworkDevice
|
class EthLan8720Device : public NetworkDevice
|
||||||
{
|
{
|
||||||
@@ -54,8 +54,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
void onDisconnected();
|
void onDisconnected();
|
||||||
|
|
||||||
espMqttClientWifi* _mqttClient = nullptr;
|
espMqttClient* _mqttClient = nullptr;
|
||||||
espMqttClientWifiSecure* _mqttClientSecure = nullptr;
|
espMqttClientSecure* _mqttClientSecure = nullptr;
|
||||||
|
|
||||||
bool _restartOnDisconnect = false;
|
bool _restartOnDisconnect = false;
|
||||||
bool _startAp = false;
|
bool _startAp = false;
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ void W5500Device::initializeMacAddress(byte *mac)
|
|||||||
void W5500Device::update()
|
void W5500Device::update()
|
||||||
{
|
{
|
||||||
_maintainResult = Ethernet.maintain();
|
_maintainResult = Ethernet.maintain();
|
||||||
_mqttClient.update();
|
_mqttClient.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t W5500Device::signalStrength()
|
int8_t W5500Device::signalStrength()
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
|||||||
{
|
{
|
||||||
Log->println(F("MQTT over TLS."));
|
Log->println(F("MQTT over TLS."));
|
||||||
Log->println(_ca);
|
Log->println(_ca);
|
||||||
_mqttClientSecure = new espMqttClientWifiSecure();
|
_mqttClientSecure = new espMqttClientSecure();
|
||||||
_mqttClientSecure->setCACert(_ca);
|
_mqttClientSecure->setCACert(_ca);
|
||||||
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
||||||
{
|
{
|
||||||
@@ -38,7 +38,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
|||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
Log->println(F("MQTT without TLS."));
|
Log->println(F("MQTT without TLS."));
|
||||||
_mqttClient = new espMqttClientWifi();
|
_mqttClient = new espMqttClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||||
@@ -144,11 +144,11 @@ void WifiDevice::update()
|
|||||||
{
|
{
|
||||||
if(_useEncryption)
|
if(_useEncryption)
|
||||||
{
|
{
|
||||||
_mqttClientSecure->update();
|
_mqttClientSecure->loop();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_mqttClient->update();
|
_mqttClient->loop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
#include "NetworkDevice.h"
|
#include "NetworkDevice.h"
|
||||||
#include "WiFiManager.h"
|
#include "WiFiManager.h"
|
||||||
#include "espMqttClientWifi.h"
|
#include "espMqttClient.h"
|
||||||
|
|
||||||
class WifiDevice : public NetworkDevice
|
class WifiDevice : public NetworkDevice
|
||||||
{
|
{
|
||||||
@@ -58,8 +58,8 @@ private:
|
|||||||
void onDisconnected();
|
void onDisconnected();
|
||||||
|
|
||||||
WiFiManager _wm;
|
WiFiManager _wm;
|
||||||
espMqttClientWifi* _mqttClient = nullptr;
|
espMqttClient* _mqttClient = nullptr;
|
||||||
espMqttClientWifiSecure* _mqttClientSecure = nullptr;
|
espMqttClientSecure* _mqttClientSecure = nullptr;
|
||||||
|
|
||||||
bool _restartOnDisconnect = false;
|
bool _restartOnDisconnect = false;
|
||||||
bool _startAp = false;
|
bool _startAp = false;
|
||||||
|
|||||||
@@ -1,97 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2022 Bert Melis. All rights reserved.
|
|
||||||
|
|
||||||
This work is licensed under the terms of the MIT license.
|
|
||||||
For a copy, see <https://opensource.org/licenses/MIT> or
|
|
||||||
the LICENSE file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "espMqttClientWifi.h"
|
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
espMqttClientWifi::espMqttClientWifi(uint8_t priority, uint8_t core)
|
|
||||||
: MqttClientSetup(false, priority, core)
|
|
||||||
, _client() {
|
|
||||||
#else
|
|
||||||
espMqttClient::espMqttClient()
|
|
||||||
: _client() {
|
|
||||||
#endif
|
|
||||||
_transport = &_client;
|
|
||||||
}
|
|
||||||
|
|
||||||
void espMqttClientWifi::update()
|
|
||||||
{
|
|
||||||
loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
espMqttClientWifiSecure::espMqttClientWifiSecure(uint8_t priority, uint8_t core)
|
|
||||||
: MqttClientSetup(false, priority, core)
|
|
||||||
, _client() {
|
|
||||||
#else
|
|
||||||
espMqttClientSecure::espMqttClientSecure()
|
|
||||||
: _client() {
|
|
||||||
#endif
|
|
||||||
_transport = &_client;
|
|
||||||
}
|
|
||||||
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setInsecure() {
|
|
||||||
_client.client.setInsecure();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setCACert(const char* rootCA) {
|
|
||||||
_client.client.setCACert(rootCA);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setCertificate(const char* clientCa) {
|
|
||||||
_client.client.setCertificate(clientCa);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setPrivateKey(const char* privateKey) {
|
|
||||||
_client.client.setPrivateKey(privateKey);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setPreSharedKey(const char* pskIdent, const char* psKey) {
|
|
||||||
_client.client.setPreSharedKey(pskIdent, psKey);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void espMqttClientWifiSecure::update()
|
|
||||||
{
|
|
||||||
loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(ARDUINO_ARCH_ESP8266)
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setFingerprint(const uint8_t fingerprint[20]) {
|
|
||||||
_client.client.setFingerprint(fingerprint);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setTrustAnchors(const X509List *ta) {
|
|
||||||
_client.client.setTrustAnchors(ta);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) {
|
|
||||||
_client.client.setClientRSACert(cert, sk);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) {
|
|
||||||
_client.client.setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
espMqttClientWifiSecure& espMqttClientWifiSecure::setCertStore(CertStoreBase *certStore) {
|
|
||||||
_client.client.setCertStore(certStore);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2022 Bert Melis. All rights reserved.
|
|
||||||
|
|
||||||
API is based on the original work of Marvin Roger:
|
|
||||||
https://github.com/marvinroger/async-mqtt-client
|
|
||||||
|
|
||||||
This work is licensed under the terms of the MIT license.
|
|
||||||
For a copy, see <https://opensource.org/licenses/MIT> or
|
|
||||||
the LICENSE file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
|
||||||
#include "Transport/ClientSync.h"
|
|
||||||
#include "Transport/ClientSecureSync.h"
|
|
||||||
#elif defined(__linux__)
|
|
||||||
#include "Transport/ClientPosix.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "MqttClientSetup.h"
|
|
||||||
|
|
||||||
class espMqttClientWifi : public MqttClientSetup<espMqttClientWifi> {
|
|
||||||
public:
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
explicit espMqttClientWifi(uint8_t priority = 1, uint8_t core = 1);
|
|
||||||
#else
|
|
||||||
espMqttClient();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void update();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
|
||||||
espMqttClientInternals::ClientSync _client;
|
|
||||||
#elif defined(__linux__)
|
|
||||||
espMqttClientInternals::ClientPosix _client;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
|
||||||
class espMqttClientWifiSecure : public MqttClientSetup<espMqttClientWifiSecure> {
|
|
||||||
public:
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
explicit espMqttClientWifiSecure(uint8_t priority = 1, uint8_t core = 1);
|
|
||||||
#else
|
|
||||||
espMqttClientSecure();
|
|
||||||
#endif
|
|
||||||
espMqttClientWifiSecure& setInsecure();
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
|
||||||
espMqttClientWifiSecure& setCACert(const char* rootCA);
|
|
||||||
espMqttClientWifiSecure& setCertificate(const char* clientCa);
|
|
||||||
espMqttClientWifiSecure& setPrivateKey(const char* privateKey);
|
|
||||||
espMqttClientWifiSecure& setPreSharedKey(const char* pskIdent, const char* psKey);
|
|
||||||
#else
|
|
||||||
espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]);
|
|
||||||
espMqttClientSecure& setTrustAnchors(const X509List *ta);
|
|
||||||
espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk);
|
|
||||||
espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type);
|
|
||||||
espMqttClientSecure& setCertStore(CertStoreBase *certStore);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void update();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
espMqttClientInternals::ClientSecureSync _client;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user