feat: verbessere WLAN-Konfiguration und füge mDNS-Unterstützung hinzu
This commit is contained in:
parent
76100593cc
commit
944b156528
68
src/wlan.cpp
68
src/wlan.cpp
@ -3,6 +3,8 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
#include <WiFiManager.h>
|
#include <WiFiManager.h>
|
||||||
|
#include <DNSServer.h>
|
||||||
|
#include <ESPmDNS.h>
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
@ -10,10 +12,11 @@ WiFiManager wm;
|
|||||||
bool wm_nonblocking = false;
|
bool wm_nonblocking = false;
|
||||||
uint8_t wifiErrorCounter = 0;
|
uint8_t wifiErrorCounter = 0;
|
||||||
|
|
||||||
void initWiFi() {
|
void wifiSettings() {
|
||||||
// Optimierte WiFi-Einstellungen
|
// Optimierte WiFi-Einstellungen
|
||||||
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
|
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
|
||||||
WiFi.setSleep(false); // disable sleep mode
|
WiFi.setSleep(false); // disable sleep mode
|
||||||
|
WiFi.setHostname("FilaMan");
|
||||||
esp_wifi_set_ps(WIFI_PS_NONE);
|
esp_wifi_set_ps(WIFI_PS_NONE);
|
||||||
|
|
||||||
// Maximale Sendeleistung
|
// Maximale Sendeleistung
|
||||||
@ -24,19 +27,45 @@ void initWiFi() {
|
|||||||
|
|
||||||
// Aktiviere WiFi-Roaming für bessere Stabilität
|
// Aktiviere WiFi-Roaming für bessere Stabilität
|
||||||
esp_wifi_set_rssi_threshold(-80);
|
esp_wifi_set_rssi_threshold(-80);
|
||||||
|
}
|
||||||
|
|
||||||
|
void startMDNS() {
|
||||||
|
if (!MDNS.begin("filaman")) {
|
||||||
|
Serial.println("Error setting up MDNS responder!");
|
||||||
|
while(1) {
|
||||||
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.println("mDNS responder started");
|
||||||
|
}
|
||||||
|
|
||||||
|
void configModeCallback (WiFiManager *myWiFiManager) {
|
||||||
|
Serial.println("Entered config mode");
|
||||||
|
oledShowTopRow();
|
||||||
|
oledShowMessage("WiFi Config Mode");
|
||||||
|
}
|
||||||
|
|
||||||
|
void initWiFi() {
|
||||||
|
// load Wifi settings
|
||||||
|
wifiSettings();
|
||||||
|
|
||||||
|
wm.setAPCallback(configModeCallback);
|
||||||
|
|
||||||
|
wm.setSaveConfigCallback([]() {
|
||||||
|
Serial.println("Configurations updated");
|
||||||
|
ESP.restart();
|
||||||
|
});
|
||||||
|
|
||||||
if(wm_nonblocking) wm.setConfigPortalBlocking(false);
|
if(wm_nonblocking) wm.setConfigPortalBlocking(false);
|
||||||
wm.setConfigPortalTimeout(320); // Portal nach 5min schließen
|
//wm.setConfigPortalTimeout(320); // Portal nach 5min schließen
|
||||||
|
wm.setWiFiAutoReconnect(true);
|
||||||
|
wm.setConnectTimeout(5);
|
||||||
|
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
oledShowMessage("WiFi Setup");
|
oledShowMessage("WiFi Setup");
|
||||||
|
|
||||||
bool res;
|
//bool res = wm.autoConnect("FilaMan"); // anonymous ap
|
||||||
// res = wm.autoConnect(); // auto generated AP name from chipid
|
if(!wm.autoConnect("FilaMan")) {
|
||||||
res = wm.autoConnect("FilaMan"); // anonymous ap
|
|
||||||
// res = wm.autoConnect("spoolman","password"); // password protected ap
|
|
||||||
|
|
||||||
if(!res) {
|
|
||||||
Serial.println("Failed to connect or hit timeout");
|
Serial.println("Failed to connect or hit timeout");
|
||||||
// ESP.restart();
|
// ESP.restart();
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
@ -52,6 +81,11 @@ void initWiFi() {
|
|||||||
|
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
display.display();
|
display.display();
|
||||||
|
|
||||||
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
// mDNS
|
||||||
|
startMDNS();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,21 +93,31 @@ void checkWiFiConnection() {
|
|||||||
if (WiFi.status() != WL_CONNECTED)
|
if (WiFi.status() != WL_CONNECTED)
|
||||||
{
|
{
|
||||||
Serial.println("WiFi connection lost. Reconnecting...");
|
Serial.println("WiFi connection lost. Reconnecting...");
|
||||||
|
wifiOn = false;
|
||||||
oledShowTopRow();
|
oledShowTopRow();
|
||||||
oledShowMessage("WiFi reconnecting...");
|
oledShowMessage("WiFi reconnecting");
|
||||||
WiFi.reconnect(); // Versuche, die Verbindung wiederherzustellen
|
WiFi.reconnect(); // Versuche, die Verbindung wiederherzustellen
|
||||||
delay(5000); // Warte 5 Sekunden, bevor erneut geprüft wird
|
vTaskDelay(5000 / portTICK_PERIOD_MS); // Warte 5 Sekunden, bevor erneut geprüft wird
|
||||||
if (WiFi.status() != WL_CONNECTED)
|
if (WiFi.status() != WL_CONNECTED)
|
||||||
{
|
{
|
||||||
Serial.println("Failed to reconnect. Restarting WiFi...");
|
Serial.println("Failed to reconnect. Restarting WiFi...");
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
delay(1000);
|
Serial.println("WiFi disconnected.");
|
||||||
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
wifiErrorCounter++;
|
wifiErrorCounter++;
|
||||||
//initWiFi();
|
|
||||||
|
//wifiSettings();
|
||||||
|
WiFi.reconnect();
|
||||||
|
Serial.println("WiFi reconnecting...");
|
||||||
|
WiFi.waitForConnectResult();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Serial.println("WiFi reconnected.");
|
||||||
wifiErrorCounter = 0;
|
wifiErrorCounter = 0;
|
||||||
|
wifiOn = true;
|
||||||
|
oledShowTopRow();
|
||||||
|
startMDNS();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user