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

Non blocking AP mode #379

Closed
git-tiger opened this issue Jun 28, 2017 · 80 comments
Closed

Non blocking AP mode #379

git-tiger opened this issue Jun 28, 2017 · 80 comments
Labels
enhancement Feature Request
Milestone

Comments

@git-tiger
Copy link

I have a project that requires the ESP8266 to startup in access point mode, but the remaining code is blocked till the access point is configured.
Is there any way to continue with the code, and still have access point available in the background?

@synfinatic
Copy link

I've got the same requirement and from what I can tell, there isn't a way to do this in the current code today.

@tablatronix tablatronix added the enhancement Feature Request label Aug 26, 2017
@ANASOMARY
Copy link

Hi Guys,

Is this feature added? am really in bad need for this.

@tablatronix
Copy link
Collaborator

tablatronix commented Mar 27, 2018

development branch

    //if this is set, portal will be blocking and wait until save or exit,  
    // is false user must manually `process()` to handle config portal
    void          setConfigPortalBlocking(boolean shouldBlock);

example
#379 (comment)

#500

@ANASOMARY
Copy link

ANASOMARY commented Mar 27, 2018

Hi,
Really appreciate your reply.
I did not get the process() part , but if I wanted the code to continue to the loop even after going into AP mode, then should I set setConfigPortalBlocking(false); ?? and in this case I will still get the portal but at the same time the code will continue to the loop function. right?

wifiManager.setAPStaticIPConfig(ip, gateway, subnet);
wifiManager.setAPCallback(configModeCallback);
setConfigPortalBlocking(false);
wifiManager.setSTAStaticIPConfig(lip, lgateway, lsubnet);
if (mdns.begin("STR_Master8004", WiFi.softAPIP()));
if (!wifiManager.autoConnect(const_cast<char*>(mySsid.c_str()), const_cast<char*>(myPassword.c_str()))) {
Serial.println("failed to connect and hit timeout");
connectionFailed = 1;
ESP.reset();
delay(1000);
}

@tablatronix
Copy link
Collaborator

tablatronix commented Mar 27, 2018

yes and process() in your loop

@ANASOMARY
Copy link

void loop()
{
process();
HandleSwitch();
server.handleClient();
delay(10);
}

Thats it??? Oh god thanks Shawn, you really saved my @$$. 💯

@tablatronix
Copy link
Collaborator

Yeah I mean its development, and hardly tested...

@ANASOMARY
Copy link

Oh snap
image

@tablatronix tablatronix added this to the dev milestone Mar 27, 2018
@tablatronix
Copy link
Collaborator

#500

@ANASOMARY
Copy link

Thanks Shawn,

does it support generic esp8266? am using a sonoff, and am getting the following when I compile:
image

@tablatronix
Copy link
Collaborator

tablatronix commented Mar 27, 2018

It should, what board?

@ANASOMARY
Copy link

Sonoff Basic

@tablatronix
Copy link
Collaborator

I mean what esp board?

@ANASOMARY
Copy link

esp8266

@tablatronix
Copy link
Collaborator

tablatronix commented Mar 27, 2018

esp8266 which one?
esp01?
esp12e?

@ANASOMARY
Copy link

ESP8266 V4.1

@ANASOMARY
Copy link

Hi Shawn,
my mistake, instead of Process() I should have put wifiManager.process(); same for setConfigPortalBlocking
It is compiling with no errors now.
Thanks again.

@ANASOMARY
Copy link

Am Sorry Shawn, But I was using the wrong library, am still getting the same compilation error for generic esp8266
image

@ANASOMARY
Copy link

ANASOMARY commented Mar 27, 2018

This is strange... I tried compiling for a different board, and am getting the same error!
image

Update: it is not compiling for any board now. Please help!

@tablatronix
Copy link
Collaborator

You are gonna have to turn on verbose compilation warnings or whatever adruino has to show some actual info

@tablatronix
Copy link
Collaborator

#524

@hassasian
Copy link

@ANASOMARY you should update your board software from board manager. You should use 2.4.1 or newer one.

@hassasian
Copy link

@tablatronix thx for ur help. But I have one more question. Our esp connected to an router. After that I close the router. Then as suppose our esp come back to AP mode. When I connected ap of esp, it cannot handle http requests. Sorry for bad eng.

@tablatronix
Copy link
Collaborator

not sure i understand

@hassasian
Copy link

hassasian commented Apr 19, 2018

Give me one more shot to explain it :) I have a esp8266 module. I'm using wifimanager development branch for nonblocking ap mode. its working great. Congrats. My problem is that ; when esp cannot access the router which esp connected before it come back to access mode as expected. And I connect the AutoConnectAP with my computer to configure my esp for another router. But esp cannot answer my http request.

@hassasian
Copy link

in addition if you reset your router which esp connected. Esp disconnected from router and enabling its ap. After your router started. Esp connect the router succesfully but esp dont close its ap. Briefly if you restart your router, your esp will enable its ap mode. After your router come back esp will connect it but dont close its ap.

