Closed
Description
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