upadte esp mqtt client
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user