POST to a web server is really slow
Posted: Sun Dec 23, 2018 4:15 pm
Hello,
I have some performance issue with the below code.
In fact, the execution of client.readString() takes around 15s. It is placed in the read_response state of my finite state machine.
Is this long time a normal execution time? Or do I something wrong?
Many thanks in advance for help or tips which will solve this performance issue.
Thoma
I have some performance issue with the below code.
In fact, the execution of client.readString() takes around 15s. It is placed in the read_response state of my finite state machine.
Is this long time a normal execution time? Or do I something wrong?
Many thanks in advance for help or tips which will solve this performance issue.
Thoma
Code: Select all
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
WiFiClientSecure client;
const int port = 443;
char * ssid = "----";
char * password = "----";
const char* host = "myserver.tld";
String url = "/store.php";
char buffer[8193];
enum { open_connection, post_data, awaiting_response, read_response, close_connection} state;
unsigned long start;
void setup()
{
for (int index = 0; index < 8192; index++)
{
buffer[index] = '0' + ((index + 1) % 10);
}
buffer[8192] = '\0';
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
state = open_connection;
}
void loop()
{
switch(state)
{
case open_connection :
if (client.connect(host, port))
{
Serial.println("Connected...");
start = millis();
state = post_data;
}
break;
case post_data :
post(host, url, buffer);
Serial.print("POST... (");
Serial.print(millis() - start);
Serial.println(" ms)");
state = awaiting_response;
break;
case awaiting_response :
if (client.available())
{
Serial.print("Response available... (");
Serial.print(millis() - start);
Serial.println(" ms)");
state = read_response;
}
break;
case read_response :
Serial.print("Start reading... (");
Serial.print(millis() - start);
Serial.println(" ms)");
client.readString();
Serial.print("Complete reading... (");
Serial.print(millis() - start);
Serial.println(" ms)");
if (!client.available())
{
Serial.print("Response read... (");
Serial.print(millis() - start);
Serial.println(" ms)");
state = close_connection;
}
break;
case close_connection :
Serial.print("Operation tooks: ");
Serial.print(millis() - start);
Serial.println(" ms");
client.stop();
Serial.print("Closed... (");
Serial.print(millis() - start);
Serial.println(" ms)");
Serial.println();
state = open_connection;
break;
default :
state = open_connection;
break;
}
}
void post(String _host, String _url, char * _data)
{
client.println(String(F("POST ")) + _url + F(" HTTP/1.1"));
client.println(String(F("Host: ")) + _host);
client.println(F("User-Agent: ESP8266/1.0"));
client.println(F("Connection: close"));
client.println(F("Content-Type: application/json"));
client.print(F("Content-Length: "));
client.println(strlen((const char *)_data));
client.println();
client.println(_data);
}