Skip to content

Multiple request failed with random error code from -1 to -5 #1200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
laibulle opened this issue Dec 12, 2015 · 1 comment
Closed

Multiple request failed with random error code from -1 to -5 #1200

laibulle opened this issue Dec 12, 2015 · 1 comment

Comments

@laibulle
Copy link

Hi,

I am creating a small extreme feedback device and I am experimenting some issues with HttpClient.

Actually request failed randomly and when is set http.setReuse(false), just the first request is succesfull.

It's working with bash so I am pretty sur that it is not a server issue.

Am I missing something ?

Thank you.

My code

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

// Wifi settings
const char SSID[ ] = "myssid";
const char SECRET[ ] = "mysecreet";

// Jenkins parameters
const char SERVER [ ] = "server";
const int PORT = 80;
const bool HTTPS = false;
const String ENDPOINTS[ ] = {"/job/success/api/json?tree=color", "/job/test/api/json?tree=color"};

// System settings
const int LED = 2;
const int CHECK_DELAY = 1000;

bool success = true;

void setup() {
    USE_SERIAL.begin(115200);
    USE_SERIAL.printf("[SETUP] WAIT ...\n");
    USE_SERIAL.flush();
    pinMode(LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output

    WiFiMulti.addAP(SSID, SECRET);
}

String getColor(HTTPClient *http, String endpoint){

    http->begin(SERVER, PORT, endpoint); //HTTP

    USE_SERIAL.println("[HTTP] " + endpoint);

    // start connection and send HTTP header
    int httpCode = http->GET();
    if(httpCode) {

        // HTTP header has been send and Server response header has been handled
        USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

    // file found at server
    if(httpCode == 200) {
      String payload = http->getString();
      payload.replace("{\"color\":\"", "");
      payload.replace("\"}", "");
      USE_SERIAL.println("[HTTP] " + payload);

      return payload;     
    }
  } else {
    USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
  }

  http->end();
  delay(200);

  return "";
}

/**
 * Build is success when color is BLUE
 */
bool isSuccess(String color){
  return color.startsWith("blue");
}

/**
 * Is building
 */
bool isBuilding(String color){
  return color.endsWith("_anime");
}

/**
 * Alternate the GPIO level
 */
void blink(int delay_ms, int count){
  for(int i=0; i <= count; i++){
    digitalWrite(LED, HIGH);  // Turn the LED off by making the voltage LOW
    delay(delay_ms);
    digitalWrite(LED, LOW);  // Turn the LED off by making the voltage HIGH
    delay(delay_ms);
  }
}

/**
 * Diplay the result of the build
 */
void display(){
  if(success){
      digitalWrite(LED, LOW);  // Turn the LED off by making the voltage LOW
      USE_SERIAL.print("[DISPLAY] Build is success\n");
    }else{
      digitalWrite(LED, HIGH);  // Turn the LED off by making the voltage HIGH
      USE_SERIAL.print("[DISPLAY] Build failed\n");
    }
}

void loop() {

  // wait for WiFi connection
  if((WiFiMulti.run() == WL_CONNECTED)) {
    HTTPClient http;
    http.setReuse(true);

    blink(10, 2);
    display();

    bool currentSuccess = true;  
    bool currentBuilding = false;   
    for(int i = 0; i < sizeof(ENDPOINTS) / sizeof(String); i++) {
      String color = getColor(&http, ENDPOINTS[i]);

      if(!isSuccess(color)){
        currentSuccess = false;
      }

      if(isBuilding(color)){
        currentBuilding = true;
      }
    }


    success = currentSuccess;

    display();
  }

  delay(CHECK_DELAY);   
}

OUTPUT

[SETUP] WAIT ...
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -5
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -2
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: -1
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: -1
[DISPLAY] Build failed
[DISPLAY] Build failed
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
[DISPLAY] Build is success
[HTTP] /job/success/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[HTTP] /job/test/api/json?tree=color
[HTTP] GET... code: 200
[HTTP] blue
[DISPLAY] Build is success
@Links2004
Copy link
Collaborator

error codes:

#define HTTPC_ERROR_CONNECTION_REFUSED  (-1)
#define HTTPC_ERROR_SEND_HEADER_FAILED  (-2)
#define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
#define HTTPC_ERROR_NOT_CONNECTED       (-4)
#define HTTPC_ERROR_CONNECTION_LOST     (-5)
#define HTTPC_ERROR_NO_STREAM           (-6)
#define HTTPC_ERROR_NO_HTTP_SERVER      (-7)

https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h#L37-L43

its look like a connection problem to the server. check your WiFi / server for stability.
the usage of the library looking good.

the current git version allow to print the errors as strings.
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino#L64

@igrr igrr closed this as completed Feb 29, 2016
Normola pushed a commit to Normola/Arduino that referenced this issue Feb 19, 2020
fixed printing/removing order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants