ESP8266: Problem compiling sketch that boots in AP or in STA and use EEPROM

Hello, specialist friends!

I have a sketch that allows you to connect in two modes: AP mode to configure the network in which the ESP8266 will connect later in STA mode. The password and the name of the network that the user enters in the AP mode are stored in the EEPROM, so that if I do a reset, the sketch boots in STA mode accessing the network stored in the AP mode. The program works perfect. However, when in the LOOP if I add "WiFiClient client = server.available ();" to read wifi data, the compiler throws the error "'class ESP8266WebServer' has no member named 'available'", that is, it does not recognize the WiFi library.
Could you help me in telling me where the error is?

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

//-------------------VARIABLES GLOBALES--------------------------
int contconexion = 0;
unsigned long previousMillis = 0;

char ssid[50];
char pass[50];

const char *ssidConf = "tutorial";
const char *passConf = "12345678";

String mensaje = "";

//-----------CODIGO HTML PAGINA DE CONFIGURACION---------------
String pagina = ""
""
""
"Tutorial Eeprom"
""
""
""
""
""



"

"
""
"ESCANEAR

";

String paginafin = ""
"";

//------------------------SETUP WIFI-----------------------------
void setup_wifi() {
// Conexión WIFI
WiFi.mode(WIFI_STA); //para que no inicie el SoftAP en el modo normal
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED and contconexion <50) {
++contconexion;
delay(250);
Serial.print(".");
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
}
if (contconexion <50) {
Serial.println("");
Serial.println("WiFi conectado");
Serial.println(WiFi.localIP());
digitalWrite(13, HIGH);
}
else {
Serial.println("");
Serial.println("Error de conexion");
digitalWrite(13, LOW);
}
}

//--------------------------------------------------------------
WiFiClient espClient;
ESP8266WebServer server(80);
//--------------------------------------------------------------

//-------------------PAGINA DE CONFIGURACION--------------------
void paginaconf() {
server.send(200, "text/html", pagina + mensaje + paginafin);
}

//--------------------AP MODE for config de WiFi------------------------
void modoconf() {

delay(100);
digitalWrite(13, HIGH);
delay(100);



WiFi.softAP(ssidConf, passConf);
IPAddress myIP = WiFi.softAPIP();
Serial.print("IP del acces point: ");
Serial.println(myIP);
Serial.println("WebServer iniciado...");

server.on("/", paginaconf); //esta es la pagina de configuracion
server.on("/guardar_conf", guardar_conf); //Graba en la eeprom la configuracion
server.on("/escanear", escanear); //Escanean las redes wifi disponibles
server.begin();

while (true) {
server.handleClient();
}
}

//---------------------PREPARING THE CONFIGURATION TO SAVE IN EEPROM-------------------------
void guardar_conf() {

Serial.println(server.arg("ssid"));//Recibimos los valores que envia por GET el formulario web
grabar(0,server.arg("ssid"));
Serial.println(server.arg("pass"));
grabar(50,server.arg("pass"));

mensaje = "Configuracion Guardada...";
paginaconf();
}

//----------------STORING EEPROM-------------------
void grabar(int addr, String a) {
int tamano = a.length();
char inchar[50];
a.toCharArray(inchar, tamano+1);
for (int i = 0; i < tamano; i++) {
EEPROM.write(addr+i, inchar*);*

  • }*

  • for (int i = tamano; i < 50; i++) {*

  • EEPROM.write(addr+i, 255);*

  • }*

  • EEPROM.commit();*
    }
    //-----------------READ FROM EEPROM------------------------
    String leer(int addr) {

  • byte lectura;*

  • String strlectura;*

  • for (int i = addr; i < addr+50; i++) {*

  • lectura = EEPROM.read(i);*

  • if (lectura != 255) {*

  • strlectura += (char)lectura;*

  • }*

  • }*

  • return strlectura;*
    }
    //---------------------------SCAN WiFi Nets to choose in AP MODE----------------------------
    *void escanear() { *

  • int n = WiFi.scanNetworks(); //devuelve el número de redes encontradas*

  • Serial.println("escaneo terminado");*

  • if (n == 0) { //si no encuentra ninguna red*

  • Serial.println("no se encontraron redes");*

  • mensaje = "no se encontraron redes";*

  • } *

  • else*

  • {*

  • Serial.print(n);*

  • Serial.println(" redes encontradas");*

  • mensaje = "";*

  • for (int i = 0; i < n; ++i)*

  • {*

  • // agrega al STRING "mensaje" la información de las redes encontradas*

  • mensaje = (mensaje) + "

    " + String(i + 1) + ": " + WiFi.SSID(i) + " (" + WiFi.RSSI(i) + ") Ch: " + WiFi.channel(i) + " Enc: " + WiFi.encryptionType(i) + "

    \r\n";*

  • //WiFi.encryptionType 5:WEP 2:WPA/PSK 4:WPA2/PSK 7:open network 8:WPA/WPA2/PSK*

  • delay(10);*

  • }*

  • Serial.println(mensaje);*

  • paginaconf();*

  • }*
    }
    //------------------------SETUP-----------------------------
    void setup() {

  • pinMode(13, OUTPUT); // D7*

  • // Inicia Serial*

  • Serial.begin(115200);*

  • Serial.println("");*

  • EEPROM.begin(512);*

  • pinMode(14, INPUT); //D5*

  • if (digitalRead(14) == 0) { // when de boton is pressed, it boots in AP*

  • modoconf();*

  • }*

  • leer(0).toCharArray(ssid, 50);*

  • leer(50).toCharArray(pass, 50);*
    // if boton not pressed, it boots in STA mode

  • setup_wifi();*
    }
    //--------------------------LOOP--------------------------------
    void loop() {

  • // Here appears the error when I compile*

  • WiFiClient client = server.available();*
    _ // **************************************************_

  • //*

  • //*

  • unsigned long currentMillis = millis();*

  • if (currentMillis - previousMillis >= 5000) { //envia la temperatura cada 5 segundos*

  • previousMillis = currentMillis;*

  • Serial.println("Funcionado...");*

  • }*
    }
    Thank you very much for your help!!!!!!!!!!!
    Pedro

you try to use WiFiWebServer as WiFiServer is used. see the examples of the ESP8266WebServer library.

you don't need to save the ssid and password to eeprom emulation. the esp8266 saves them after WiFi.begin(ssid, password) and next time if connects without WiFi.begin. so after the user entered the data cal WiFi.begin with them and that is all. next time in setup() only test if it connects with WiFi.waitForConnectResult();. if not, start AP. see the WiFiManager library

Hi Juraj,
Thank you very much for your good advice. It was the obvious solution (I did not know the WiFiManager library). I was able to save more than 200 lines of code with an elegant and efficient solution.
Really thank you very much!!!

Regards,
Pedro :slight_smile: