-->
Page 1 of 2

Unable to use HTTP client in ESPAsyncWebServer callback

PostPosted: Sun May 13, 2018 10:39 am
by Cosmic Mac
Hi guys,

I'm unable to use HTTP client from inside a ESPAsyncWebServer callback ("connection refused" error).
Same function called from anywhere outside the callback works as expected.

My test code:
Code: Select all#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncWebServer.h>

const char *ssid = "********";
const char *password = "********";
const char *hostname = "esptest";
const char *url = "http://httpbin.org/ip";

HTTPClient http;
AsyncWebServer server(80);

void cltTest(const char *context) {

    Serial.printf("\n\n>>>> cltTest called from %s\nHeap: %d\n", context, ESP.getFreeHeap());

    http.begin(url);
    if (http.GET() > 0) {
        http.writeToStream(&Serial);
    }
    http.end();
}

void setup() {

  Serial.begin(115200);
  Serial.setDebugOutput(true);

  Serial.printf("Attempting to connect to SSID: %s", ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nConnected!");

  MDNS.begin(hostname);
  MDNS.addService("http", "tcp", 80);

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    cltTest("Server callback");
    request->send(200, "text/plain", "Done");
  });
  server.begin();

  cltTest("Setup");
}

void loop() {
  MDNS.update();
}


And some traces, after pointing my browser at http://esptest.local :
Code: Select allSDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:1.4.0rc2

[...]

>>>> cltTest called from Setup
Heap: 38776
[HTTP-Client][begin] url: http://httpbin.org/ip
[HTTP-Client][begin] host: httpbin.org port: 80 url: /ip
[HTTP-Client] connected to httpbin.org:80
[HTTP-Client] sending request header
-----
GET /ip HTTP/1.1
Host: httpbin.org
User-Agent: ESP8266HTTPClient
Connection: close
Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0

-----
[HTTP-Client][handleHeaderResponse] RX: 'HTTP/1.1 200 OK'
[HTTP-Client][handleHeaderResponse] RX: 'Connection: close'
[HTTP-Client][handleHeaderResponse] RX: 'Server: gunicorn/19.8.1'
[HTTP-Client][handleHeaderResponse] RX: 'Date: Sun, 13 May 2018 15:26:56 GMT'
[HTTP-Client][handleHeaderResponse] RX: 'Content-Type: application/json'
[HTTP-Client][handleHeaderResponse] RX: 'Content-Length: 26'
[HTTP-Client][handleHeaderResponse] RX: 'Access-Control-Allow-Origin: *'
[HTTP-Client][handleHeaderResponse] RX: 'Access-Control-Allow-Credentials: true'
[HTTP-Client][handleHeaderResponse] RX: 'Via: 1.1 vegur'
[HTTP-Client][handleHeaderResponse] RX: ''
[HTTP-Client][handleHeaderResponse] code: 200
[HTTP-Client][handleHeaderResponse] size: 26
{"origin":"123.45.67.89"}
[HTTP-Client][writeToStreamDataBlock] connection closed or file end (written: 26).
[HTTP-Client][end] tcp stop
[HTTP-Client][end] tcp is closed
pm open,type:2 0


>>>> cltTest called from Server callback
Heap: 35448
[HTTP-Client][begin] url: http://httpbin.org/ip
[HTTP-Client][begin] host: httpbin.org port: 80 url: /ip
[HTTP-Client] failed connect to httpbin.org:80
[HTTP-Client][returnError] error(-1): connection refused
[HTTP-Client][end] tcp is closed


Any idea?

Cheers

Re: Unable to use HTTP client in ESPAsyncWebServer callback

PostPosted: Sun May 13, 2018 12:02 pm
by Cosmic Mac
Well, I just tried with ESP32, and this code is running flawlessly... (after usual tweaks on dependencies names)
:(

Re: Unable to use HTTP client in ESPAsyncWebServer callback

PostPosted: Sat May 19, 2018 1:17 pm
by Cosmic Mac
So, no idea, really?

Re: Unable to use HTTP client in ESPAsyncWebServer callback

PostPosted: Sat May 19, 2018 1:30 pm
by rudy
Cosmic Mac wrote:Well, I just tried with ESP32, and this code is running flawlessly... (after usual tweaks on dependencies names)
:(


When I read this I assumed you meant that you found your solution. Maybe that's what other people thought.

Now, knowing you still are looking for a solution, unfortunately I don't know enough to help.