Search code examples
arduinoandroid-wifiesp8266

I am unable to connect successfully to 8266 wifi module using arduino


Hi I am new to arduino programming and I am have an issue. I have successfully managed to display the wifi using esp8266 module i.e when I run my code the esp8266 module creates a wifi. It also asks for password but after that there is no output of successful connection. I am using the method wifi.softAp(username,password) for creation of wifi network. I have written the following code:

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

const char* ssid = "Jeet";//Wifi username
const char* password = "wifibin12"; //Wifi password

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/plain", "<h1>hello from esp8266!</h1>");
}

void setup(void) {
  // put your setup code here, to run once:

Serial.begin(115200);
//WiFi.mode(WIFI_AP);
Serial.print("this is my pass");
Serial.print(password);
WiFi.softAP(ssid, password);
Serial.print("Setting soft-Ap ... ");
// Wait for connection

while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }


//If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP

  server.on("/", handleRoot);      //Which routine to handle at root location

  server.begin();                  //Start server
  Serial.println("HTTP server started");

}

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

When I run the code I get ............. output continuously on serial monitor. Kindly help me fix this issue if anyone knows what I am doing wrong. Suggestions would also be appreciated.


Solution

  • It's getting stuck to while loop. Wifi.status() returns WL_CONNECTED when it is connected to wifi network (to another Access Point). So if you want just get AP to work you should try this:

    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    
    const char* ssid = "Jeet";            //Wifi username
    const char* password = "wifibin12";  //Wifi password
    
    ESP8266WebServer server(80);
    
    void handleRoot() {
      server.send(200, "text/plain", "<h1>hello from esp8266!</h1>");
    }
    
    void setup(void) {
      // put your setup code here, to run once:
      Serial.begin(115200);
      Serial.print("this is my pass");
      Serial.print(password);
      WiFi.softAP(ssid, password);
      Serial.print("Setting soft-Ap ... ");
      // Wait for connection
    
    //If connection successful show IP address in serial monitor
      Serial.println("");
      Serial.print("AP name ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());  //IP address assigned to your ESP
    
      server.on("/", handleRoot);      //Which routine to handle at root location
    
      server.begin();                  //Start server
      Serial.println("HTTP server started");
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      server.handleClient();
    }
    

    And WiFi.localIP() doesn't return AP's local ip. Default is 192.168.4.1.

    I recommend looking docs and examples from here: https://github.com/esp8266/Arduino/tree/master/doc/esp8266wifi