AsyncHTTPRequest Help

Hey everyone!

I'm using an ESP8266
I'm trying to pull XML data from a site, specifically this one
https://aviationweather.gov/cgi-bin/data/metar.php?ids=CYKF&format=xml&taf=false

I've been able to pull the information off of it with the regular WifiClientSecure, but I wanted to implement Async due to the code I already have freezing while the request is being handled, so I found this library that claims to be able to do it on the ESP8266

Just to test it, I loaded up one of the examples, specifically this one

/****************************************************************************************************************************
  AsyncHTTPRequest_ESP.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet

  For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)

  AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer

  Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest)

  Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic
  Licensed under MIT license

  Copyright (C) <2018>  <Bob Lemaire, IoTaWatt, Inc.>
  This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
  as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  You should have received a copy of the GNU General Public License along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *****************************************************************************************************************************/
//************************************************************************************************************
//
// There are scores of ways to use AsyncHTTPRequest.  The important thing to keep in mind is that
// it is asynchronous and just like in JavaScript, everything is event driven.  You will have some
// reason to initiate an asynchronous HTTP request in your program, but then sending the request
// headers and payload, gathering the response headers and any payload, and processing
// of that response, can (and probably should) all be done asynchronously.
//
// In this example, a Ticker function is setup to fire every 300 seconds to initiate a request.
// Everything is handled in AsyncHTTPRequest without blocking.
// The callback onReadyStateChange is made progressively and like most JS scripts, we look for
// readyState == 4 (complete) here.  At that time the response is retrieved and printed.
//
// Note that there is no code in loop().  A code entered into loop would run oblivious to
// the ongoing HTTP requests.  The Ticker could be removed and periodic calls to sendRequest()
// could be made in loop(), resulting in the same asynchronous handling.
//
// For demo purposes, debug is turned on for handling of the first request.  These are the
// events that are being handled in AsyncHTTPRequest.  They all begin with Debug(nnn) where
// nnn is the elapsed time in milliseconds since the transaction was started.
//
//*************************************************************************************************************

#if !( defined(ESP8266) ||  defined(ESP32) )
  #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting.
#endif

// Level from 0-4
#define ASYNC_HTTP_DEBUG_PORT     Serial
#define _ASYNC_HTTP_LOGLEVEL_     4

// 300s = 5 minutes to not flooding
#define HTTP_REQUEST_INTERVAL     60  //300

// 10s
#define HEARTBEAT_INTERVAL        10

int status;     // the Wifi radio's status

const char* ssid        = "SSID";
const char* password    = "PASS";

#if (ESP8266)
  #include <ESP8266WiFi.h>
#elif (ESP32)
  #include <WiFi.h>
#endif

#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET      "AsyncHTTPRequest_Generic v1.10.2"
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN             1010002

// Seconds for timeout, default is 3s
#define DEFAULT_RX_TIMEOUT           10

// Uncomment for certain HTTP site to optimize
//#define NOT_SEND_HEADER_AFTER_CONNECTED        true

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <AsyncHTTPRequest_Generic.h>             // https://github.com/khoih-prog/AsyncHTTPRequest_Generic

#include <Ticker.h>

AsyncHTTPRequest request;
Ticker ticker;
Ticker ticker1;

void heartBeatPrint()
{
  static int num = 1;

  if (WiFi.status() == WL_CONNECTED)
    Serial.print(F("H"));        // H means connected to WiFi
  else
    Serial.print(F("F"));        // F means not connected to WiFi

  if (num == 80)
  {
    Serial.println();
    num = 1;
  }
  else if (num++ % 10 == 0)
  {
    Serial.print(F(" "));
  }
}

void sendRequest()
{
  static bool requestOpenResult;

  if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
  {
    //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt");
    requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt");

    if (requestOpenResult)
    {
      // Only send() if open() returns true, or crash
      request.send();
    }
    else
    {
      Serial.println(F("Can't send bad request"));
    }
  }
  else
  {
    Serial.println(F("Can't send request"));
  }
}

