One major problem seems to exists within the AT firmware of most ESP's, by. By default a, the http headers it sends include "Connection: close". However, butthe firmware seems to be buggy and unable to cope with the server closing the connection after it sends the response. Thus, in order to be able to get a response back from the target server you need to have "Connection: keep-alive" instead. This means you must explicitly add this to your header since "Connection: close" will be added if nothing else is said. Also to make life easier for the WifiEsp library you could make sure your server returns an already well-known status string for the library like fx "SEND OK"… I'm doing this on a barometer logger that sends data to a MVC Web Api written in C# and it works perfect...
Here's my code:
String PostHeader = "POST http://" + server + ":" + String(port) + "/api/values HTTP/1.1\r\n";
PostHeader += "Connection: keep-alive\r\n";
PostHeader += "Content-Type: application/json; charset=utf-8\r\n";
PostHeader += "Host: " + server + ":" + String(port) + "\r\n";
PostHeader += "Content-Length: " + String(jsonString.length()) + "\r\n\r\n";
PostHeader += jsonString;
Serial.println(PostHeader);
client.connect(server.c_str(), port);
client.println(PostHeader);
client.stop();
And the full log looks like this:
POST http://192.168.10.197:8001/api/values HTTP/1.1
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Host: 192.168.10.197:8001
Content-Length: 144
{"IpAddress":"192.168.10.230","MacAddress":"5c:cf:7f:d9:2f:c8","Temperature":34.6,"Pressure":1007,"Altitude":48.48573,"PressureAtSeaLevel":1007}
[WiFiEsp] Connecting to 192.168.10.197
[WiFiEsp] Disconnecting 3
In the file debug.h located in the WifiEsp library source code you could alter a define and get more output to your serial console. Open the file and change
#define _ESPLOGLEVEL_ 3
to
#define _ESPLOGLEVEL_ 4
Save the file and recompile/deploy your source code to your Arduino and you will get extensive information about all AT commands the library sends and what the library receives in return.