Hi guys,
I am playing with the HttpClient library.
My current goal is to POST some data on a server within my local network. I started a conversation here, where we can see the debug log:
HttpClient> Connecting to IP: 192.168.1.2:80
HttpClient> Start of HTTP Request.
POST /wifispark HTTP/1.0
Connection: close
Content-Length: 19
Content-Type: application/json
Accept: */*
Connection: Close
{"humidity":"43.5"} // <-----this is the body of the POST request
HttpClient> End of HTTP Request.
HttpClient> Receiving TCP transaction of 85 bytes.
HTTP/1.1 200 OK
Date: Sat, 09 Apr 2016 10:54:46 GMT
Connection: close
Thank you! // <-----this is the answer from the server
HttpClient> End of TCP transaction.
HttpClient> Error: Timeout while reading response.
HttpClient> End of HTTP Response (5700ms).
HttpClient> Status Code: 200
We can see that a timeout occurs after receiving the response from the server. It seems that the client is still waiting for something in order to close the TCP connection. I made a log of the server-side TCP/IP packets (wireshark format) that you can get here. Among others, we can clearly see that the client is not closing the TCP connection, and sends a [PSH,ACK] instead of sending a [FIN,ACK] (frame number 54 at time 00:52:59.568634000).
Here is the client-side code:
// This #include statement was automatically added by the Particle IDE.
#include "HttpClient/HttpClient.h"
TCPClient client;
HttpClient http;
IPAddress ServerIP(192, 168, 1, 2); //the IP address of my local server
unsigned int nextHTTPRequest = 0; // Next time to contact the server
#define HTTPREQUESTINTERVAL_MS 20000
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "Accept" , "*/*"},
{ "Connection","Close"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
void setup() {
request.port = 80;
request.ip=ServerIP;
request.path ="/wifispark";
}
void loop() {
if( nextHTTPRequest<millis() )
{
nextHTTPRequest=millis()+HTTPREQUESTINTERVAL_MS;
request.body = "{\"humidity\":\"43.5\"}";
http.post(request, response, headers);
if(response.status==200)
{
//OK
}
else
{
//ERROR
}
response.body="";
}
delay(100);
}
Any help would be really appreciated.