arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
288 stars 172 forks source link

Status code -2 #29

Open abhir24 opened 7 years ago

abhir24 commented 7 years ago
#include <ArduinoHttpClient.h>
#include <SPI.h>
#include <WiFi.h>
#include "config.h"

//char ssid[] = "JioFi2_C5E559"; //  your network SSID (name)
//char pass[] = "8nfdtyc8jf";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

IPAddress server(35,154,192,57); 

//char serverAddress[] = "35.154.192.57";
int port = 8080;

WiFiClient client;
HttpClient client1 = HttpClient(client, server, port);
String response;
int statusCode = 0;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv != "1.1.0") {
    Serial.println("Please upgrade the firmware");
  }

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);

    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();
}

void loop() {
  Serial.println("making POST request");
  String contentType = "application/json";
  String postData = "one";

  client1.post("/smart_stove/api.php/checkText", contentType, postData);

  statusCode = client1.responseStatusCode();
  response = client1.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  //Serial.println("Wait five seconds");
  //delay(5000);

  /*while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }*/

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

void printWifiStatus() {
  // 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);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

The response I am getting is status code: -2 Response:

I am new to github, I hope I am using it right. Sorry otherwise.

per1234 commented 7 years ago

Also posted on the forum: http://forum.arduino.cc/index.php?topic=481200.msg3299310#msg3299310

c0dehunter commented 5 years ago

Was this ever solved? I am having the same problem. It works for the first .get(), but for second .get() and all subsequent .get()'s, I get -2.

c0dehunter commented 5 years ago

I solved this by callin http.stop() after each call.

paulchrisycj commented 3 years ago

@c0dehunter Hi, mind explaining more about how did you call http.stop()? Because I can't find a variable named http in your code. I'm facing the same issue here. Would appreciate any help you can offer 🙏🏻

bryanjj commented 1 year ago

@paulchrisycj late to the party here, but I believe he meant calling HttpClient stop() which in the above code would be: client1.stop(). I just ran into this same issue and this fix seemed to work!

lobodpav commented 1 year ago

I solved this by callin http.stop() after each call.

The httpClient.stop() should be part of the documentation and examples too. Took me a decent hour to figure this out for myself.

paulchrisycj commented 1 year ago

Actually I have solved this issue by calling httpClient.connectionKeepAlive(); in setup().

Basically my server overloaded and requests after the overloading caused this error.

By using httpClient.connectionKeepAlive(); to keep the connection alive, httpClient is just making a new request using the connection that has already been made when you set the httpClient object up.

paulchrisycj commented 1 year ago

Actually I have solved this issue by calling httpClient.connectionKeepAlive(); in setup().

Basically my server overloaded and requests after the overloading caused this error.

By using httpClient.connectionKeepAlive(); to keep the connection alive, httpClient is just making a new request using the connection that has already been made when you set the httpClient object up.

Unsure if anyone else is still facing the same issue, but if there is someone, do verify if this works. Thanks.

bryanjj commented 1 year ago

@paulchrisycj persistent vs non-persistent connections should be used depending on the use case. Unless you have a specific reason to keep the connection open, you probably should just close it.

paulchrisycj commented 1 year ago

Well based on the use case in this issue thread, the POST Request is written inside the loop() function. Since it will be making more than 1 request, it should be a persistent connection.