Hi,
da es immer wieder Probleme mit meinem ESP8266 und einem verlässlichen Reconnect gab, habe ich jetzt mal was zusammen geschustert.
So blockiert das WiFi Reconnect nicht mehr und das unzuverlässige "WiFi.status() != WL_CONNECTED" habe ich auch ersetzt.
Aber irgendwie kommt mir das recht "gross" vor.
Kann man das vielleicht irgendwie zusammenfassen?
Danke & Gruß
Peff
//=======Libraries
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoOTA.h>
#include <TelnetPrint.h>
//=======SETTINGS
//Timer für Heartbeat 1 sec.
unsigned long HeartbeatMillis = 0; // will store last time Heartbeat was running
const long Heartbeatinterval = 1000; // interval at which to send (milliseconds)
//Timer für WiFi reconnect 300 sec.
unsigned long WiFiMillis = 0; // will store last time WiFi Reconnect was running
const long WiFiinterval = 60000; // interval at which to send (milliseconds)
bool WiFiconnecting = false; // Schalter für Wifi reconnect
int WiFiconnecting_count = 0; // Counter für WiFi connection
bool Wifiactive = false; // Schalter für Wifi verbunden
bool WifiOff = false; // Schalter für Wifi OFF
int WifiOff_count = 0; //Counter wie lange WiFi Off bleiben soll
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
//============SETUP
void setup() {
byte i = 0;
Serial.begin(9600);
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
Connect();
Serial.print(F("Connecting WiFi."));
while (WiFi.localIP() == IPAddress(0, 0, 0, 0)) {
delay (500);
++i;
Serial.print(F("."));
if (i > 20) {
ESP.restart();
}
}
ideOTASetup();
ArduinoOTA.begin();
}
//============LOOP
void loop() {
time_t now;
time(&now);
if (Wifiactive == true) {
server.handleClient(); // Listen for HTTP requests from clients
delay(2); //allow the cpu to switch to other tasks
ArduinoOTA.handle(); // IDE OTA Upload via ArduinoIDE
}
unsigned long currentMillis = millis();
if (currentMillis - HeartbeatMillis >= Heartbeatinterval) {
HeartbeatMillis = currentMillis;
Serial.print(F("Wifiactive: "));
Serial.println(Wifiactive);
Serial.println(WiFi.localIP());
Serial.println(WiFi.status());
Serial.print(F("Wifi-OFF: "));
Serial.println(WifiOff);
Serial.print(F("Wifi-OFF_count: "));
Serial.println(WifiOff_count);
Serial.print(F("WiFi-Connecting: "));
Serial.println(WiFiconnecting);
Serial.print(F("WiFi-Connecting_count: "));
Serial.println(WiFiconnecting_count);
Serial.print(F("Unix-Time: "));
Serial.println(now);
if (Wifiactive == false && WifiOff == false && WiFiconnecting_count == 0) {
Serial.println(F("Switching WiFi Off, no Wifi available"));
WiFi.mode(WIFI_OFF);
WifiOff = true;
WifiOff_count = 60;
}
if (WifiOff == true) {
--WifiOff_count;
}
if (WifiOff == true && WifiOff_count == 0) {
WiFiconnecting = true;
}
if (Wifiactive == false && WiFiconnecting == true) {
Serial.println("Try to reconnect");
WiFiconnecting = false;
Connect();
WiFiconnecting_count = 20;
}
if (Wifiactive == true && WiFiconnecting_count > 0) {
WiFiconnecting_count = 0;
}
if (Wifiactive == false && WiFiconnecting_count > 0) {
--WiFiconnecting_count;
}
if (Wifiactive == true) {
TelnetPrint.print(F("."));
}
Serial.println(F("___________________________"));
}
}
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Wifiactive = true;
TelnetPrint.begin();
Serial.println(F("Connected to Wi-Fi."));
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Wifiactive = false;
TelnetPrint.stop();
Serial.println(F("Disconnected from Wi-Fi."));
}
void Connect() {
WiFi.setAutoReconnect(false);
WiFi.mode(WIFI_STA);
WiFi.hostname("ESP8266-TEST");
WiFi.begin(ssid, password);
WifiOff = false;
}
void ideOTASetup()
{
ArduinoOTA.setHostname("ESP8266-TEST");
ArduinoOTA.onStart([]() {
TelnetPrint.println("[otaide] OTA started");
});
}