tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.6k stars 1.98k forks source link

Changing connection at runtime, second call of autoconnect() portal webpage issue #832

Open Bjack795 opened 5 years ago

Bjack795 commented 5 years ago

Basic Infos

Hardware

WiFimanager Branch/Release:

Esp8266/Esp32:

Hardware: ESP-12e, esp01, esp25

Description

The first time I call autoconnect() function at startup() it's working fine. Then if I want to change the connection at runtime and I call it again the portal webpage is not working, even though the events in the Serial monitor are the same and I'm able to create the hotspot, I simply can't get access to the portal webpage.

tablatronix commented 5 years ago

You are calling autoconnect more than once? Not sure I understand

Code and logs help

Bjack795 commented 5 years ago

I explain it better. In my project your WiFimanager is called at startup with autoconnect() but I noticed that if I were outside my house the code was blocking there because no known wifi is found and it starts the portal loop never ending. So I modified it slightly, in practise I trigger the timeout exit after 5 "NUM CLIENTS". returning true for configPortalHasTimeout(). If I'm outside the code goes on after 5 apClientCheck. If I want I can open the portal before the timeout and register a wifi station, all goes ok.

Then I wanted to call this configuration on demand and not automatically only at the beginning, because maybe when you turn on the device you're not near to a wifi to be connected but then you are.

Calling again autoconnect() in the middle of the operations it starts the portal if no wifi is known, it gives me the IP. It also shows up the same [EVENTS] in the Serial Monitor (so no error is showing up) but when I connect to the created temporary wifi and I go to the IP address, the portal webpage is no more showing up.

In my opinion, the first time autoconnect() is used some variable is filled up and differs by default. So on the second call something is going wrong and alla works fine till portal webpage which is not showing up.

I have studied your code but there is something I'm missing, maybe you could tell me why on the second call I have these problems. Lastly I say that the second call it's happening in the same situation of the startup so I'm not already connected to any wifi even though I'm in the middle of runtime.

Thank you

Bjack795 commented 5 years ago

I put the two Serial outputs FIRST CALL

WM: [1] AutoConnect WM: [2] ESP32 event handler enabled WM: [2] [EVENT] 2 WM: [2] Connecting as wifi client... WM: [2] setSTAConfig static ip not set WM: [3] WIFI station disconnect WM: [1] No saved credentials, skipping wifi WM: [2] Connection result: WL_NO_SSID_AVAIL WM: [3] lastconxresult: WL_NO_SSID_AVAIL WM: [1] AutoConnect: FAILED WM: [2] AccessPoint set password is VALID WM: [1] password WM: [3] WIFI station disconnect WM: [3] WiFi station enable WM: [2] Disabling STAWM: [2] [EVENT] 3 WM: [2] Enabling AP WM: [1] StartAP with SSID: BjackWifi WM: [2] [EVENT] WM: [2] [EVENT] 3 WM: [2] [EVENT] 13 WM: [2] [EVENT] 14 WM: [2] [EVENT] 13 WM: [2] [EVENT] 13 WM: [1] AP IP address: 192.168.4.1 WM: [3] setupConfigPortal WM: [1] Starting Web Portal WM: [3] dns server started with ip: 192.168.4.1 WM: [2] HTTP server started WM: [2] WiFi Scan ASYNC started WM: [2] Config Portal Running, blocking, waiting for clients...WM: [2] [EVENT] 2 WM: [2] [EVENT] 1 WM: [2] WiFi Scan ASYNC completed in 3015 ms WM: [2] WiFi Scan ASYNC found: 3 WM: [2] NUM CLIENTS: 0 E (44745) event: mismatch or invalid event, id=63 E (44746) event: default event handler failed! WM: [2] [EVENT] 63 WM: [2] [EVENT] 15 WM: [2] [EVENT] 17 WM: [2] [EVENT] 17 *WM: [3] -> connectivitycheck.android.com

SECOND CALL

