svenyonson
svenyonson

Reputation: 1909

Unable to send HTTPClient requests from within ESPAsyncWebServer handler

I have an ESP8266 that I am using as a web server that can control several Wi-Fi switches. I have functions that can GET or POST to the Wi-Fi switches successfully when called from within loop(). But if I call those same functions from within a server.on('xxxx') handler, HTTPClient fails to connect.

The function:

void getZone(int zone) {

    WiFiClient client;
    HTTPClient http;

    String ip = getZoneIP(zone);
    String url = "http://"+ip+"/status";
    Serial.printf("getZone - url: %s\n", url.c_str());

    http.begin(client, url);
    http.setTimeout(3000);

    int httpCode = http.GET();

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

      if (httpCode == HTTP_CODE_OK) {
        // Do something
      }
      else {
        // Handle error
      }
    } else {
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    http.end();
}

Calling the function from the web server handler:

server.on("^\\/zone\\/([0-9]+)$", HTTP_GET, [](AsyncWebServerRequest *request){
  int zone = request->pathArg(0).toInt();

  // THIS FAILS: 
  getZone(zone);

  //  getZone - url: http://192.168.0.224/status
  //  [HTTP-Client][begin] url: http://192.168.0.224/status
  //  [HTTP-Client][begin] host: 192.168.0.224 port: 80 url: /status
  //  [HTTP-Client][sendRequest] type: 'GET' redirCount: 0
  //  [HTTP-Client] failed connect to 192.168.0.224:80
  //  [HTTP-Client][returnError] error(-1): connection failed
  //  [HTTP] GET... failed, error: connection failed
  //  [HTTP-Client][end] tcp is closed


});

Calling the function from the main loop:

unsigned long previousMillis = 0;
#define POLL_INTERVAL (60*1000)   

void loop() {
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= POLL_INTERVAL) {
    previousMillis = currentMillis;

    // THIS WORKS
    getZone(0);

    // getZone - url: http://192.168.0.224/status
    // [HTTP-Client][begin] url: http://192.168.0.224/status
    // [HTTP-Client][begin] host: 192.168.0.224 port: 80 url: /status
    // [HTTP-Client][sendRequest] type: 'GET' redirCount: 0
    // [HTTP-Client] connected to 192.168.0.224:80
    // [HTTP-Client] sending request header
    // -----
    // GET /status HTTP/1.1
    // Host: 192.168.0.224
    // User-Agent: ESP8266HTTPClient
    // Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    // Connection: keep-alive
    // Content-Length: 0

    // -----
    // [HTTP-Client][handleHeaderResponse] RX: 'HTTP/1.1 200 OK
    // [HTTP-Client][handleHeaderResponse] RX: 'Content-Type: text/html
    // [HTTP-Client][handleHeaderResponse] RX: 'Access-Control-Allow-Origin: *
    // [HTTP-Client][handleHeaderResponse] RX: 'Content-Length: 2
    // [HTTP-Client][handleHeaderResponse] RX: 'Connection: close
    // [HTTP-Client][handleHeaderResponse] RX: '
    // [HTTP-Client][handleHeaderResponse] code: 200
    // [HTTP-Client][handleHeaderResponse] size: 2
  }
}

Upvotes: 0

Views: 778

Answers (0)

Related Questions