Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP mode I connect to ESP32 and configure WiFi, after several auto reconnections it Damages the memory(?), maybe because of WiFiManager #1796

Open
szczepul opened this issue Jan 26, 2025 · 0 comments

Comments

@szczepul
Copy link

Problem Description:

While using the WiFiManager library on the ESP32-WROOM-32D module, I encountered the following issue: after several cycles of disconnecting and reconnecting to a previously configured access point, the module becomes unresponsive. This manifests as a black screen and no response to button presses. The configuration page also becomes inaccessible.

Additionally, after such a state, it is necessary to reflash the program with the full flash memory erase option enabled. Without this, after restarting, the program operates in a completely unstable manner.

Attempts to Resolve:

  • Replaced the ESP32 module with a new unit.
  • Updated the WiFiManager library to the latest version.
  • Verified the correctness of the code and configuration.

Unfortunately, these actions did not yield the desired results.

Technical Details:

  • Module: ESP32-WROOM-32D devkitc v4
  • WiFiManager Library Version: [new]
  • Development Environment: Arduino IDE,
  • Firmware Version: [provide version]

I kindly request assistance in resolving this issue or guidance on steps to diagnose and fix it.

Thank you in advance for your help.

From Wifimanager 192.168.4.1/info:
`esp32
Uptime
4 mins 3 secs
Chip ID
a9286f24
Chip rev
100
Flash size
4194304 bytes
PSRAM Size
0 bytes
CPU frequency
240MHz
Memory - Free heap
216912 bytes available
Memory - Sketch size
Used / Total bytes
1046992 / 2357712

Temperature
66.67 C° / 177.60 F°

WiFi
Connected
No
Station SSID
Station IP
0.0.0.0
Station gateway
0.0.0.0
Station subnet
0.0.0.0
DNS Server
0.0.0.0
Hostname
esp32-A97894
Station MAC
24:6F:28:A9:78:94
Access point IP
192.168.4.1
Access point MAC
24:6F:28:A9:78:95
Access point hostname
espressif
BSSID
About
WiFiManager
v2.0.17
Arduino
3.1.1
Build date
Jan 25 2025 20:14:58
`

