Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Falesh
#38228 I have got my NodeMCU working well now connecting to my local server. The one thing I still need to work out is the inconsistency of the time it takes to fetch a page from the server. Mostly it takes 5ms but sometimes it spikes up to anything between 50ms or 150ms and very occasionally 3600ms.

I put a load over various sized capacitors on the power line so that shouldn't be causing the issue. Is there a way to add a timeout to client.connect() so if it takes longer then 20ms it stops and retries the connection? For my application to work I need it to consistently take less then 90ms per request.

Code: Select all#include <ESP8266WiFi.h>
 
char ssid[] = "virginmedia54841";
char pass[] = "ljksdfli30";

const char* host = "192.168.0.27";
IPAddress server(192,168,0,27);

WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(100);
 
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, pass); 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  } 
  Serial.println("Connected to wifi");
}
 
void loop() {
  unsigned long testStartTime = millis();

  if (client.connect(server, 80)) {
    client.print(String("GET ") + "/index.php HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ArduinoWiFi/1.1" +
               "Connection: close\r\n\r\n");
    client.println();
  } else {
    Serial.println("connection failed");
  }

  Serial.println(millis() - testStartTime);
  client.stop();
  delay(100);
}
User avatar
By WereCatf
#38233 For one, are you sure the issue doesn't lie with the server and/or your local network? Can any other device using 2.4GHz WIFI get consistent <90ms replies from your server?

Secondly, it's a bad idea to design the sketch to rely on such strict timings over WIFI since WIFI is always subject to interference.

Thirdly, I do not know if running the sketch as both an AP and a STA simultaneously can affect such things, but you could test adding a
Code: Select allWiFi.mode(WIFI_STA);
before bringing WIFI up to set it strictly as a STA and seeing if that changes anything.
User avatar
By Falesh
#38295 Thanks for the reply. I set up another wireless router and tried it on that. The results were much more stable but I was still getting the odd rare 3.5 second delay.

It would still be very useful to be able to set the timeout/retries on client.connect() so I am able to do something else in the code if it takes too long. For instance I could keep getting data and filling up an external SRAM chip to act as a data buffer for 90ms then do the other stuff I have planned. However this wouldn't work if I have no way to break out of an abnormally long client.connect().
User avatar
By WereCatf
#38304 Now that I look at your sketch, you're actually testing the delay of client.connect() + client.print() -- modify your sketch to check how long client.connect() takes, then a separate reading for client.print(), like e.g. follows:
Code: Select all#include <ESP8266WiFi.h>
 
char ssid[] = "virginmedia54841";
char pass[] = "ljksdfli30";

const char* host = "192.168.0.27";
IPAddress server(192,168,0,27);

WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(100);
 
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to wifi");
}
 
void loop() {
  unsigned long testStartTime = millis();

  if (client.connect(server, 80)) {
    Serial.print("client.connect() millis: ");
    Serial.println(millis() - testStartTime);
    testStartTime = millis();
    client.print(String("GET ") + "/index.php HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ArduinoWiFi/1.1" +
               "Connection: close\r\n\r\n");
    client.println();
    Serial.print("client.print() millis: ");
    Serial.println(millis() - testStartTime);
  } else {
    Serial.println("connection failed");
  }

  client.stop();
  delay(100);
}


Now you can see which part it actually is that takes so long and maybe we can then see if we can do anything about it, yes?