added WiFi reconnect check
This commit is contained in:
@@ -51,6 +51,14 @@ class Network {
|
||||
*/
|
||||
static void checkMQTT();
|
||||
|
||||
/**
|
||||
* @brief Checks if WiFi is still connected
|
||||
*
|
||||
* If WiFi is not connected, this function try a reconnect.
|
||||
* This function should be called every mainloop.
|
||||
*/
|
||||
static void checkWiFi();
|
||||
|
||||
/**
|
||||
* @brief Get the Mqtt Client object
|
||||
*
|
||||
|
||||
+12
-5
@@ -14,9 +14,20 @@
|
||||
|
||||
// General config
|
||||
#define MQTT_TIME_RECONNECT 2500
|
||||
#define HW1
|
||||
#define ESP32BROKER
|
||||
|
||||
|
||||
//Network config ESP32
|
||||
#ifdef ESP32BROKER
|
||||
#define WLAN_SSID "ESP32-Broker"
|
||||
#define WLAN_PASSWORD "doit2022"
|
||||
#define WLAN_IP "192.168.4.77"
|
||||
#define WLAN_SUBNETMASK "255.255.255.0"
|
||||
#define WLAN_GATEWAY "192.168.4.1"
|
||||
#define MQTT_SERVER "192.168.4.1"
|
||||
#define MQTT_PORT 1883
|
||||
#endif //ESP32BROKER
|
||||
|
||||
//Network config RHEDE
|
||||
#ifdef RHEDE
|
||||
#define WLAN_SSID "LebennigHuus"
|
||||
@@ -29,7 +40,6 @@
|
||||
#define MQTT_AUTH
|
||||
#define MQTT_USER "kleiax"
|
||||
#define MQTT_PASSWORD "p?{$_~5%hBM7wrcFkr55KWr#"
|
||||
#define NTP_SERVER "2.de.pool.ntp.org"
|
||||
#endif //RHEDE
|
||||
|
||||
//Network config HW1
|
||||
@@ -41,7 +51,6 @@
|
||||
#define WLAN_GATEWAY "192.168.0.1"
|
||||
#define MQTT_SERVER "172.22.64.216"
|
||||
#define MQTT_PORT 1883
|
||||
#define NTP_SERVER "2.de.pool.ntp.org"
|
||||
#endif //HW1
|
||||
|
||||
//Network config FRENZY
|
||||
@@ -53,7 +62,6 @@
|
||||
#define WLAN_GATEWAY 0xfe01a8c0 //192.168.1.254
|
||||
#define MQTT_SERVER 0x0201a8c0 //192.168.1.2
|
||||
#define MQTT_PORT 1883
|
||||
#define NTP_SERVER 0x0201a8c0 //192.168.1.2
|
||||
#endif //FRENZY
|
||||
|
||||
//Network config TPMOBIL
|
||||
@@ -65,5 +73,4 @@
|
||||
#define WLAN_GATEWAY 0x0101a8c0 //192.168.1.1
|
||||
#define MQTT_SERVER 0x0201a8c0 //192.168.1.2
|
||||
#define MQTT_PORT 1883
|
||||
#define NTP_SERVER 0x0201a8c0 //192.168.1.2
|
||||
#endif //TPMOBIL
|
||||
|
||||
@@ -23,3 +23,6 @@ lib_deps =
|
||||
br3ttb/PID@^1.2.1
|
||||
marcoschwartz/LiquidCrystal_I2C@^1.1.4
|
||||
upload_port = COM3
|
||||
|
||||
[platformio]
|
||||
description = A Rover who should be drive a route by gps.
|
||||
|
||||
@@ -85,6 +85,7 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Network::checkWiFi();
|
||||
Network::checkMQTT();
|
||||
driveManager.loop();
|
||||
}
|
||||
|
||||
@@ -86,6 +86,19 @@ void Network::checkMQTT() {
|
||||
mqtt_client->loop();
|
||||
}
|
||||
|
||||
void Network::checkWiFi() {
|
||||
static uint64_t previousMillis = 0;
|
||||
static uint16_t delay = 5000;
|
||||
uint64_t currentMillis = millis();
|
||||
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= delay)) {
|
||||
std::cout << "Reconnecting to WiFi..." << std::endl;
|
||||
WiFi.disconnect();
|
||||
WiFi.reconnect();
|
||||
previousMillis = currentMillis;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
PubSubClient* Network::getMqttClient() {
|
||||
return mqtt_client;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user