Juerd / ESP-WiFiSettings

WiFi Manager for the ESP32 Arduino environment
Other
168 stars 35 forks source link

Cannot connect to soft AP after .portal() is called #29

Closed ameerhazo closed 1 year ago

ameerhazo commented 1 year ago

Hello, I'd like to be able to reconfigure some parameters on the portal even after initial configuration by having the portal brought up again after a button press. The button press function to open the portal works but for some reason, I'm not able to open the portal after successfully connecting to the soft AP. The .portal() function was called in the loop function when the button is pressed for 3 seconds. Thanks in advance for the help!

Regards, Ameer

ameerhazo commented 1 year ago

Hi @Juerd, could you please help me with my problem here :D Would be greatly appreciated.

Juerd commented 1 year ago

Without much more information, it's almost impossible to tell if you're doing something wrong, or if this is a bug.

ameerhazo commented 1 year ago

Hello @Juerd, thanks for the fast reply. In the API reference section, you wrote,

void portal();

Normally, this function is called by .connect(). To allow reconfiguration after the initial configuration, you could call .portal() manually, for example when a button is pressed during startup.

I wish it would work like this. Unfortunately, when this function is called, the portal AP is opened and I'm able to connect to the AP but accessing the AP on the browser gives me a HTTP 500 error.

Juerd commented 1 year ago

void portal(); Normally, this function is called by .connect(). To allow reconfiguration after the initial configuration, you could call .portal() manually, for example when a button is pressed during startup. I wish it would work like this. Unfortunately, when this function is called, the portal AP is opened and I'm able to connect to the AP but accessing the AP on the browser gives me a HTTP 500 error.

It works for me, e.g. in https://github.com/revspace/operame/blob/main/operame.ino#L182

I don't know why it doesn't work in your case, and I can't debug remotely.

ameerhazo commented 1 year ago

Hi @Juerd thanks for the code example, I'll try it out on my end to see if it works for me. Thank you and have a nice evening :D

ameerhazo commented 1 year ago

Hi @Juerd, so I've been trying to figure out why WiFiSettings.portal() is not working for me for my code usage. The code is as follows:

` // display device info on config page WiFiSettings.hostname = device_ID; WiFiSettings.heading("Device ID:"); WiFiSettings.heading(device_ID);

// Define custom settings saved by WifiSettings
// MQTT configuration variable to be shown on config UI provided by the WiFi Settings library
bool checkbox_mqtt_auth_dummy = WiFiSettings.checkbox("MQTT broker authentification");
String mqtt_brokeraddress_dummy = WiFiSettings.string("MQTT broker address", "mqtt.eclipse.org");
String mqtt_brokerauth_user = WiFiSettings.string("Username", "username");
String mqtt_brokerauth_pass = WiFiSettings.string("Password", "password");

// try to connect to WiFi with a timeout of 30 seconds - launch portal if connection fails
WiFiSettings.connect(true, -1);

// MQTT config
checkbox_mqtt_auth = checkbox_mqtt_auth_dummy;
Mqttbrokeraddress = mqtt_brokeraddress_dummy;
Mqtt_auth_username = mqtt_brokerauth_user;
Mqtt_auth_password = mqtt_brokerauth_pass;
//

// if user wants MQTT to be used, set the MQTT Broker, port number and the MQTT Client

MQTT_Client.setServer(Mqttbrokeraddress.c_str(), 1883);
MQTT_Client.setClient(hagerClient);

int retries = 0;

while (retries < 5)
{
    retries++;

    if (checkbox_mqtt_auth) // mqtt broker needs authentification data to connect
    {
        connected_tobroker = MQTT_Client.connect(device_ID, Mqtt_auth_username.c_str(), Mqtt_auth_password.c_str(), lwt_Topic.c_str(), 1, true, lwt_message.c_str());
    }
    else // mqtt broker doesnt need authentication to connect
    {
        connected_tobroker = MQTT_Client.connect(device_ID, lwt_Topic.c_str(), 1, true, lwt_message.c_str());
    }

    if (connected_tobroker)
    {
        Serial.println("Connected to MQTT Broker."); // successfully connected to broker

        break;
    }
    else
    {
        Serial.println(retries);
        Serial.println("Connecting to MQTT Broker failed... ");
        Serial.print("failed, MQTT connection status =");
        Serial.print(MQTT_Client.state());
        Serial.println(" try again in 5 seconds");
        // Wait 5 seconds before retrying
        delay(5000);
    }
}

if(!connected_tobroker) // if number of MQTT reconnection attempts exceeds the maximum retry limit, open the portal
{
    Serial.println("Retries exceeded, restarting device");
    WiFiSettings.portal(); // this doesnt work for some reason
}`

So the gist of the code is that I want the portal to be restarted again if the MQTT connection to the broker is not successful after a few tries. (User may have given wrong username or password for the broker connection on the portal or the MQTT broker is down). This is to ensure that I'm able to prevent the device from going into an infinite loop not doing anything and gives the user the option to fix the mistake.

The bug I found is that when I called the WiFiSettings.portal() method, I can see that the portal is up. I'm also able to connect to the soft AP (takes longer for some reason) but I'm unable to access the portal (HTTP ERROR 500). I also noticed that I cant see the debugging output in the serial monitor that usually shows up when I'm connected to device (eg. 13 WiFi Networks found).

It would be greatly appreciated if you could help me debug this because this library is really quite practical :D and I thank you again for your work.

Regards, Ameer

Juerd commented 1 year ago

I don't know what's wrong.