When I use MKR1000 with ARDUINOHTTPCLIENT library and some simple http post example I have problem that my Arduino sends the request only twice. After that I get error code in response -2. I have tried to change time and restart the Arduino many times, but I keep getting the same problem. I change server domain where my Arduino make call and the problem is still same.
Here is code. Can anyone help me:
#include "ArduinoHttpClient.h"
#include "WiFi101.h"
//Setup Wifi Parameters
char ssid[] = "xxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";
char serverAddress[] = "xxxxxx.xxxxxxx.com";
int port = 80;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
String response;
int statusCode = 0;
void setup()
{
Serial.begin(9600);
while ( status != WL_CONNECTED)
{
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
delay(6000);
}
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop()
{
Serial.println("making POST request");
String contentType = "application/x-www-form-urlencoded";
String postData = "name=Alice&age=12";
client.post("/public/insert_mysql.php HTTP/1.1", contentType, postData);
// read the status code and body of the response
statusCode = client.responseStatusCode();
response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
Serial.println("Wait five seconds");
delay(5000);
}
This is information from Serial Debug:
SSID: xxxxxxxxxxxxx
IP Address: xxxxxxxxx
making POST request
Status code: 200
Response:
Wait five seconds
making POST request
Status code: 200
Response:
Wait five seconds
making POST request
Status code: -2
Response:
Wait five seconds
making POST request
Status code: -2
Response:
Wait five seconds
making POST request
Status code: -2
Response:
Wait five seconds
making POST request