upadte esp mqtt client

This commit is contained in:
technyon
2023-02-26 14:34:08 +01:00
parent 8fd17206dd
commit b14ea6326e
27 changed files with 454 additions and 203 deletions

View File

@@ -1,5 +1,4 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -11,7 +10,6 @@
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClient mqttClient;
Ticker reconnectTimer;
size_t fetchPayload(uint8_t* dest, size_t len, size_t index) {
Serial.printf("filling buffer at index %zu\n", index);
@@ -45,8 +43,6 @@ void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
reconnectTimer.once(5, connectToWiFi);
}
void onMqttConnect(bool sessionPresent) {
@@ -60,7 +56,7 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
reconnectTimer.once(5, connectToMqtt);
connectToMqtt();
}
}
@@ -75,6 +71,8 @@ void setup() {
Serial.println();
Serial.println();
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect);

View File

@@ -1,6 +1,5 @@
#include <ESP8266WiFi.h>
#include <Updater.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -14,7 +13,6 @@
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClient mqttClient;
Ticker reconnectTimer;
bool disconnectFlag = false;
bool restartFlag = false;
@@ -35,8 +33,6 @@ void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
reconnectTimer.once(5, connectToWiFi);
}
void onMqttConnect(bool sessionPresent) {
@@ -57,7 +53,7 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
}
if (WiFi.isConnected()) {
reconnectTimer.once(5, connectToMqtt);
connectToMqtt();
}
}
@@ -114,6 +110,8 @@ void setup() {
Serial.println();
Serial.println();
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect);

View File

@@ -1,5 +1,4 @@
#include <WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -9,7 +8,6 @@
#define MQTT_PORT 1883
espMqttClient mqttClient;
Ticker reconnectTimer;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -32,7 +30,6 @@ void WiFiEvent(WiFiEvent_t event) {
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
reconnectTimer.once(5, connectToWiFi);
break;
default:
break;
@@ -60,7 +57,7 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
reconnectTimer.once(5, connectToMqtt);
connectToMqtt();
}
}
@@ -109,6 +106,8 @@ void setup() {
Serial.println();
Serial.println();
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);

View File

@@ -1,5 +1,4 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -11,7 +10,6 @@
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClient mqttClient;
Ticker reconnectTimer;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -30,8 +28,6 @@ void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
reconnectTimer.once(5, connectToWiFi);
}
void onMqttConnect(bool sessionPresent) {
@@ -55,7 +51,7 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
reconnectTimer.once(5, connectToMqtt);
connectToMqtt();
}
}
@@ -104,6 +100,8 @@ void setup() {
Serial.println();
Serial.println();
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect);

View File

@@ -1,5 +1,4 @@
#include <WiFi.h>
#include <Ticker.h>
#include <espMqttClientAsync.h>
#define WIFI_SSID "yourSSID"
@@ -9,7 +8,6 @@
#define MQTT_PORT 1883
espMqttClientAsync mqttClient;
Ticker reconnectTimer;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -32,7 +30,6 @@ void WiFiEvent(WiFiEvent_t event) {
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
reconnectTimer.once(5, connectToWiFi);
break;
default:
break;
@@ -60,7 +57,7 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
reconnectTimer.once(5, connectToMqtt);
connectToMqtt();
}
}
@@ -109,6 +106,8 @@ void setup() {
Serial.println();
Serial.println();
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);

View File

@@ -1,5 +1,4 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <espMqttClientAsync.h>
#define WIFI_SSID "yourSSID"
@@ -11,7 +10,6 @@
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClientAsync mqttClient;
Ticker reconnectTimer;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -30,8 +28,6 @@ void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
reconnectTimer.once(5, connectToWiFi);
}
void onMqttConnect(bool sessionPresent) {
@@ -55,7 +51,7 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
reconnectTimer.once(5, connectToMqtt);
connectToMqtt();
}
}
@@ -104,6 +100,8 @@ void setup() {
Serial.println();
Serial.println();
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect);

View File

@@ -1,5 +1,4 @@
#include <WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -16,7 +15,6 @@ const char rootCA[] = \
"-----END CERTIFICATE-----\n";
espMqttClientSecure mqttClient;
Ticker reconnectTimer;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -39,7 +37,6 @@ void WiFiEvent(WiFiEvent_t event) {
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
reconnectTimer.once(5, connectToWiFi);
break;
default:
break;
@@ -64,7 +61,7 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
reconnectTimer.once(5, connectToMqtt);
connectToMqtt();
}
}
@@ -113,6 +110,8 @@ void setup() {
Serial.println();
Serial.println();
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
WiFi.onEvent(WiFiEvent);
//mqttClient.setInsecure();

View File

@@ -1,5 +1,4 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <espMqttClient.h>
#define WIFI_SSID "yourSSID"
@@ -14,7 +13,6 @@ const uint8_t fingerprint[] = {0xee, 0xbc, 0x4b, 0xf8, 0x57, 0xe3, 0xd3, 0xe4, 0
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
espMqttClientSecure mqttClient;
Ticker reconnectTimer;
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
@@ -33,8 +31,6 @@ void onWiFiConnect(const WiFiEventStationModeGotIP& event) {
void onWiFiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
reconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
reconnectTimer.once(5, connectToWiFi);
}
void onMqttConnect(bool sessionPresent) {
@@ -58,7 +54,7 @@ void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));
if (WiFi.isConnected()) {
reconnectTimer.once(5, connectToMqtt);
connectToMqtt();
}
}
@@ -107,6 +103,8 @@ void setup() {
Serial.println();
Serial.println();
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
wifiConnectHandler = WiFi.onStationModeGotIP(onWiFiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWiFiDisconnect);