After If statement is true dont check anymore

Im using esp8266 to create a wifi and after i enter my house wifi credentials i want to make esp work as client
this is what i got so far:

//====================================
//ESP8266 Access Point Control of LED
//====================================
#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
//
  String net_pswd = "";
  String net_ssid = "";
  bool wifiReady = false;

//--------------------------------------

const char* ssid = "ESP8266_AP";

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

////////////////////////ACCESS POINT COMMANDS////////////////////////////////////
void handleRoot()
{
  server.send(200, "text/html", "haloooooooocaune");
}
void setssid()
{
  if (server.hasArg("set")) { 
    net_ssid = server.arg("set");
    Serial.print("net ssid ");
    Serial.println(net_ssid);
    
    server.send(200, "text/html", "ssid set");

    
  }
}
void setpswd()
{
  if (server.hasArg("set")) { 
      net_pswd = server.arg("set");

    Serial.print("net pswd ");
    Serial.println(net_pswd);
    server.send(200, "text/html", "password set");

    
  }
  
}
////////////////////////////////////////////////
////////////////CLIENT COMMANDS///////////////////////////////
int premenna;
void test() {
  if (server.hasArg("set")) { // this is the variable sent from the client
    int readingInt = server.arg("set").toInt();
    premenna = readingInt;
   
    Serial.print("Premennna: ");
    Serial.println(premenna);
    server.send(200, "text/html", "premenna set");
  }
}
void rootPage() { 
  server.send(200, "text/plain", "FUNGUJ !"); 
}

//////////////////////////////////////////////////////////////

  

void setup()
{

//access point begin
  Serial.begin(115200);
  WiFi.mode(WIFI_AP_STA);
   WiFi.softAP(ssid);
  IPAddress myIP = WiFi.softAPIP();
  delay(5000);
  Serial.print("AP IP address: ");
  Serial.println(myIP);

 
  server.on("/", handleRoot);
  server.on("/ssid/", HTTP_GET, setssid);
  server.on("/pswd/", HTTP_GET, setpswd);
  
  
 server.begin();


  
 
   //Begin WiFi
  if(wifiReady){
   while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(100); }
  WiFi.begin(net_ssid, net_pswd);
 
  // WiFi Connected
  Serial.print("Connected! SSID: ");
  Serial.println(net_ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}
  
  
  

  // Start Web Server
  server.on("/", rootPage);
 
  /////////////commands//////////////////////
  server.on("/test/", HTTP_GET, test);
  
  /////////////commands end//////////////////////////////
 
    
  server.begin();
 
}
//===============================================
void loop(){


   server.handleClient();
  if(net_ssid != "" && net_pswd != "")
  {
    wifiReady = true;
  }
   

   delay(1000);

}

im guessing that it doesnt work because by the time the wifiReady = true the setup function allready run and im unable to run it again.

Any ideas as to how to make this are welcome

in the setup() you need to run your server.handleClient(); in a loop so that you get an opportunity to enter the information.

while (!wifiReady) server.handleClient();

Once you get the information, you need to terminate that server, switch WiFi mode and if the connection was successful then you can go the loop


the alternative is to use WiFiManager - Arduino Reference which makes all this easy

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.