WM: [1] AutoConnect WM: [2] ESP32 event handler enabled WM: [2] Connecting as wifi client... WM: [2] setSTAConfig static ip not set WM: [3] WIFI station disconnect WM: [1] No saved credentials, skipping wifi WM: [2] Connection result: WL_NO_SSID_AVAIL WM: [3] lastconxresult: WL_NO_SSID_AVAIL WM: [1] AutoConnect: FAILED WM: [2] AccessPoint set password is VALID WM: [1] password WM: [3] WIFI station disconnect WM: [3] WiFi station enable WM: [2] Disabling STAWM: [2] [EVENT] 3 WM: [2] [EVENT] 3 WM: [2] Enabling AP WM: [1] StartAP with SSID: BjackWifi WM: [2] [EVENT] WM: [2] [EVENT] WM: [2] [EVENT] 3 WM: [2] [EVENT] 3 WM: [2] [EVENT] 13 WM: [2] [EVENT] 13 WM: [2] [EVENT] 14 WM: [2] [EVENT] 14 WM: [2] [EVENT] 13 WM: [2] [EVENT] 13 WM: [2] [EVENT] 13 WM: [2] [EVENT] 13 WM: [1] AP IP address: 192.168.4.1 WM: [3] setupConfigPortal WM: [1] Starting Web Portal WM: [3] dns server started with ip: 192.168.4.1 WM: [2] HTTP server started WM: [2] WiFi Scan ASYNC started WM: [2] Config Portal Running, blocking, waiting for clients...WM: [2] [EVENT] 2 WM: [2] [EVENT] 2 WM: [2] NUM CLIENTS: 0 WM: [2] [EVENT] 1 WM: [2] WiFi Scan ASYNC completed in 3016 ms WM: [2] WiFi Scan ASYNC found: 5 WM: [2] [EVENT] 1 WM: [2] WiFi Scan ASYNC completed in 3017 ms WM: [2] WiFi Scan ASYNC found: 5 E (43441) event: mismatch or invalid event, id=63 E (43442) event: default event handler failed! WM: [2] [EVENT] 63 WM: [2] [EVENT] 63 WM: [2] [EVENT] 15 WM: [2] [EVENT] 15 WM: [2] [EVENT] 17 WM: [2] [EVENT] 17 WM: [2] [EVENT] 17 WM: [2] [EVENT] 17 WM: [2] NUM CLIENTS: 1 WM: [2] NUM CLIENTS: 1 WM: [2] NUM CLIENTS: 1 WM: [2] NUM CLIENTS: 1

In the first call I was so fast that NUM CLIENTS is not shown for the first time. In the second call I never reach *WM: [3] -> connectivitycheck.android.com"

tablatronix commented 5 years ago

event: mismatch or invalid event

776

We have not figured out what this is yet, seems like a esp lib bug

tablatronix commented 5 years ago

esp said that is a harmless error, but it is odd...

you never get to *WM: [3] -> connectivitycheck.android.com"

because your device is not connecting, or failing dns

No idea what would be causing it, I will try to test this, maybe try with startconfigportal, I have used that after autoconnect with no issues

Bjack795 commented 5 years ago

Ok I'll try thank you!

tablatronix commented 5 years ago

can you try a different client device also just to see

Bjack795 commented 5 years ago

I've only my Wemos D32 pro V2

Bjack795 commented 5 years ago

I've tried only the startconfigportal, but the problem is the same. I can see the hotspot, connect to it but then I can't load the options page to connect to a WiFi.

tablatronix commented 5 years ago

I mean with another device, like a computer

Bjack795 commented 5 years ago

Oh excuse me I didn't see "client" word. I'll try from the pc.

tablatronix commented 5 years ago

I am curious to see if this is some kind of dns issue, have you noticed if the ip is changing the second time?

Bjack795 commented 5 years ago

So 1) I tried with my PC and the result is the same of the Android device. 2) The portal IP is never changing.

Your curiosity could be well posed since I had to add a line to your library some weeks ago for another problem. Since when I was restarting the board (even the autorestart after the uploading of a new script) the first time it was giving me error 202 that is AUTH_FAIL and the code was blocked there without continuing.

Then restarting again all worked fine.

So I modified the code inserting an ESP.restart() in correspondence of AUTH_FAIL occurrence and I solved that problem. A friend of mine suggested that it could have been a problem related to the Local IP address (that was always remaining the same) and lease time, since I can't put it as continuos.

In practise my D32 was trying to connect with that IP at first restart-> AUTH_FAIL Then with another restart -> all fine.

So in my opinion this problem could be related to that, but now it's not exiting with AUTH_FAIL since my ESP is not restarting automatically.

Bjack795 commented 5 years ago

PS. The problem happens either if at startup a connection is established or not.

tablatronix commented 5 years ago

I was going to say maybe it is your router, sometimes routers do not let a reconnection so quickly because they think the ip is still connected, but it is probably not that. The test would be to have some delay and call a disconnect between the 2 autoconnects, sounds like maybe the state gets messed up by the first, and a disconnect might fix it WiFi.disconnect(this erased credentials) or a wifi off

Bjack795 commented 5 years ago

It can't be the router since if I disconnect the wifi and I call again autoconnect() it connects to the wifi if it has the credentials. The problem of the double call is on the portal side (that is independent on the router since I can choose my neighbour wifi if I want).

I tried either the WiFi.disconnect(true/false) and the WiFi.mode(WIFI_OFF). Trying autoconnect() or portalconfig() and none of them changed anything.

The portal is working only at startup. The connection to a memorized wifi is always working.

tablatronix commented 5 years ago