and my code Arduino IDE , Oled ssd1309
`/*
V2
Wersja testy_Ap_wifi 2

Pod projekt ORI oraz NaraTEMeratury

osiągnięto:

  • nazwa widziana w siecu ESPDevice
  • zmiana z poziomu WiFiManager (WMG) czas AP trybu i czas skanowania sieci WiFi
  • Łączenie z automatu do sieci zapisanych
  • tryb AP po przytrzymaniu 3 sek. klawisza wyboru
  • kontrolki stanu AP i WiFi na OLED ssd1309
  • na OLED wyświetla IP adr klienta albo IP konfiguracyjnej sieci trybu AP.

*/

#include <WiFi.h>
#include <WiFiManager.h>
#include <U8g2lib.h>
#include <Wire.h>

// Definicje pinów dla przycisków
#define BUTTON_UP 32
#define BUTTON_DOWN 33
#define BUTTON_SELECT 27

// Inicjalizacja wyświetlacza OLED
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);

// Zmienne globalne
String currentMenu = "MAIN";
int currentMenuOption = 0;
bool isAPMode = false;
unsigned long lastDisplayUpdate = 0;
const unsigned long displayUpdateInterval = 500;
unsigned long lastWiFiCheckTime = 0;
unsigned long wifiCheckInterval = 15000; // 15 sekund
unsigned long apStartTime = 0;
unsigned long apTimeout = 180000; // 3 minuty
unsigned long buttonPressTime = 0;
bool buttonHeld = false;
bool wrongPassword = false; // Flaga dla złego hasła Wi-Fi

WiFiManager wifiManager;
WiFiManagerParameter customAPTimeout("apTimeout", "Czas trybu AP (ms) min.120000: ", "180000", 7);
WiFiManagerParameter customWiFiScanInterval("wifiCheckInterval", "Interwał skanowania WiFi (ms)", "15000", 7);

void setup() {
WiFi.setHostname("MyESP32Device");

// Inicjalizacja przycisków
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);

// Inicjalizacja OLED
u8g2.begin();
u8g2.enableUTF8Print();
u8g2.setFont(u8g2_font_profont10_tr);

// Serial do debugowania
Serial.begin(115200);
Serial.println("Rozpoczęcie programu...");

// Dodajemy parametry do WiFiManager
wifiManager.addParameter(&customAPTimeout);
wifiManager.addParameter(&customWiFiScanInterval);

// WiFiManager w trybie nieblokującym
wifiManager.setConfigPortalBlocking(false);

Serial.printf("Zapisana sieć Wi-Fi to: '%s'", WiFiManager().getWiFiSSID().c_str());

// Automatyczne połączenie lub uruchomienie trybu AP
if (wifiManager.autoConnect("ORI_setupWWW")) {
    Serial.println("Połączono z zapisanym Wi-Fi.");
} else {
    Serial.println("Nie znaleziono zapisanej sieci, włączenie trybu AP.");
}

// Pobranie wartości z WiFiManager i ustawienie zakresów
apTimeout = constrain(atoi(customAPTimeout.getValue()), 120000, 1800000); // Min: 2 min, Max: 30 min
wifiCheckInterval = constrain(atoi(customWiFiScanInterval.getValue()), 5000, 60000); // Min: 5 sek, Max: 60 sek
Serial.printf("Ustawienia: apTimeout = %lu ms, wifiCheckInterval = %lu ms\n", apTimeout, wifiCheckInterval);

}

void loop() {
unsigned long currentMillis = millis();

// Obsługa WiFiManager w trybie NonBlocking
wifiManager.process();

// Sprawdzanie stanu Wi-Fi w tle
if (!isAPMode && currentMillis - lastWiFiCheckTime >= wifiCheckInterval) {
    lastWiFiCheckTime = currentMillis;
    if (!WiFi.isConnected()) {
        Serial.println("Skanowanie zapisanych sieci Wi-Fi w tle...");
        if (checkForSavedWiFi()) {
            WiFi.begin(WiFiManager().getWiFiSSID().c_str());
            unsigned long startAttemptTime = millis();
            while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 15000) {
                if (WiFi.status() == WL_CONNECT_FAILED) {
                    wrongPassword = true;
                    break;
                }
                delay(500);
                Serial.print(".");
            }

            if (WiFi.status() == WL_CONNECTED) {
                Serial.println("\nPołączono z zapisanym Wi-Fi.");
                wrongPassword = false;
            } else {
                Serial.println("\nNie udało się połączyć z Wi-Fi.");
            }
        }
    }
}

// Obsługa trybu AP
if (isAPMode) {
    if (currentMillis - apStartTime >= apTimeout) {
        Serial.println("Czas trybu AP minął. Wyłączanie AP...");
        disableAPMode();
    }
}

// Obsługa przycisku SELECT dla trybu AP
handleAPButton();

// Aktualizacja OLED
if (currentMillis - lastDisplayUpdate >= displayUpdateInterval) {
    lastDisplayUpdate = currentMillis;
    displayCurrentState();
}

// Obsługa przycisków menu
handleButtons();

}

void enableAPMode() {
WiFi.disconnect();
WiFi.mode(WIFI_AP);
WiFi.softAP("ORI_setupWWW");
isAPMode = true;
apStartTime = millis();
Serial.println("Tryb AP włączony.");
}

void disableAPMode() {
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
isAPMode = false;
Serial.println("Tryb AP wyłączony.");
}

void handleAPButton() {
if (digitalRead(BUTTON_SELECT) == LOW) {
if (!buttonHeld) {
buttonPressTime = millis();
buttonHeld = true;
} else if (millis() - buttonPressTime >= 2500) {
if (!isAPMode) {
enableAPMode();
} else {
disableAPMode();
}
buttonHeld = false; // Zapobiega wielokrotnemu włączeniu/wyłączeniu
}
} else {
buttonHeld = false;
}
}

bool checkForSavedWiFi() {
int n = WiFi.scanNetworks(); // Skanowanie sieci
if (n == 0) {
Serial.println("Brak dostępnych sieci Wi-Fi.");
return false;
}

Serial.println("Dostępne sieci Wi-Fi:");
Serial.printf("Zapisana sieć Wi-Fi to: '%s'\n", WiFiManager().getWiFiSSID().c_str());

for (int i = 0; i < n; i++) {
    Serial.printf("SSID: %s, RSSI: %d\n", WiFi.SSID(i).c_str(), WiFi.RSSI(i));
    if (WiFi.SSID(i) == WiFiManager().getWiFiSSID()) {
        Serial.println("Zapisana sieć Wi-Fi dostępna.");
        return true;
    }
}
return false;

}

void displayCurrentState() {
u8g2.clearBuffer();

// Pasek statusu
u8g2.setFont(u8g2_font_profont10_tr);

// AP status
if (isAPMode) {
    u8g2.drawBox(0, 0, 20, 10); // Tło podświetlenia dla AP
    u8g2.setDrawColor(0);
    u8g2.drawStr(2, 8, "AP");
    u8g2.setDrawColor(1);
} else {
    u8g2.drawStr(2, 8, "AP");
}

// Wi-Fi status
if (WiFi.status() == WL_CONNECTED) {
    u8g2.drawBox(25, 0, 20, 10); // Tło podświetlenia dla WF
    u8g2.setDrawColor(0);
    u8g2.drawStr(27, 8, "WF");
    u8g2.setDrawColor(1);
} else {
    u8g2.drawStr(27, 8, "WF");
}

// Adres IP lub komunikaty o błędach
char ipBuffer[20];
if (WiFi.status() == WL_CONNECTED) {
    snprintf(ipBuffer, sizeof(ipBuffer), "IP: %s", WiFi.localIP().toString().c_str());
} else if (isAPMode) {
    snprintf(ipBuffer, sizeof(ipBuffer), "AP: %s", WiFi.softAPIP().toString().c_str());
} else if (wrongPassword) {
    strcpy(ipBuffer, "WiFi: wrong pass");
} else {
    strcpy(ipBuffer, "Brak znanego WiFi");
}
u8g2.drawStr(0, 18, ipBuffer);

// Menu
u8g2.setFont(u8g2_font_helvR08_tr);
u8g2.drawStr(0, 35, currentMenuOption == 0 ? "> Opcja 1" : "  Opcja 1");
u8g2.drawStr(0, 48, currentMenuOption == 1 ? "> Ustawienia" : "  Ustawienia");

u8g2.sendBuffer();

}

void handleButtons() {
static unsigned long lastButtonPress = 0;
unsigned long currentMillis = millis();

if (currentMillis - lastButtonPress < 200) return; // Debounce

if (digitalRead(BUTTON_UP) == LOW) {
    currentMenuOption = (currentMenuOption - 1 + 2) % 2; // 2 opcje
    lastButtonPress = currentMillis;
    displayCurrentState(); // Odśwież wyświetlacz po zmianie
} else if (digitalRead(BUTTON_DOWN) == LOW) {
    currentMenuOption = (currentMenuOption + 1) % 2;
    lastButtonPress = currentMillis;
    displayCurrentState(); // Odśwież wyświetlacz po zmianie
} else if (digitalRead(BUTTON_SELECT) == LOW) {
    if (currentMenu == "MAIN") {
        if (currentMenuOption == 0) {
            Serial.println("Wybrano Opcja 1");
        }
    }
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant