ESP running AP mode without the use loop()

Hi,

I need to program an ESP8266 to display AP MODE when a button is pressed at boot. When in AP MODE, store the data (custom IPs, passwords, etc.) into an eprom and then boot into Station mode.

For some reasons, I cannot use Wifi Manager library as it interferes with my eprom.

The following code does that but the AP settings web page is served in the loop() function.

Is there a way to serve the AP settings form before reaching the loop function so that the latter can be fully dedicated to the Station web page? Or doesn’t really matter as it doesn’t affect the performance?

TIA

#include <ESP8266WiFi.h>
#include <EEPROM.h>

const char APssid[] = "ESP8266_";
const char APpass[] = "esp00000";

//from eeprom
const char myssid[] = "";
const char mypass[] = "";

const int ledPin = 2; // onboard, green LED
const int triggerPin = 4; // Digital pin to be read

bool APmode = false;  //flagged when button is pressed
bool STAmode = true;

WiFiServer server(80);

void setup()
{
  Serial.begin(115200);
  delay(10);
  Serial.println("");

  pinMode(triggerPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);  //keeps LED off

  //if button pressed during power up, go into AP MODE
  if (digitalRead(triggerPin) == LOW) {
    Serial.println("Button pressed");
    Serial.println("");
    STAmode = false; //we want to display AP web
    APmode = true;
    setupWiFi();
  }

  //*****
  //***** read custom settings from eeprom ***
  //*****

  //boot into Station mode only is the button has not been pressed
  if (STAmode) {
    WiFi.mode(WIFI_STA);
    WiFi.begin("myssid", "mypass"); //Connect to your WiFi router
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

    Serial.println("");
    Serial.print("WiFi connected. ");
    Serial.print("Local IP address: ");
    Serial.println(WiFi.localIP());
    Serial.println("");
  }

  Serial.println("Starting server");
  server.begin();
}

void loop()
{
  if (APmode) APweb();
  else if (STAmode) STAweb();
}

void APweb() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');

  int addr_start = req.indexOf(' ');
  int addr_end = req.indexOf(' ', addr_start + 1);

  req = req.substring(addr_start + 1, addr_end);

  Serial.print("req string: ");
  Serial.println(req);
  Serial.println("");

  client.flush();

  if (req.startsWith("/?sBtn=")) {
    Serial.println("Data submitted");
    Serial.println("");

    //do things here


  }

  //do data processing here based on string reg

  client.flush();

  // Prepare the response. Start with the common header:

  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "<head>";
  s += "<title>ESP MANAGER</title>";
  s += "<h1>Hell world! I'm in AP MODE</h1>";
  s += "</head>";
  s += "<body>";
  s += "<form method='get' action='/'>";
  s += "<button id='btn' type='submit' name='sBtn' value='btn'>Submit";
  s += "</button>";
  s += "</form>";
  s += "</body>";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);

  Serial.println("Client disconnected");
  Serial.println("");

  //*****
  //***** write custom settings to eeprom ***
  //*****
}

void STAweb() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');

  Serial.print("req string: ");
  Serial.println(req);
  Serial.println("");

  client.flush();


  //do data process here based on string reg

  client.flush();

  // Prepare the response. Start with the common header:
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "<h1>Hello world! I'm in STA MODE</h1>";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);

  Serial.println("Client disonnected");
  Serial.println("");

  //*****
  //***** write web data to eeprom ***
  //*****
}

void setupWiFi()
{
  WiFi.mode(WIFI_AP);
  WiFi.softAP(APssid, APpass);
}

You probably loose a few nanoseconds per loop doing that if (APmode) test... not a big deal compared to the cost of looping. I would not worry about it at all.