@tablatronix
Copy link
Collaborator

Yeah the ap stays running, the portal only runs if you reboot.
The ap is not supposed to stay running, thats probably a bug

@hassasian
Copy link

Hmm so that means if our router stop working we need to restart our esp physically? Or can we understand that esp disconnected from router. For restarting programmatically.

@tablatronix
Copy link
Collaborator

I will test some more

@tablatronix
Copy link
Collaborator

I added some overflow fixes try now

@vaz83
Copy link

vaz83 commented Jul 14, 2018

Will try now and let you know.

@vaz83
Copy link

vaz83 commented Jul 14, 2018

Hi tablatronix, i've just tested, not working, now, even the parameters are not showing up, this is my code:

#include <FS.h>
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <ArduinoJson.h>

WiFiManager wifiManager;
WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;
char mqtt_server[40] = "0.0.0.0";
char mqtt_topicIN[80] ="IOT_Device/IN";
char mqtt_topicOUT[80] ="IOT_Device/OUT";
char mqtt_port[6]  = "1883";

/*
void handleRoot() {
  wifiManager.startWebPortal();
}

void handleReset() {
  server.send(200, "text/plain", "Resetting!");
    //Serial.println("diconnecting client and wifi");
    //client.disconnect();
    wifi_station_disconnect();

  WiFiManager wifiManager;
  wifiManager.resetSettings();
  ESP.reset();
  
}

void handleNotFound(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}
*/

void setup()
{
  Serial.begin(115200);
  Serial.println("Setup mode...");
  //wifiManager.resetSettings();
   gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP & event)
  {
    Serial.print("Station connected, IP: ");
    Serial.println(WiFi.localIP());
  });

   disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected & event)
  {
    Serial.println("Station disconnected!");
  });
  WiFiManagerParameter custom_text1("<p><b>MQTT Settings</b></p>");
  WiFiManagerParameter custom_mqtt_server("server", "Server IP", mqtt_server, 40);
  WiFiManagerParameter custom_mqtt_port("port", "Port", mqtt_port, 6);
  WiFiManagerParameter custom_mqtt_topicIN("topicIn", "Input Topic", mqtt_topicIN, 80);
  WiFiManagerParameter custom_mqtt_topicOUT("topicOut", "Output Topic", mqtt_topicOUT, 80);
  wifiManager.addParameter(&custom_text1);
  wifiManager.addParameter(&custom_mqtt_server);
  wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_mqtt_topicIN);
  wifiManager.addParameter(&custom_mqtt_topicOUT);

  wifiManager.setConfigPortalBlocking(false);
  
  wifiManager.autoConnect("IOT Device");

  
  
  /*
  server.on("/", handleRoot);

  server.on("/inline", []()
  {
    server.send(200, "text/plain", "this works as well");
  });
  server.on("/resetwifi", handleReset);
  server.onNotFound(handleNotFound);
  server.begin();
  */
  Serial.println("HTTP server started");
}



void loop()
{
  wifiManager.process();
 
  Serial.println("hello world");
  // put your main code here, to run repeatedly:
}

Now, even if you take the setConfigPortalBlocking, the parameters are no longer showing up.

In the serial I got this:

*WM: <- Request redirected to captive portal
*WM: <- HTTP Root
*WM: Scan is cached
*WM: <- HTTP Wifi
*WM: Scan is cached
*WM: 1 networks found
*WM: AP: -35 V@Z Wifi
*WM: [ERROR] WiFiManagerParameter is out of scope
*WM: <- Request redirected to captive portal
*WM: <- HTTP Root
*WM: Scan is cached

@tablatronix
Copy link
Collaborator

tablatronix commented Jul 14, 2018

*WM: [ERROR] WiFiManagerParameter is out of scope

There you go

Your wifiparameter objects need to be global or in scope, once setup exits, the references are broken.

If anyone has better ideas how to do this, I am open to it.

You can make your parameters static, or define them outside of setup

@vaz83
Copy link

vaz83 commented Jul 14, 2018

Hi Tablatronix,
I have done what you said (put the variabes global at the beggining), the error still persists. Like I said, even if I comment the setConfigPortalBlocking(false), no parameters are shown and error *WM: [ERROR] WiFiManagerParameter is out of scope is shown. with the version of WM i had from yesterday it was working fine.

@tablatronix
Copy link
Collaborator

works for me

#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

WiFiManager wm;
  WiFiManagerParameter custom_mqtt_server("server", "mqtt server", "servername", 40);
  WiFiManagerParameter custom_mqtt_port("port", "mqtt port", "1234", 6);