ok I will see if I can reproduce, and you have the latest pull from development? And you have a current revision esp32?

Bjack795 commented 5 years ago

I've downloaded it in December maybe I give it a try with the newest one before making you work. Yup I have the current revision esp32.

Bjack795 commented 5 years ago

Ok same problem with the latest pull from development.

tablatronix commented 5 years ago

Where is your wm obj declared, new everytime, or a global ?

tablatronix commented 5 years ago

confirmed, using global wm instance.

[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 3 - STA_STOP
*WM: [2] Enabling AP 
*WM: [1] StartAP with SSID:  OnDemandAP
*WM: [2] AP has anonymous access! 
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 3 - STA_STOP
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 13 - AP_START
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 14 - AP_STOP
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 13 - AP_START
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 13 - AP_START
*WM: [1] AP IP address: 192.168.4.1
*WM: [3] setupConfigPortal 
*WM: [1] Starting Web Portal 
*WM: [3] dns server started with ip:  192.168.4.1
*WM: [2] HTTP server started 
*WM: [2] WiFi Scan ASYNC started 
*WM: [2] Config Portal Running, blocking, waiting for clients...[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 2 - STA_START

*WM: [2] Portal Timeout In 39 seconds
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 1 - SCAN_DONE
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 15 - AP_STACONNECTED
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 17 - AP_STAIPASSIGNED

Last event is staipassigned, I see no client requests either which is odd, I wonder if forgetting the ap will change this, it is as if the client does not ping captive portal cause it thinks it has a good connection, or does not even try.. I am thinking maybe some dhcp session is still the same, not sure how dhcp works, but maybe we can fix somehow

tablatronix commented 5 years ago

now its working fine, hmm

[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 1 - SCAN_DONE
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 15 - AP_STACONNECTED
[D][WiFiGeneric.cpp:342] _eventCallback(): Event: 17 - AP_STAIPASSIGNED
[V][WebServer.cpp:284] handleClient(): New client
[V][Parsing.cpp:113] _parseRequest(): method: GET url: /hotspot-detect.html search: 
[V][Parsing.cpp:219] _parseRequest(): headerName: Host
tablatronix commented 5 years ago

It only happened once, Cannot do it again, works everytime...

/**
 * OnDemandConfigPortal.ino
 * example of running the configPortal AP manually, independantly from the captiveportal
 * trigger pin will start a configPortal AP for 120 seconds then turn it off.
 * 
 */
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

// select which pin will trigger the configuration portal when set to LOW
#define TRIGGER_PIN 0

int timeout = 40; // seconds to run for
WiFiManager wm; // GLOBAL

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("\n Starting");
  pinMode(TRIGGER_PIN, INPUT_PULLUP);
}

void loop() {
  // is configuration portal requested?
  if ( digitalRead(TRIGGER_PIN) == LOW) {
    // WiFiManager wm; // LOCAL

    //reset settings - for testing
    //wifiManager.resetSettings();

    // set configportal timeout
    wm.setConfigPortalTimeout(timeout);

    if (!wm.autoConnect("OnDemandAP")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      // ESP.restart();
    }

    //if you get here you have connected to the WiFi
    // Serial.println("connected...yeey :)");

  }

  // put your main code here, to run repeatedly:
}
Bjack795 commented 5 years ago

My wm is always new but I tried also to define it globally in the past. So your last code is working? I test it and I tell you. Thank you.

tablatronix commented 5 years ago

I think they were both working, I had once time where it just sat there. Not sure what to make of this.

Bjack795 commented 5 years ago

I tried your code with a different "trigger" for the autoconnect()

/**
 * OnDemandConfigPortal.ino
 * example of running the configPortal AP manually, independantly from the captiveportal
 * trigger pin will start a configPortal AP for 120 seconds then turn it off.
 * 
 */
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

// select which pin will trigger the configuration portal when set to LOW

int timeout = 200; // seconds to run for
WiFiManager wm; // GLOBAL

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("\n Starting");
}

void loop() {
  // is configuration portal requested?

  if ( Serial.available()>0 && Serial.read() == 99) //if I insert "c" in the Serial Monitor I reload the autoconnect()
  {
    Serial.println();
    // WiFiManager wm; // LOCAL

    //reset settings - for testing
    //wifiManager.resetSettings();

    // set configportal timeout
    wm.setConfigPortalTimeout(timeout);
    wm.resetSettings();
    if (!wm.autoConnect("OnDemandAP")) {

      Serial.println("failed to connect and hit timeout");
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      // ESP.restart();
    }

    //if you get here you have connected to the WiFi
    // Serial.println("connected...yeey :)");

  }

  // put your main code here, to run repeatedly:
}

The first time I called it, it worked, the second time it didn't: (I've erased the settings to start again the portal, otherwise it would have connected to the saved wifi)