void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState)
{
  (void) optParm;

  if (readyState == readyStateDone)
  {
    AHTTP_LOGDEBUG(F("\n**************************************"));
    AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString());

    if (request->responseHTTPcode() == 200)
    {
      Serial.println(F("\n**************************************"));
      Serial.println(request->responseText());
      Serial.println(F("**************************************"));
    }
  }
}

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);

  while (!Serial && millis() < 5000);

  Serial.print(F("\nStarting AsyncHTTPRequest_ESP using "));
  Serial.println(ARDUINO_BOARD);
  Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION);

  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);

  Serial.print(F("Connecting to WiFi SSID: "));
  Serial.println(ssid);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(F("."));
  }

  Serial.print(F("\nAsyncHTTPRequest @ IP : "));
  Serial.println(WiFi.localIP());

  request.setDebug(false);

  request.onReadyStateChange(requestCB);
  ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest);

  ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);

  // Send first request now
  sendRequest();
}

void loop()
{
}

And it worked with the default URL included, but as soon as I changed it over to the URL I want to pull data from, I get an error saying:

open: error parsing URL
Can't send bad request

Any help or insight would be appreciated, I'm not really all that familiar with networking and APIs and whatnot

By the way, this is the code that I was able to successfully implement, however it is blocking which is what I'm trying to avoid

#include <ESP8266WiFi.h>

const char* ssid = "SSID";
const char* password = "PASS";
const int httpPort = 443;

const char* host = "aviationweather.gov";

void setup() {
  Serial.begin(115200);
  while (!Serial);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Print the IP address
  Serial.println(WiFi.localIP());

  Serial.println("type enter in the Serial monitor to trigger a request");

}

void loop() {
  if (Serial.read() == '\n') { // type enter in the Serial monitor to trigger a request
    WiFiClientSecure client;
    client.setInsecure();
    if (!client.connect(host, httpPort)) {
      Serial.print("connection to "); Serial.print(host); Serial.println(" failed.");
      return;
    }

    String url = "/cgi-bin/data/metar.php?ids=CYKF&format=xml&taf=false";
    Serial.print("Requesting URL: ");  Serial.print("http://"); Serial.print(host); Serial.println(url);

    client.print("GET ");
    client.print(url);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(host);
    client.println("User-Agent: LED Sectional Client");
    client.println("Connection: close\r\n");
    client.println();
    client.flush();

    while (!client.available()) yield(); // active wait for the answer (bad)

    Serial.println("Response:\n----------------------------");
    while (client.available())  Serial.write(client.read()); // dump the answer to the Serial monitor
    Serial.println();
    Serial.println("\n----------------------------");
    Serial.println("closing connection");
    client.stop();
    Serial.println("type enter in the Serial monitor to trigger a request");
  }
}

don't you need HTTPS ?

I'm not sure honestly, there is an AsyncHTTPSRequest library however it looks like it isn't supported by the ESP8266 :frowning:

Appreciate the input though, I've kept trying and so far no luck with anything I've tried other than the blocking code

The server won’t answer to an HTTP request if it’s expecting an https one.

The esp8266 has only one core so you won’t do two things really in parallel… the payload does not seem too large, how long does the request take?

Most of the time only about 2 or so seconds, sometimes it can take more but enough to cause some issues with functionality.

Have you considered an ESP32 ?

I have, I was hoping there would be some sort of solution with the ESP8266 as I've already built the majority of the project around it, you're saying that might be the only option?

I’m unclear what the exact issue you are having. Do you need to read that xml often?

It's every minute or so, what I'm making involved an LED strip and it tends to freeze for a few seconds when the request is going through, it needs to poll the XML (I have switched to JSON now) every minute. If you say there's no way to do async on the 8266 then so be it

it may be possible, if you look at that library the author states they plan to support the ESP8266 in the future...

I don't know of a library which does that today (and I stopped using esp8266 given the price of the ESP32 boards....)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.