0

I have a strange issue with my sketch using WiFiEsp.h library from https://github.com/bportaluri/WiFiEsp

When i send 1 request to my ESP8266 from my browser as http://192.168.0.25 , it seems that my ESP8266 receives 2 requests and run 2 times. See the sketch here and Serial Termninal:

void loop()
{
  // listen for incoming clients
  WiFiEspClient client = server.available();
  if (client) {
    Serial.println("New client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          Serial.println("Sending response");

          // send a standard http response header
          // use \r\n instead of many println statements to speedup data send
          client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<html>\r\n");
          client.print("Hello World!\r\n");
          client.print("</html>\r\n");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(10);

    // close the connection:
    client.stop();
    Serial.println("Client disconnected");
  }
}

Serial temrinal:

[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: Numericable-6010
[WiFiEsp] Connected to Numericable-6010
You're connected to the network
SSID: Numericable-6010
IP Address: 192.168.0.25

To see this page in action, open a browser to http://192.168.0.25

[WiFiEsp] Server started on port 80
[WiFiEsp] New client 0
New client
GET / HTTP/1.1
Host: 192.168.0.25
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.0.25/
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7

Sending response
[WiFiEsp] Disconnecting  0
Client disconnected
[WiFiEsp] New client 1
New client
GET /favicon.ico HTTP/1.1
Host: 192.168.0.25
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://192.168.0.25/
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7

Sending response
[WiFiEsp] Disconnecting  1
Client disconnected

Please, could you explain why 2 clients connected ? How can i disconnect just after the first one ?

  • 1
    browsers automaticaly request /favicon.ico for sites – Juraj Dec 3 '18 at 14:33
  • Thanks for information, but how should i code my url request ? – Teddol Dec 3 '18 at 16:03
  • Could you explain the 'href' parameters ? – Teddol Dec 3 '18 at 19:26
  • instead of pointing to a server, the icon is encoded in the href parameter as base64 string – Juraj Dec 3 '18 at 19:27
2

Browsers automaticaly request /favicon.ico for sites. Add this to <head> section of your HTML to eliminate the 'favicon' request:

<link rel="icon" href="data:;base64,iVBORw0KGgo=">

client.println("HTTP/1.1 200 OK\r\n" "Content-Type: text/html"); 
client.println("Connection: close"); 
client.println(); 
client.println("<!DOCTYPE HTML>"); 
client.println("<html><head>"); 
client.println("<link rel='icon' href='data:;base64,iVBORw0KGgo='>"); 
client.println("</head><body>"); 
client.println("Hello World!"); 
client.println("</body></html>");

based on this SO answer, instead of pointing to a server, the icon is encoded in the href parameter as base64 string

  • I changed the <link> to the last edit of the SO answer – Juraj Dec 3 '18 at 19:20
0

Adding head part with hortcut icon link in html response fix the issue. Thanks !!!

            client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<head>\r\n");
          client.print("<link rel='shortcut icon' href='data:image/x-icon;,' type='image/x-icon'> \r\n");
          client.print("</head>\r\n");
          client.print("<html>\r\n");
          client.print("Hello World!\r\n");
          client.print("</html>\r\n");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.