-->
Page 1 of 1

Slow connection to local server but fast to remote

PostPosted: Sun Jan 03, 2016 5:25 pm
by Falesh
When I run the sketch below to connect to adafruit it takes about 50ms. When I run it to connect to my local server, XAMPP on my PC, it takes 1000ms. When I connect to the local server from my other PC it loads almost instantaneously so it's not the local server that is slow. Does anyone have any idea why this might be?

Code: Select all/*
 *  Simple HTTP get webclient test
 */
 
#include <ESP8266WiFi.h>
 
const char* ssid     = "virginmedia545641";
const char* password = "mypassword";
 
const char* host = "192.168.0.10";
//const char* host = "www.adafruit.com";

String url = "/electronics/test.php";
//String url = "/testwifi/index.html";
const int httpPort = 80;
String remoteData;

 
void setup() {
  Serial.begin(115200);
  delay(100);
 
  // We start by connecting to a WiFi network
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
void loop() {
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  unsigned long testStartTime = millis();
 
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(1);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    remoteData = client.readStringUntil('\r');
  }
  Serial.println(millis() - testStartTime);
  //Serial.println(remoteData);
  //delay(3000);
}

Re: Slow connection to local server but fast to remote

PostPosted: Mon Jan 04, 2016 4:24 am
by eduperez
First think that comes to my mind is that the local HTTP server might be tying to reverse-DNS the IP address of the client connecting. Are both the ESP and the PC on the same network? Do you use the same URL in both cases?

Re: Slow connection to local server but fast to remote

PostPosted: Mon Jan 04, 2016 9:08 am
by Falesh
The PC connects to a router which connects to the Virgin Hub which connects to the local server, all via cable. Both the PC & ESP8226 used the same url to get the page, the PC used Firefox as the browser. I tried connecting the ESP8226 wirelessly to both the router and the Virgin Hub, the results were the same.