void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    delay(3000);

    //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around

    // add a custom input field

  WiFiManagerParameter custom_field("customfield", "Custom Field", "Default Value", 40);
  WiFiManagerParameter custom_token("api_token", "api token", "0123456789", 9); // overflow
  // WiFiManagerParameter custom_tokenb("invalid token", "invalid token", "abcdefghijk", 11);

  //add all your parameters here
  wm.addParameter(&custom_mqtt_server);
  wm.addParameter(&custom_mqtt_port);
  wm.addParameter(&custom_field);
  wm.addParameter(&custom_token);
  // wm.addParameter(&custom_tokenb); // overflow

    //reset settings - wipe credentials for testing
    //wm.resetSettings();

    wm.setConfigPortalBlocking(false);
    if(wm.autoConnect("AutoConnectAP")){
        //if you get here you have connected to the WiFi
        Serial.println("connected...yeey :)");
    }
    else 
    {
        Serial.println("non blocking config portal running");
    }

    wm.startConfigPortal("OnDemandAP");
}

void loop() {
    wm.process();
    // put your main code here, to run repeatedly:
    
}

@vaz83
Copy link

vaz83 commented Jul 14, 2018

this is giving *WM: [ERROR] WiFiManagerParameter is out of scope .. Am I doing something wrong?

#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic

WiFiManager wm;
char mqtt_server[40] = "0.0.0.0";
char mqtt_topicIN[80] ="IOT_Device/IN";
char mqtt_topicOUT[80] ="IOT_Device/OUT";
char mqtt_port[6]  = "1883";

WiFiManagerParameter custom_mqtt_server("server", "Server IP", mqtt_server, 40);
WiFiManagerParameter custom_mqtt_port("port", "Port", mqtt_port, 6);
WiFiManagerParameter custom_mqtt_topicIN("topicIn", "Input Topic", mqtt_topicIN, 80);
WiFiManagerParameter custom_mqtt_topicOUT("topicOut", "Output Topic", mqtt_topicOUT, 80);

void setup()
{
  Serial.begin(115200);
  delay(3000);
  Serial.println("Setup mode...");
  //wifiManager.resetSettings();
 
  wm.addParameter(&custom_mqtt_server);
  wm.addParameter(&custom_mqtt_port);
  wm.addParameter(&custom_mqtt_topicIN);
  wm.addParameter(&custom_mqtt_topicOUT);

  wm.setConfigPortalBlocking(false);

  if(wm.autoConnect("IOT_Device"))
  {
    //if you get here you have connected to the WiFi
    Serial.println("connected...yeey :)");
  }
  else 
  {
    Serial.println("non blocking config portal running");
  }
  wm.startConfigPortal("IOT_Device"); 
  
}

void loop()
{
  wm.process();
 
  //Serial.println("hello world");
  // put your main code here, to run repeatedly:
}

@tablatronix
Copy link
Collaborator

testing

@tablatronix
Copy link
Collaborator

I dont see it

screen shot 2018-07-14 at 6 12 46 pm

@tablatronix
Copy link
Collaborator

I am on esp32, should I test on 8266 also ?

@vaz83
Copy link

vaz83 commented Jul 14, 2018

Can you please try on the 8266? Is the one I am using

@tablatronix
Copy link
Collaborator

Will do

@vaz83
Copy link

vaz83 commented Jul 15, 2018

Hi tablatronix, were you able to test it on the esp8266 ? Parameters are not showing up :(

@tablatronix
Copy link
Collaborator

Yeah works fine there as well.

@vaz83
Copy link

vaz83 commented Jul 16, 2018

It's working only with the version I had from last Friday, when I download the newer version it doesn't work

@tablatronix
Copy link
Collaborator

This was just patched this morning, there was a bug if you were not setting a default value, maybe that was causing an issue.

@xserg
Copy link

xserg commented Aug 19, 2018

Hi!
Thank you much for patching, it works fine now. Can you pls check setConfigPortalTimeout with setConfigPortalBlocking(false);
The issue when config portal never disconnected.
In case the router switched off, and esp rebooted in this time, config portal will start and never disconnected after time out and router up. (esp8266 12)
Thanks

@tablatronix
Copy link
Collaborator

tablatronix commented Aug 19, 2018

setConfigPortalTimeout is not implemented when configPortal is non blocking, because you are in control of it, and can stop it when you want

@xserg
Copy link

xserg commented Sep 24, 2018

Thank you much for work, It seems in the example, onDemandNonBlocking, setConfigPortalBlocking(false); missing...

@xserg
Copy link

xserg commented Oct 3, 2018

Hi @tablatronix, how to setup ap name in noblocking without autoconnect? tnx

@tablatronix
Copy link
Collaborator

tablatronix commented Oct 3, 2018

You mean on demand?
Same arguments to startconfogportal as autoconnect

@tablatronix
Copy link
Collaborator

can this be closed?

@vaz83
Copy link

vaz83 commented Jan 20, 2019

Yes

@faizakbar18
Copy link

I try using this code,
#500
and get error like this:

Screenshot_1

how to fix it?

NodeMCU v3

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

No branches or pull requests

10 participants