title: Arduino IDE/ESP8266: Why does a button have to be pressed twice to get the correct output?
body: <p>I have been teaching myself about programming the ESP8226, specifically the ESP8226-12F. I have this script based on an online example:</p>
  
  <pre><code>#include &lt;ESP8266WiFi.h&gt;
  
  WiFiServer server(80); //Initialize the server on Port 80
  
  void setup() {
    WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint
    WiFi.begin("***","***"); // Provide the (SSID, password);
    Serial.begin(115200); //Start communication between the ESP8266-12E and the monitor window
    IPAddress HTTPS_ServerIP= WiFi.localIP(); // Obtain the IP of the Server
    Serial.print("Server IP is: "); // Print the IP to the monitor window
    Serial.println(HTTPS_ServerIP);
    while(WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
  
    Serial.println(WiFi.localIP());
    server.begin(); // Start the HTTP Server
  }
  
  void loop() {
    String status;
    WiFiClient client = server.available();
    if (!client) {
      return;
    }
    Serial.println("Somebody has connected :)");
    //Read what the browser has sent into a String class and print the request to the monitor
    String request = client.readString();
    Serial.println(request);
    // Handle the Request
    if (request.indexOf("/CLOSE") != -1) {
      Serial.println(request.indexOf("/CLOSE"));
      status = "CLOSED";
    }
    if (request.indexOf("/OPEN") != -1) {
      Serial.println(request.indexOf("/OPEN"));
      status = "OPEN";
    }
    Serial.println(status);
    // The HTML
    String s = "HTTP/1.1 200 OK";
    s += "Content-Type: text/html\r\n\r\n";
    s += "&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv=\"Content-Security-Policy\"   content=\"default-src 'self'\"&gt; &lt;meta name=\"referrer\" content=\"no-referrer\" /&gt;&lt;/head&gt;&lt;body&gt;";
    s += "&lt;br&gt;&lt;input type=\"button\" name=\"b1\" value=\"OPEN DOOR\" onclick=\"javascript:location.href='/OPEN'\"&gt;";
    s += "&lt;br&gt;&lt;input type=\"button\" name=\"b2\" value=\"CLOSE DOOR\" onclick=\"javascript:location.href='/CLOSE'\"&gt;";
    if(status == "OPEN") {
      s+= "&lt;br&gt;&lt;br&gt;&lt;p&gt;OPEN&lt;/p&gt;";
    } else {
      s+= "&lt;br&gt;&lt;br&gt;&lt;p&gt;CLOSED&lt;/p&gt;";
    }
    s += "&lt;/body&gt;&lt;/html&gt;\n";
    client.flush(); //clear previous info in the stream
    client.print(s); // Send the response to the client
    delay(1);
    Serial.println("Client disonnected");
  </code></pre>
  
  <p>You'll see that the initial HTTP response creates two buttons: OPEN DOOR and CLOSE DOOR.</p>
  
  <p>The issue I am having is that the CLOSE button has to be pressed twice in order for the sketch to output "CLOSED".</p>
  
  <p>Here's the output from the Serial Monitor.</p>
  
  <p>1: First time browse to <a href="http://10.0.1.92" rel="nofollow noreferrer">http://10.0.1.92</a>:</p>
  
  <pre><code>Somebody has connected :)
  GET / HTTP/1.1
  Host: 10.0.1.92
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip, deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Connection: keep-alive
  
  Client disonnected
  </code></pre>
  
  <p>2: Press Open Button <a href="http://10.0.1.92/OPEN" rel="nofollow noreferrer">http://10.0.1.92/OPEN</a> and "OPEN" is displayed</p>
  
  <pre><code>Somebody has connected :)
  GET /OPEN HTTP/1.1
  Host: 10.0.1.92
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip, deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Connection: keep-alive
  Referer: http://10.0.1.92/
  
  
  4  // This is value of request.indexOf("/CLOSE")
  OPEN
  Client disonnected
  </code></pre>
  
  <p>3: Press CLOSE <a href="http://10.0.1.92/CLOSE" rel="nofollow noreferrer">http://10.0.1.92/CLOSE</a> and "OPEN" is still displayed</p>
  
  <pre><code>Somebody has connected :)
  GET /CLOSE HTTP/1.1
  Host: 10.0.1.92
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip, deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Connection: keep-alive
  Referer: http://10.0.1.92/OPEN
  
  
  4 
  371 // No idea where this comes from
  OPEN
  Client disonnected
  </code></pre>
  
  <p>4: Press CLOSE the second time and "CLOSED" is displayed:</p>
  
  <pre><code>Somebody has connected :)
  GET /CLOSE HTTP/1.1
  Host: 10.0.1.92
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip, deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Connection: keep-alive
  Referer: http://10.0.1.92/CLOSE
  
  
  4
  CLOSED
  Client disonnected
  </code></pre>
  
  <p>Why does the CLOSE button have to be pressed twice to get "CLOSED" to display? Is this a logic issue, or an HTTP issue, or some combination of the two?</p>