WM: [1] SETTINGS ERASED WM: [3] WiFi station enable WM: [1] AutoConnect WM: [2] ESP32 event handler enabled WM: [2] Connecting as wifi client... WM: [1] STA static IP: WM: [2] setSTAConfig static ip not set WM: [3] WIFI station disconnect WM: [1] No saved credentials, skipping wifi WM: [2] Connection result: WL_NO_SSID_AVAIL WM: [3] lastconxresult: WL_NO_SSID_AVAIL WM: [1] AutoConnect: FAILED WM: [3] WIFI station disconnect WM: [3] WiFi station enable WM: [2] Disabling STA WM: [2] Enabling AP WM: [1] StartAP with SSID: OnDemandAP WM: [2] AP has anonymous access! WM: [1] AP IP address: 192.168.4.1 WM: [3] setupConfigPortal WM: [1] Starting Web Portal WM: [3] dns server started with ip: 192.168.4.1 WM: [2] HTTP server started WM: [2] WiFi Scan ASYNC started WM: [2] Config Portal Running, blocking, waiting for clients... WM: [2] Portal Timeout In 199 seconds E (124306) event: mismatch or invalid event, id=63 E (124306) event: default event handler failed! WM: [2] Portal Timeout In 169 seconds E (164123) event: mismatch or invalid event, id=63 E (164123) event: default event handler failed! dhcps: send_offer>>udp_sendto result 0 WM: [2] Portal Timeout In 139 seconds *WM: [2] Portal Timeout In 109 seconds

tablatronix commented 5 years ago

Can you connect manually? 192.16.4.1?

Bjack795 commented 5 years ago

To the portal IP? What do you mean with "manually"?

tablatronix commented 5 years ago

goto web browser and goto it manually, to see if its just the captive portal that is not working

Bjack795 commented 5 years ago

I've always done like this, I used the captive portal only twice.

tablatronix commented 5 years ago

interesting, can you ping it ?

Bjack795 commented 5 years ago

On the second call of autoconnect(): 4 packages sent 4 received min: 1 ms max: 15 ms average: 6 ms

tablatronix commented 5 years ago

right so its there, and when to goto http://192.168.4.1, you get a timeout ?

Bjack795 commented 5 years ago

I can connect to the portal but the access page is not available and of course then it results in a timeout since I simply can't reach the page.

tablatronix commented 5 years ago

and how are you closing the first autoconnect? letting it timeout, pressing exit, saving?

tablatronix commented 5 years ago

It sounds like the webserver is not setting up correctly the second time.. maybe there is some debugging we can enable, let me check.

Bjack795 commented 5 years ago

It depends, I tried both connecting and saving or leaving it to timeout.

tablatronix commented 5 years ago

And you are using the example plain, not adding any libraries atm.

have you tried a full flash erase ( desepration )

Bjack795 commented 5 years ago

I'm using a plain example (the one posted before). How can I do a full flash erase?

tablatronix commented 5 years ago

you can use esptool , or there is an arduino board menu option, to erase all or wifi

Bjack795 commented 5 years ago

I can't find this option here https://github.com/platformio/platform-espressif32/issues/127 in my IDE, since with esptool I'm scared to damage the board. Where is that arduino IDE option?

tablatronix commented 5 years ago

ohh right you use pio, I do also, in arduino IDE you can erase from the board menu I think

Bjack795 commented 5 years ago

Nope I use Arduino IDE, the link was to show you the option I can't find. Could you please tell me where this option is in the Arduino IDE?

tablatronix commented 5 years ago

I don't use arduino, But it looks like the device menu

Bjack795 commented 5 years ago

In my opinion that option is visible only for some kind of devices, not for the esp32. I will give a try with esptool if I find the correct firmware to be flashed. Anyways the problem is certainly in flash memory since I also had that problem on "even" restarts as I said in a previous comment. So it is something staying there even after a restart and even with a plain sketch.

tablatronix commented 5 years ago

ohh right esp32 uses nvs erase, only know how to do it in code.

tablatronix commented 5 years ago

esptool erase_flash should work for esp32

Bjack795 commented 5 years ago

But is the erase_flash command enough or then I should re-upload the firmware before loading again the sketches? Since erase_flash command is totally wiping out the memory, bootloader included, or not?

tablatronix commented 5 years ago

no bootloader is never erased

Bjack795 commented 5 years ago

I flashed the board with esptool.py --port COM5 erase_flash successful procedure.

I reloaded the sketch and again the same problem with the same outputs:

E (78179) event: mismatch or invalid event, id=63 E (78179) event: default event handler failed! E (87352) event: mismatch or invalid event, id=63 E (87353) event: default event handler failed! dhcps: send_offer>>udp_sendto result 0

Erasing flash memory is not working.