I've got an ESP-07s and I used an example code from the internet. When I power it and the program starts, it should just connect to my Wifi network. It has a simple webpage that should be locally accessible in my Wifi network.

I'll start by giving the code:

// Load Wi-Fi library
#include <ESP8266WiFi.h>

const char* ssid     = "MyWifiNetworkName";
const char* password = "MyWifiPassword";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output16State = "off";
String output0State = "off";

// Assign output variables to GPIO pins
const int output16 = 16;
const int output0 = 0;

unsigned long currentTime = millis();
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

void setup() {
  // Initialize the output variables as outputs
  pinMode(output16, OUTPUT);
  pinMode(output0, OUTPUT);
  pinMode(2, OUTPUT);
  // Set outputs to LOW
  digitalWrite(output16, LOW);
  digitalWrite(output0, LOW);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  bool ledOn = true;
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(2, ledOn ? HIGH : LOW);
    ledOn = ! ledOn;
  // Print local IP address and start web server
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");

void loop(){
  WiFiClient client = server.accept();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Connection: close");
            // turns the GPIOs on and off
            if (header.indexOf("GET /16/on") >= 0) {
              Serial.println("GPIO 16 on");
              output16State = "on";
              digitalWrite(output16, HIGH);
            } else if (header.indexOf("GET /16/off") >= 0) {
              Serial.println("GPIO 16 off");
              output16State = "off";
              digitalWrite(output16, LOW);
            } else if (header.indexOf("GET /0/on") >= 0) {
              Serial.println("GPIO 0 on");
              output0State = "on";
              digitalWrite(output0, HIGH);
            } else if (header.indexOf("GET /0/off") >= 0) {
              Serial.println("GPIO 0 off");
              output0State = "off";
              digitalWrite(output0, LOW);
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons 
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");
            // Web Page Heading
            // Display current state, and ON/OFF buttons for GPIO 5  
            client.println("<p>GPIO 16 - Status " + output16State + "</p>");
            // If the output5State is off, it displays the ON button       
            if (output16State=="off") {
              client.println("<p><a href=\"/16/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/16/off\"><button class=\"button button2\">OFF</button></a></p>");
            // Display current state, and ON/OFF buttons for GPIO 4  
            client.println("<p>GPIO 0 - Status " + output0State + "</p>");
            // If the output4State is off, it displays the ON button       
            if (output0State=="off") {
              client.println("<p><a href=\"/0/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/0/off\"><button class=\"button button2\">OFF</button></a></p>");
            // The HTTP response ends with another blank line
            // Break out of the while loop
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
    // Clear the header variable
    header = "";
    // Close the connection
    Serial.println("Client disconnected.");

When I power it, one of the following cases happen:

Case 1

It just connects within, say 3 seconds. This is the desired case. It prints its IP address to the serial output, and when I navigate to its IP, the webpage loads and everything works. Of course, this is as how it should be.

Case 2

It keeps "trying" to connect, and after 1 ~ 2 minutes it is connected, but then the following weird thing happens. It seems to be connected, and when I look in the list of devices that are connected to my router (via router's web interface) it shows the ESP is connected, and has the IP enter image description here As shown in my code, the ESP also prints its IP to the serial output as soon as it is connected. In the normal situation (case 1) this IP starts with 192.168.x.x, and this is expected, since all devices shown in the connected devices list have an IP that starts with the same range. But in this case, the IP shown in the serial output is something like "". Also, the ESP's webpage is not accessible. So: router shows ESP as connected, having IP, while the ESP itself prints a totally different IP ( The webpage is at none of the given IP's acessible.

Case 3

It just doesn't connect. But I guess this could also be case 2 but then with an indefinitely "trying time" that I haven't waited.

  • Router settings have never been changed. Everything is kept default as it was when we got it from our internet provider.
  • I read on the internet an ESP can give problems when the WIfi channel 13 / 14 are used. The router has set this to automatically choose a channel, and when I look in the router's web interface during a case 2, the channel was 1.
  • There's no MAC filtering.
  • The wifi signal strength at the location of the device is excellent.
  • The router sends both the 2.4 and 5 GHz signals.

It's a 1 / 10 chance that case 1 (desired) occurs.

  • I suspect insufficient powering
    – Juraj
    May 20 at 8:01
  • The ESP-07 has a tiny ceramic antenna and a socket (IPEX) for an external antenna. If you use an external antenna, there is a small capacitor on the board which has to be removed. Changing the antenna configuration may help if poor RF performance is the main issue.
    – 6v6gt
    May 20 at 8:52
  • Since you are joining an existing WLAN and not creating your own (soft) access point, I suppose you could try explicitly forcing the mode to station mode WiFi.mode(WIFI_STA); Also, this could be similar to your problem where it appears to attribute a 169.254.x.x address to a missing DHCP reponse. arduino.stackexchange.com/questions/78604/… so you could try giving it a static IP address in the right range.
    – 6v6gt
    May 20 at 9:27
  • @6v6gt The question shows an RSSI of -45dBm reported by the router, which is a good value. Poor RF performance is unlikely to be the cause of the problem.
    – StarCat
    May 22 at 6:13


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.